small format & refactor
This commit is contained in:
parent
8ec8f1eda9
commit
4228edfb25
BIN
src/jit_bayes
BIN
src/jit_bayes
Binary file not shown.
|
@ -16,7 +16,7 @@ proc getOEIS(): seq[seq[string]] =
|
|||
if i > 3:
|
||||
let seq = split(line, ",")
|
||||
let l = seq.len
|
||||
let nums = seq[1..(l-2)]
|
||||
let nums = seq[1..(l-2)]
|
||||
seqs.add(nums)
|
||||
i = i + 1
|
||||
f.close()
|
||||
|
@ -24,7 +24,7 @@ proc getOEIS(): seq[seq[string]] =
|
|||
var seqs = getOEIS()
|
||||
|
||||
## Sequence helpers
|
||||
proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
||||
proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
||||
if xs.len == 0:
|
||||
return true
|
||||
elif ys.len == 0:
|
||||
|
@ -34,7 +34,7 @@ proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
|||
else:
|
||||
return false
|
||||
|
||||
proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[string]] =
|
||||
proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[string]] =
|
||||
var continuations: seq[seq[string]]
|
||||
for seq in seqs:
|
||||
if startsWithSubsequence(start, seq):
|
||||
|
@ -49,23 +49,23 @@ proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[
|
|||
|
||||
## Find index (or -1)
|
||||
|
||||
proc findIndex(xs: seq[string], y: string): int =
|
||||
proc findIndex(xs: seq[string], y: string): int =
|
||||
for i, x in xs:
|
||||
if x == y:
|
||||
return i
|
||||
return -1
|
||||
|
||||
## Do simple predictions
|
||||
proc compareTuple (x: (string, float), y: (string, float)): int =
|
||||
proc compareTuple (x: (string, float), y: (string, float)): int =
|
||||
let (_, p1) = x
|
||||
let (_, p2) = y
|
||||
if p1 < p2: return 1
|
||||
elif p1 > p2: return -1
|
||||
else: return 0
|
||||
|
||||
proc predictContinuation(seqs: seq[seq[string]], start: seq[string]): seq[(string, float)] =
|
||||
proc predictContinuation(seqs: seq[seq[string]], start: seq[string]): seq[(string, float)] =
|
||||
let continuations = getSequencesWithStart(seqs, start)
|
||||
let l = start.len
|
||||
let l = start.len
|
||||
var nexts: seq[string]
|
||||
var ps: seq[float]
|
||||
for c in continuations:
|
||||
|
@ -78,9 +78,9 @@ proc predictContinuation(seqs: seq[seq[string]], start: seq[string]): seq[(strin
|
|||
ps[i] = ps[i] + 1.0
|
||||
let sum = foldl(ps, a + b, 0.0)
|
||||
ps = ps.map( p => p/sum)
|
||||
var next_and_ps = zip(nexts, ps)
|
||||
var next_and_ps = zip(nexts, ps)
|
||||
# next_and_ps = sort(next_and_ps, compareTuple)
|
||||
sort(next_and_ps, compareTuple)
|
||||
sort(next_and_ps, compareTuple)
|
||||
# ^ sorts in place
|
||||
# also, openArray refers to both arrays and sequences.
|
||||
return next_and_ps
|
||||
|
@ -93,15 +93,19 @@ print continuation_probabilities
|
|||
|
||||
## Predict continuation but without access to all oeis sequences
|
||||
|
||||
proc predictContinuationWithTruncatedHypotheses(seqs: seq[seq[string]], start: seq[string], num_hypotheses: int): seq[(string, float)] =
|
||||
let n = if num_hypotheses < seqs.len: num_hypotheses else: seqs.len
|
||||
proc predictContinuationWithTruncatedHypotheses(seqs: seq[seq[string]], start: seq[string], num_hypotheses: int): seq[(string, float)] =
|
||||
let n = if num_hypotheses < seqs.len: num_hypotheses else: seqs.len
|
||||
let truncated_seqs = seqs[0..<n]
|
||||
return predictContinuation(truncated_seqs, start)
|
||||
|
||||
let l = seqs.len
|
||||
for i in 1..10:
|
||||
let n = (l.float * (i.float/10.0)).int
|
||||
echo "Predictions with ", (100.0 * i.float/10.0).int, "% of the hypotheses"
|
||||
let predictions = predictContinuationWithTruncatedHypotheses(seqs, start, n)
|
||||
print predictions
|
||||
proc showPredictionsWithMoreHypotheses() =
|
||||
let l = seqs.len
|
||||
for i in 1..10:
|
||||
let n = (l.float * (i.float/10.0)).int
|
||||
echo "Predictions with ", (100.0 * i.float/10.0).int, "% of the hypotheses"
|
||||
let predictions = predictContinuationWithTruncatedHypotheses(seqs, start, n)
|
||||
print predictions
|
||||
|
||||
## showPredictionsWithMoreHypotheses()
|
||||
|
||||
proc jitBayesLoop(seqs: seq[seq[string]], observations: seq[string]) =
|
||||
|
|
Loading…
Reference in New Issue
Block a user