From bcecb3b08891a386932eb0942f93ae872703e9e5 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 13 Feb 2020 12:56:09 -0800 Subject: [PATCH] markov-chains: initial implementation. --- extra/markov-chains/authors.txt | 1 + extra/markov-chains/markov-chains.factor | 21 +++++++++++++++++++++ extra/markov-chains/summary.txt | 1 + 3 files changed, 23 insertions(+) create mode 100644 extra/markov-chains/authors.txt create mode 100644 extra/markov-chains/markov-chains.factor create mode 100644 extra/markov-chains/summary.txt diff --git a/extra/markov-chains/authors.txt b/extra/markov-chains/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/markov-chains/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/markov-chains/markov-chains.factor b/extra/markov-chains/markov-chains.factor new file mode 100644 index 0000000000..ef2da1986a --- /dev/null +++ b/extra/markov-chains/markov-chains.factor @@ -0,0 +1,21 @@ +! Copyright (C) 2020 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: assocs assocs.extras assocs.private fry grouping kernel +math.extras random sequences ; + +IN: markov-chains + +: transitions ( string -- clumps ) + { t } { f } surround 2 clump ; + +: push-transitions ( table seq -- table ) + transitions over [ + [ drop H{ } clone ] cache inc-at + ] with-assoc assoc-each ; + +: transition-table ( seq -- table ) + H{ } clone swap [ push-transitions ] each ; + +: markov-chain ( table -- seq ) + t swap '[ _ at weighted-random dup ] [ dup ] produce nip ; diff --git a/extra/markov-chains/summary.txt b/extra/markov-chains/summary.txt new file mode 100644 index 0000000000..2eb763e2b2 --- /dev/null +++ b/extra/markov-chains/summary.txt @@ -0,0 +1 @@ +Markov chain processes.