step: add a prediction component

This commit is contained in:
NunoSempere 2023-05-23 18:59:54 -04:00
parent 883fb01995
commit 97259baf4b
7 changed files with 38 additions and 56 deletions

View File

@ -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
---

View File

@ -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

Binary file not shown.

View File

@ -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

Binary file not shown.

View 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")

View File