Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created January 25, 2017 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/2560a6faea2e8badd2d647c9623a5e4b to your computer and use it in GitHub Desktop.
Save vznvzn/2560a6faea2e8badd2d647c9623a5e4b to your computer and use it in GitHub Desktop.
require 'statsample'
def f2(n)
n = (n * 3 + 1) / 2 while (n.odd?)
n /= 2 while (n.even?)
return n
end
def adv(x)
n1 = n = x['n']
l = [n]
while (n >= n1 && n != 1)
n = f2(n)
l << n
end
x['l'] = l
x['ls'] = l.size
x['ns'] = x['n'].to_s(2).length
x['h'] = x['ls'].to_f / x['ns']
x['h2'] = (x['h'] * $g).to_i
return x
end
def next2(z)
l = [z]
p = z['p'] + 1
l << adv({'n'=>z['n'] + 2**p, 'p'=>p})
l << z.merge({'p'=>p})
return l
end
def insert(l, x)
l << x
end
def delete(l, j)
z = l.delete_at(j)
return z
end
def sum(l)
t = 0
l.each { |x| t += x }
return t
end
def stat(l)
l = [0] if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return a, sd, l.max.to_f #, l.min.to_f
end
def dist(l)
l2 = []
l.each_with_index \
{
|x, i|
h = x[1]['h2']
l2[h] = [] if (l2[h].nil?)
l2[h] << i
}
l1 = (0...l2.size).sort_by { |i| l2[i].nil? ? 0 : l2[i].size }
return l2, l1
end
def rank(l1, l2)
l1h, l1s = dist(l1)
l2h, l2s = dist(l2)
j = l1s.find { |x| !l2h[x].nil? && (l1h[x].nil? || l1h[x].size < $n) }
j = l2h.size - 1 if (j.nil?)
k = l2s.find { |x| !l2h[x].nil? }
l = (0...l2.size).to_a
l.sort_by! { |x| l2[x][1]['ls'] }
k = l.find { |x| x != j }
return l2h[j][rand(l2h[j].size)], k
end
def opt(c)
l = []
l1 = []
insert(l, next2({'n'=>1, 'p'=>0}))
puts('# ' + Time.now.to_s)
t = Time.now.to_i
c.times \
{
|i|
$stderr.puts([i, sprintf('%.1fm', (Time.now.to_i - t) / 60.0), Time.now.to_s].join("\t")) if (i % 100 == 0)
j, k = rank(l1, l)
if (l.size > 1000) then
z2 = delete(l, [j, k].max)
z1 = delete(l, [j, k].min)
l1 += [z1, z2]
z = j < k ? z1 : z2
else
z = delete(l, j)
l1 += [z]
end
insert(l, next2(z[1]))
insert(l, next2(z[2]))
$stdout.flush
}
puts('# ' + Time.now.to_s)
return l1.map { |x| x[1] }
end
def stat2(l, t)
return Hash[[['a', 'sd', 'mx' #, 'mn'
], stat(l).map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def sample(c)
l = opt(c)
a = {}
h = {}
l.each \
{
|x|
n = x['n']
h2 = x['h2']
h[h2] = h.fetch(h2, {}).merge!({ n => nil })
a[n] = x
}
l2 = []
c1 = 10
h1 = h.select { |k, v| v.size >= c1 }.sort
h1.each_with_index \
{
|kv, n|
k, v = kv
l1 = v.keys.sort
c1.times \
{
|i|
n = l1[(i.to_f / (c1 - 1) * (l1.size - 1)).to_i]
l2 << a[n]
}
}
return l2
end
def data(x)
n = x['n']
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl)
return {'nl' => nl, 'ls' => x['ls'], 'h2' => x['h2'], 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1)
end
def fit(l, y, lx)
l1 = l.map { |x| x.values }.transpose
n = {}
l[0].keys.each_with_index { |x, i| n[x] = i }
a = {}
a['y'] = l1[n[y]].to_vector()
lx.each { |x| a[x] = l1[n[x]].to_vector() }
ds = a.to_dataset()
r = Statsample::Regression.multiple(ds, 'y')
$stderr.puts(r.summary)
return r.coeffs.merge({'c' => r.constant})
end
def err(z, l1, fn)
z = z.dup
c1 = z.shift
l = []
l1.each_with_index \
{
|l2, j|
nl, = l2
t = c1
z.size.times { |i| t += z[i] * l2[i + $c] }
b = fn.call(t, nl)
l << [l2[0...$c], t, b].flatten
}
return l
end
def predict(z, l1)
l1.each \
{
|x|
t = z['c']
(z.keys - ['c']).each { |k| t += z[k] * x[k] }
x['h2_p'] = t
x['ls_p'] = t / $g * x['nl']
}
end
def out(fn, l)
f = File.open("#{fn}.txt", 'w')
f.puts(l[0].keys.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
end
$n = 20
$g = 50
l2 = sample(2000).map { |x| data(x) }
$stderr.puts("#{l2.size} pts")
out('out', l2)
$c = 3
z = fit(l2, 'h2', l2[0].keys - ['nl', 'ls', 'h2'])
predict(z, l2)
out('out1', l2.sort_by { |x| x['h2'] })
out('out2', l2.sort_by { |x| x['ls'] })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment