, , : loop ?
- fn , , , . repeatedly, .
, , , format - clojure.string/join, , apply str over .
- , , , , :
(defn make-generator
"Takes a probability distribution, in the form of a map
from values to the desired likelihood of that value appearing in the output.
Normalizes the probabilities and returns a nullary producer fn with that distribution."
[p-distribution]
(let[sum-probs (reduce + (vals p-distribution))
normalized (reduce
(fn [] (reduce
(rand)
normalized))))
(defn markov-chain
"Takes a series of states, returns a producer fn.
Each call, the process changes to the next state in the series with probability :p-switch,
and produces a value from the :producer of the current state."
[states]
(let[cur-state (atom (first states))
next-states (atom (cycle states))]
(fn []
(when (< (rand) (:p-switch @cur-state))
(reset! cur-state (first @next-states))
(swap! next-states rest))
((:producer @cur-state)))))
(def my-states [{:p-switch 0.02 :producer (make-generator {\A 1 \C 1 \G 1 \T 1}) :name "std"}
{:p-switch 0.04 :producer (make-generator {\A 1 \C 4 \G 4 \T 1}) :name "cpg"}])
(defn create-genome [n]
(->> my-states
markov-chain
(repeatedly n)
clojure.string/join))
, :
let in make-generator , 1.make-generator , reduce. , , 2 . (reduce + [4 5 2 9]) (((4 + 5) + 2) + 9). , if create-model, .markov-chain cur-state next-states, ( cycle) . m models, .when, , , , . : @cur-state .
, , , , , .
, , ( ) . " " , .