diff --git a/src/jit_bayes b/src/jit_bayes index 227fea4..f402e3c 100755 Binary files a/src/jit_bayes and b/src/jit_bayes differ diff --git a/src/jit_bayes.nim b/src/jit_bayes.nim index 8321f2b..991a693 100644 --- a/src/jit_bayes.nim +++ b/src/jit_bayes.nim @@ -4,20 +4,10 @@ import sequtils import std/sugar import std/algorithm -## Define type +## Prediction type & helpers. type prediction = (string, float) # string represents a hypothesis, # prediction represents the predictionability mass - -## Utils -## Find index (or -1) -proc findIndex(xs: seq[string], y: string): int = - for i, x in xs: - if x == y: - return i - return -1 - -## Do simple predictions proc comparePredictions (x: prediction, y: prediction): int = let (_, p1) = x let (_, p2) = y @@ -33,6 +23,14 @@ proc getHypothesis (t: prediction): string = let (h, _) = t return h +## Utils +## Find index (or -1) +proc findIndex(xs: seq[string], y: string): int = + for i, x in xs: + if x == y: + return i + return -1 + ## Get sequences let file_path = "../data/one_to_three" ## let file_path = "../data/stripped" @@ -71,12 +69,12 @@ proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[ return continuations ## Pretty print sequences - # var start = @["1", "2", "3", "4", "5"] # var continuations = getSequencesWithStart(seqs, start) # print continuations proc predictContinuation(seqs: seq[seq[string]], observations: seq[string]): seq[prediction] = + let continuations = getSequencesWithStart(seqs, observations) let l = observations.len var nexts: seq[string] @@ -121,25 +119,63 @@ proc jitBayesLoop( initial_num_hypotheses: int, num_hypotheses_step: int, ) = - let l = observations.len + print "## Prediction with limited number of hypotheses (~JIT-Bayes)" + if n_observations_seen < 1: + echo "in jitBayesLoop function, n_observations_seen must be 1 or greater" + return + + var num_hypotheses = initial_num_hypotheses + var hypotheses = seqs[0.. getHypothesis(prediction)) + let correct_continuation_index = findIndex(considered_continuations, correct_continuation) + + if correct_continuation_index == -1: + echo "Correct continuation not found in set of hypotheses of size ", num_hypotheses, "/", seqs.len, ". Increasing size of the set of hypotheses." + + var found_concordant_hypothesis = false + var concordant_hypotheses: seq[seq[string]] + + while (not found_concordant_hypothesis) and ( num_hypotheses < seqs.len ): + num_hypotheses = num_hypotheses + num_hypotheses_step + if num_hypotheses > seqs.len: + num_hypotheses = seqs.len + hypotheses = seqs[0.. i) and h[i] == observations[i]) + if concordant_hypotheses.len > 0: + found_concordant_hypothesis = true + + if not found_concordant_hypothesis: + echo "Increased number of hypotheses to ", num_hypotheses, ", but didn't find any hypotheses concordant with observations. Giving up." + return + else: + echo "Increased number of hypotheses to ", num_hypotheses, ", and found ", concordant_hypotheses.len, " concordant hypotheses. Continuing" + + else: + echo "Correct continuation was ", correct_continuation + echo "It was assigned a probability of ", getProbability(predictions[correct_continuation_index]) + + echo "" ## Display outputs -var observations = @["1", "2", "3", "4", "5", "6"] -echo "Initial sequence", observations +echo "" -print "Full prediction with access to all hypotheses (~Solomonoff)" +## var observations = @["1", "2", "3", "4", "5", "6"] +var observations = @["1", "2", "3", "109", "5", "6"] +echo "## Full prediction with access to all hypotheses (~Solomonoff)" +echo "## Initial sequence: ", observations let continuation_probabilities = predictContinuation(seqs, observations) print continuation_probabilities +echo "" -print "Prediction with limited number of hypotheses (~JIT-Bayes)" -jitBayesLoop(seqs, observations, 3, 1_000, 1_000) +jitBayesLoop(seqs, observations, 3, 1_000, 2_000) +echo ""