-
-
Save jdmonty/5eb9e6adbb51d72d074d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Markov chain that learns by observing data. | |
/// Let it observe a sequence of states. | |
/// Then you can ask it, given a state, | |
/// what are the probabilities of the next states occuring? | |
type MarkovChain (numStates : int) = | |
let mutable numObservations = 0 | |
let mutable lastState = 0 | |
let mutable matrix = Array2D.create numStates numStates (0, 0) | |
let approx (n : int, d : int) = | |
if d > 0 then float n / float d | |
else 0.0 | |
member this.TransitionProbabilities = | |
if numObservations = 0 then Array2D.create numStates numStates (0.0) | |
else | |
let r = 1 | |
matrix |> Array2D.map approx | |
member this.PredictNextState (state : int) = | |
let r = Array.create numStates 0.0 | |
for i = 0 to (numStates - 1) do | |
r.[i] <- matrix.[state, i] |> approx | |
r | |
member this.Observe (state : int) = | |
if numObservations > 0 then | |
for i = 0 to (numStates - 1) do | |
let s, c = matrix.[lastState, i] | |
let a = if i = state then 1 else 0 | |
matrix.[lastState, i] <- (s + a, c + 1) | |
lastState <- state | |
numObservations <- numObservations + 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment