diff --git a/src/jit_bayes b/src/jit_bayes index 82f7aaa..bc65513 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 b7c42ec..8194519 100644 --- a/src/jit_bayes.nim +++ b/src/jit_bayes.nim @@ -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..