step: develop jit-bayes loop.
This commit is contained in:
parent
c2e2836df5
commit
d70eac48f0
BIN
src/jit_bayes
BIN
src/jit_bayes
Binary file not shown.
|
@ -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 hypotheses = seqs[0..initial_num_hypotheses]
|
||||
for i in n_observations_seen..<l:
|
||||
var num_hypotheses = initial_num_hypotheses
|
||||
var hypotheses = seqs[0..<num_hypotheses]
|
||||
|
||||
let l = observations.len
|
||||
for i in n_observations_seen..<l: # to do: make so that this can start at 0.
|
||||
let predictions = predictContinuation(hypotheses, observations[0..<i])
|
||||
echo "Prediction after seeing ", i, " observations: ", observations[0..<i]
|
||||
echo "### Prediction after seeing ", i, " observations: ", observations[0..<i]
|
||||
print predictions
|
||||
|
||||
## let possible_continuations = predictions.map()
|
||||
let correct_continuation = observations[i]
|
||||
let considered_continuations = predictions.map(prediction => 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..<num_hypotheses]
|
||||
concordant_hypotheses = filter(hypotheses, proc(h: seq[string]): bool = (h.len > 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 ""
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user