Created
December 24, 2016 03:17
-
-
Save vznvzn/0386c5a96cbc1c187a0c3f87dd685bfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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 stat(l).map { |x| x / t } | |
end | |
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def fit(l1, c) | |
l1 = l1.transpose | |
a = {} | |
a['y'] = l1[c].to_vector() | |
($c...l1.size).each \ | |
{ | |
|i| | |
a["d#{i}"] = l1[i].to_vector() | |
} | |
ds = a.to_dataset() | |
r = Statsample::Regression.multiple(ds, 'y') | |
# $stderr.puts(r.summary) | |
return [r.constant] + r.coeffs.values_at(*(a.keys - ['y'])) | |
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 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, x['ls'], x['h2'], d(ns), d(nsh), d(nsl), asdm1].flatten | |
end | |
def out(fn, l) | |
f = File.open("#{fn}.txt", 'w') | |
l.each { |x| f.puts(x.join("\t")) } | |
f.close | |
end | |
def out2(fn, l) | |
out("#{fn}e", l) | |
out("#{fn}e2", l.sort_by { |x| x[1] }) | |
end | |
def samplefit(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 | |
} | |
# h.sort.each { |k, v| p([k, v.size]) }; exit; | |
l2 = [] | |
l3 = [] | |
c1 = 10 | |
h1 = h.select { |k, v| v.size >= c1 }.sort | |
h1.each_with_index \ | |
{ | |
|kv, n| | |
k, v = kv | |
l1 = v.keys.sort | |
l0 = [] | |
c1.times \ | |
{ | |
|i| | |
n = l1[(i.to_f / (c1 - 1) * (l1.size - 1)).to_i] | |
l0 << n | |
l2 << data(a[n]) | |
l3 << a[n] | |
} | |
} | |
$stderr.puts("#{l2.size} pts") | |
$c = 3 | |
z = fit(l2, $c - 1) | |
out('out', l2) | |
out2('out', err(z, l2, lambda { |x, nl| x / $g * nl })) | |
return z, l3 | |
end | |
def adv2(l) | |
l2 = [] | |
l.each_with_index \ | |
{ | |
|n, i| | |
x = {'n' => n} | |
l2 << data(x) | |
} | |
return l2 | |
end | |
def avg(l) | |
return sum(l) / l.size | |
end | |
def improve(z, l) | |
c = 5 | |
l = l.select { |x| x['ls'] >= c } | |
$stderr.puts("#{l.size} pts2") | |
l4 = [] | |
l.each \ | |
{ | |
|x| | |
l2 = adv2(x['l'][0...c]) | |
l1 = err(z, l2, lambda { |x, nl| x / $g * nl }) | |
l3 = [] | |
c.times \ | |
{ | |
|i| | |
nl = l1[i][0] | |
l1[i][3] += i.to_f / nl | |
l1[i][4] += i | |
} | |
(3..4).each \ | |
{ | |
|j| | |
l3 << avg(l1.map { |x| x[j]}) | |
} | |
nl = x['n'].to_s(2).length | |
l4 << [nl, x['ls'], x['h2']] + l3 | |
} | |
out2('out2', l4) | |
end | |
def compare() | |
$n = 20 | |
$g = 50 | |
z, l = samplefit(3000) | |
# $stderr.puts(z.inspect) | |
improve(z, l) | |
end | |
l2 = compare() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment