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:
|
if i > 3:
|
||||||
let seq = split(line, ",")
|
let seq = split(line, ",")
|
||||||
let l = seq.len
|
let l = seq.len
|
||||||
let nums = seq[1..(l-2)]
|
let nums = seq[1..(l-2)]
|
||||||
seqs.add(nums)
|
seqs.add(nums)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -24,7 +24,7 @@ proc getOEIS(): seq[seq[string]] =
|
||||||
var seqs = getOEIS()
|
var seqs = getOEIS()
|
||||||
|
|
||||||
## Sequence helpers
|
## Sequence helpers
|
||||||
proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
||||||
if xs.len == 0:
|
if xs.len == 0:
|
||||||
return true
|
return true
|
||||||
elif ys.len == 0:
|
elif ys.len == 0:
|
||||||
|
@ -34,7 +34,7 @@ proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
||||||
else:
|
else:
|
||||||
return false
|
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]]
|
var continuations: seq[seq[string]]
|
||||||
for seq in seqs:
|
for seq in seqs:
|
||||||
if startsWithSubsequence(start, seq):
|
if startsWithSubsequence(start, seq):
|
||||||
|
@ -49,23 +49,23 @@ proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[
|
||||||
|
|
||||||
## Find index (or -1)
|
## 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:
|
for i, x in xs:
|
||||||
if x == y:
|
if x == y:
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
## Do simple predictions
|
## 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 (_, p1) = x
|
||||||
let (_, p2) = y
|
let (_, p2) = y
|
||||||
if p1 < p2: return 1
|
if p1 < p2: return 1
|
||||||
elif p1 > p2: return -1
|
elif p1 > p2: return -1
|
||||||
else: return 0
|
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 continuations = getSequencesWithStart(seqs, start)
|
||||||
let l = start.len
|
let l = start.len
|
||||||
var nexts: seq[string]
|
var nexts: seq[string]
|
||||||
var ps: seq[float]
|
var ps: seq[float]
|
||||||
for c in continuations:
|
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
|
ps[i] = ps[i] + 1.0
|
||||||
let sum = foldl(ps, a + b, 0.0)
|
let sum = foldl(ps, a + b, 0.0)
|
||||||
ps = ps.map( p => p/sum)
|
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)
|
# next_and_ps = sort(next_and_ps, compareTuple)
|
||||||
sort(next_and_ps, compareTuple)
|
sort(next_and_ps, compareTuple)
|
||||||
# ^ sorts in place
|
# ^ sorts in place
|
||||||
# also, openArray refers to both arrays and sequences.
|
# also, openArray refers to both arrays and sequences.
|
||||||
return next_and_ps
|
return next_and_ps
|
||||||
|
@ -93,15 +93,19 @@ print continuation_probabilities
|
||||||
|
|
||||||
## Predict continuation but without access to all oeis sequences
|
## Predict continuation but without access to all oeis sequences
|
||||||
|
|
||||||
proc predictContinuationWithTruncatedHypotheses(seqs: seq[seq[string]], start: seq[string], num_hypotheses: int): seq[(string, float)] =
|
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 n = if num_hypotheses < seqs.len: num_hypotheses else: seqs.len
|
||||||
let truncated_seqs = seqs[0..<n]
|
let truncated_seqs = seqs[0..<n]
|
||||||
return predictContinuation(truncated_seqs, start)
|
return predictContinuation(truncated_seqs, start)
|
||||||
|
|
||||||
let l = seqs.len
|
proc showPredictionsWithMoreHypotheses() =
|
||||||
for i in 1..10:
|
let l = seqs.len
|
||||||
let n = (l.float * (i.float/10.0)).int
|
for i in 1..10:
|
||||||
echo "Predictions with ", (100.0 * i.float/10.0).int, "% of the hypotheses"
|
let n = (l.float * (i.float/10.0)).int
|
||||||
let predictions = predictContinuationWithTruncatedHypotheses(seqs, start, n)
|
echo "Predictions with ", (100.0 * i.float/10.0).int, "% of the hypotheses"
|
||||||
print predictions
|
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