Thursday, 15 August 2013

clojure - Function taking sequential args and own previous value -



clojure - Function taking sequential args and own previous value -

i'm doing work hidden markov models. more specifically, forwards , backward algorithms filtering , smoothing. i've settled on representation , have working forwards fn takes previous probability distribution hidden variable , model , returns new probability distribution. want filtering function takes sequence of sensor states , model , returns sequence consisting of

the initial state (contained within model) the result of using forwards on previous state in homecoming sequence, next sensor state , model. repeat 2 until no more sensor states remain.

i've managed working recursion, since it's not tail-position recursion breaks recur , seems non-idiomatic , bad solution. i've tried formulate work for, reductions , iterate can't seem create of them fit. way improve it?

(defn filtering "perform filtering decide state based on evidence. returns sequence of state probabilities given sequence of evidence." [evidence {:keys [transition sense initial state-map] :as model}] (if (empty? evidence) (vector initial) (let [reading (first evidence) history (filtering (drop 1 evidence) model) previous-state (vector (peek history)) fwd (forward previous-state reading model) ] (conj history fwd) ) ) )

the recursion first goes downwards end of evidence , produces (:initial model). initial value of reductions.

then each returning level of recursion applies #(forward %1 %2 model) result of application lower recursion level (as %1, taken peek) , next element of evidence (as %2). code applies iteratively two-argument function consecutive elements of sequence (taken in reverse order) - case reduce.

but results accumulated in history vector returned upper levels of recursion. reduce accumulated results reductions. believe construction of code this:

(reductions #(forward %1 %2 model) (:initial model) (reverse evidence))

clojure

No comments:

Post a Comment