step: add a prediction component
This commit is contained in:
parent
883fb01995
commit
97259baf4b
3
index.md
3
index.md
|
@ -20,6 +20,9 @@ Where stripped.gz can be found at <https://oeis.org/wiki/JSON_Format,_Compressed
|
|||
|
||||
- [ ] Exploration of OEIS data
|
||||
- [ ] Subdivide subsequent tasks into steps
|
||||
- [ ] JIT Bayesianism
|
||||
- [ ] Infrabayesianism x1: Predicting interleaved sequences
|
||||
- [ ] Infrabayesianism x2: Deterministic game of producing a fixed deterministic prediction, and then the adversary picking whatever minimizes your loss
|
||||
|
||||
---
|
||||
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
import strutils
|
||||
import sequtils
|
||||
import print
|
||||
|
||||
## Get sequences
|
||||
let file_path = "./data/stripped"
|
||||
proc getOEIS(): seq[seq[string]] =
|
||||
let f = open(file_path)
|
||||
var i = 0
|
||||
var line : string
|
||||
var seqs: seq[seq[string]]
|
||||
while f.read_line(line):
|
||||
if i > 3:
|
||||
let seq = split(line, ",")
|
||||
let l = seq.len
|
||||
let nums = seq[1..(l-2)]
|
||||
seqs.add(nums)
|
||||
i = i + 1
|
||||
f.close()
|
||||
return seqs
|
||||
var seqs = getOEIS()
|
||||
|
||||
## Sequence helpers
|
||||
proc startsWithSubsequence(xs: seq[string], ys: seq[string]): bool =
|
||||
if xs.len == 0:
|
||||
return true
|
||||
elif ys.len == 0:
|
||||
return false
|
||||
elif xs[0] == ys[0]:
|
||||
return startsWithSubsequence(xs[1..<xs.len], ys[1..<ys.len])
|
||||
else:
|
||||
return false
|
||||
|
||||
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):
|
||||
continuations.add(seq)
|
||||
return continuations
|
||||
|
||||
proc predictContinuation(seqs: seq[seq[string]], start: seq[string]): seq[seq[string]] =
|
||||
let continuations = getSequencesWithStart(seqs, start)
|
||||
|
||||
|
||||
## Pretty print sequences
|
||||
var start = @["1", "2", "3"]
|
||||
var continuations = getSequencesWithStart(seqs, start)
|
||||
print continuations
|
||||
|
||||
|
BIN
src/jit_bayes
BIN
src/jit_bayes
Binary file not shown.
|
@ -45,13 +45,34 @@ proc getSequencesWithStart(seqs: seq[seq[string]], start: seq[string]): seq[seq[
|
|||
# var continuations = getSequencesWithStart(seqs, start)
|
||||
# print continuations
|
||||
|
||||
## 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 predictContinuation(seqs: seq[seq[string]], start: seq[string]): seq[string] =
|
||||
proc predictContinuation(seqs: seq[seq[string]], start: seq[string]): int =
|
||||
let continuations = getSequencesWithStart(seqs, start)
|
||||
let l = start.len
|
||||
let next_items = continuations.map(c => c[l])
|
||||
return next_items
|
||||
var nexts: seq[string]
|
||||
var ps: seq[float]
|
||||
for c in continuations:
|
||||
let next = c[l]
|
||||
let i = findIndex(nexts, next)
|
||||
if i == -1:
|
||||
nexts.add(next)
|
||||
ps.add(1.0)
|
||||
else:
|
||||
ps[i] = ps[i] + 1.0
|
||||
let sum = foldl(ps, a + b, 0.0)
|
||||
echo nexts
|
||||
echo ps
|
||||
echo ps.map(p=> p/sum)
|
||||
return 1
|
||||
# to do: wrangle this in some kind of sequence of tuples, e.g., using some zip type of thing.
|
||||
|
||||
var start = @["1", "2", "3", "4", "5"]
|
||||
let sample_continuations = predictContinuation(seqs, start)
|
||||
print(sample_continuations)
|
||||
var start = @["1", "2", "3", "4", "5", "6"]
|
||||
echo predictContinuation(seqs, start)
|
||||
|
|
BIN
src/scratchpad/findIndex
Executable file
BIN
src/scratchpad/findIndex
Executable file
Binary file not shown.
8
src/scratchpad/findIndex.nim
Normal file
8
src/scratchpad/findIndex.nim
Normal file
|
@ -0,0 +1,8 @@
|
|||
proc findIndex(xs: seq[string], y: string): int =
|
||||
for i, x in xs:
|
||||
if x == y:
|
||||
return i
|
||||
return -1
|
||||
|
||||
let xs = @["a", "b", "c"]
|
||||
echo findIndex(xs, "a")
|
0
src/scratchpad/seq_tuples.nim
Normal file
0
src/scratchpad/seq_tuples.nim
Normal file
Loading…
Reference in New Issue
Block a user