Created
June 8, 2017 03:35
-
-
Save vznvzn/83a915ef6116c70e7699371b54b753bb 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) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def f1(n) | |
n = (n * 3 + 1) / 2 if (n.odd?) | |
n /= 2 if (n.even?) | |
return n | |
end | |
def dense(w, d) | |
w2 = w - 1 | |
a = (0...w2).to_a | |
s = '0' * w2 | |
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' } | |
return ('1' + s).to_i(2) | |
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 stat2(l, t, n) | |
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)[0..2].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 data(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, 1) | |
l1 = ns.split(/1+/) | |
l1.shift | |
asdm0 = stat2(l1.map { |x| x.length }, nl, 0) | |
return {'n' => n, 'ns' => ns, 'nl' => nl, 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1).merge(asdm0) | |
end | |
def dist() | |
w = 100 | |
d = 0.0 | |
c = 100 | |
l = [] | |
(c + 1).times \ | |
{ | |
|x| | |
d = x.to_f / c | |
n = dense(w, d) | |
x = {0 => data(n), 1 => data(f2(n))} | |
(ARGV[0].nil? ? 1 : ARGV[0].to_i).times { n = f2(n) } | |
x[1]['wr'] = (n.to_s(2).length.to_f / x[0]['nl'].to_f) #** (1.0 / 10) | |
l << x | |
} | |
$stderr.puts("#{l.size} pts") | |
return l | |
end | |
def sum(l) | |
t = 0.0 | |
l.each { |x| t += x } | |
return t | |
end | |
def av(l) | |
return nil if (l.empty?) | |
return sum(l) / l.size | |
end | |
def corr(l, y1) | |
yp = "#{y1}_p" | |
ye = "#{y1}_e" | |
xav = av(l.map { |x| x[y1] }) | |
yav = av(l.map { |x| x[yp] }) | |
tx = ty = txy = e = 0.0 | |
m = nil | |
l.each \ | |
{ | |
|z| | |
x = z[y1] | |
y = z[yp] | |
txy += (x - xav) * (y - yav) | |
tx += (x - xav) ** 2 | |
ty += (y - yav) ** 2 | |
z[ye] = (x - y) | |
e1 = z[ye].abs | |
e += e1 | |
m = [m, e1].compact.max | |
} | |
r = txy / (Math.sqrt(tx) * Math.sqrt(ty)) | |
e /= l.size | |
return {'r' => r, 'e_a' => e, 'e_m' => m} | |
end | |
def dot(x, z) | |
t = z['c'] | |
(z.keys - ['c']).each { |v| t += z[v] * x[v] } | |
return t | |
end | |
def predict(l, vy, z) | |
l.each \ | |
{ | |
|x| | |
x[1]["#{vy}_p"] = dot(x[0], z) | |
} | |
end | |
def fit(l, vx, vy) | |
a = {} | |
vx.each { |v| a[v] = l.map { |x| x[0][v] }.to_vector() } | |
a[vy] = l.map { |x| x[1][vy] }.to_vector() | |
ds = a.to_dataset() | |
r = Statsample::Regression.multiple(ds, vy) | |
# $stderr.puts(r.summary) | |
z = r.coeffs.merge({'c' => r.constant}) | |
predict(l, vy, z) | |
a = corr(l.map { |x| x[1] }, vy) | |
return z.merge!(a) | |
end | |
def fmt(a) | |
a2 = {} | |
a.each \ | |
{ | |
|k, v| | |
a2[k] = v.is_a?(Numeric) ? sprintf("%.3g", v).to_f : v | |
} | |
$stderr.puts(a2.inspect) | |
end | |
def model(v) | |
l = dist() | |
a = {} | |
l2 = (1..l.size).map { {} } | |
(v + ['wr']).each \ | |
{ | |
|x| | |
a[x] = fit(l, v, x) | |
l1 = l.map { |y| y[1][x] } | |
a[x].merge!({'mn' => l1.min, 'mx' => l1.max}) | |
l.size.times \ | |
{ | |
|i| | |
l2[i][x] = (l[i][1]["#{x}_p"] - l[i][1][x]) / a[x]['e_m'] | |
} | |
fmt({'x' => x}.merge(a[x].select { |k, v| ['r', 'e_m', 'e_a', 'mn', 'mx'].member?(k) })) | |
} | |
f = File.open('err.txt', 'w') | |
f.puts(l2[0].keys.join("\t")) | |
l2.each { |x| f.puts(x.values.join("\t")) } | |
f.close | |
a['l'] = l | |
return a | |
end | |
def detect(a, v, n, j) | |
x1 = data(n) | |
i = 0 | |
c = 500 | |
r = [3, 2] | |
x1['nl'] = r[0] | |
loop \ | |
{ | |
x2 = {} | |
(v + ['wr']).each \ | |
{ | |
|vy| | |
x2[vy] = dot(x1, a[vy].select { |k, x| (v + ['c']).member?(k) }) | |
x2[vy] += a['l'][j][1]["#{vy}_e"] if (!j.nil? && (a['x'].nil? || vy == 'wr')) | |
x2[vy] = a[vy]['mn'] if (x2[vy] < a[vy]['mn']) | |
x2[vy] = a[vy]['mx'] if (x2[vy] > a[vy]['mx']) | |
} | |
x2['nl'] = x1['nl'] * x2['wr'] | |
x1 = x2 | |
i += 1 | |
break if (x1['nl'] < r[1] || i == c) | |
} | |
return i == c ? nil : i | |
end | |
def scan(a, v, j, c) | |
l = [] | |
(c + 1).times \ | |
{ | |
|i| | |
x = detect(a, v, dense(100, i.to_f / c), j) | |
l << x | |
} | |
return l | |
end | |
def cat(a, v, j, l1) | |
l2 = scan(a, v, j, 20) | |
l = [] | |
[l1, l2].transpose.each \ | |
{ | |
|l12| | |
if (l12[1].nil?) then | |
c = 'x' | |
elsif (l12[1] < l12[0]) | |
c = '+' | |
elsif (l12[1] > l12[0]) | |
c = '-' | |
end | |
l << c | |
} | |
return l | |
end | |
def count(c, la, lb, p2) | |
if (c.empty?) then | |
c.replace({'c1' => 0, 'c2' => 0, 't' => 0}) | |
end | |
[la, lb].transpose.each \ | |
{ | |
|x| | |
next if (x[0] == '?') | |
p1 = x[1] | |
c['c1'] += 1 if (p1 == x[0]) | |
c['c2'] += 1 if (p2 == x[0]) | |
c['t'] += 1 | |
} | |
end | |
def d3(x) | |
return sprintf('%.3f', x) | |
end | |
def strlist(l) | |
return '[' + l.join(', ') + ']' | |
end | |
def fmt2(c) | |
return strlist([d3(c['c1'].to_f / c['t']), d3(c['c2'].to_f / c['t']), c['t']]) | |
end | |
def catmap(l, a, b) | |
return l.map { |x| x == a ? b : x } | |
end | |
def range(a, v, r, l1) | |
cw = {} | |
cx = {} | |
cy = {} | |
cz = {} | |
ln = nil | |
r.each \ | |
{ | |
|j| | |
a.delete('x') | |
la = cat(a, v, j, l1) | |
a['x'] = true | |
lb = cat(a, v, j, l1) | |
p2 = a['l'][j][1]['wr_e'] < 0 ? '+' : '-' | |
count(cw, la, lb, p2) | |
count(cx, catmap(la, 'x', '?'), lb, p2) | |
count(cy, catmap(la, '-', '?'), lb, p2) | |
count(cz, catmap(la, 'x', '+'), catmap(lb, 'x', '+'), p2) | |
$stderr.puts([j, cw, cx, cy, cz].inspect) | |
} | |
printf("%8s %s\n", r.inspect, strlist([fmt2(cw), fmt2(cx), fmt2(cy), fmt2(cz)])) | |
$stdout.flush | |
end | |
def test(a, v) | |
l1 = scan(a, v, nil, 20) | |
x = 0 | |
c = a['l'].size / 5 | |
(0...(a['l'].size - 1)).step(c) \ | |
{ | |
|x| | |
range(a, v, (x...(x + c)), l1) | |
} | |
end | |
srand(0) | |
v = ['a1', 'a0', 'dh', 'dl', 'sd0', 'sd1', 'mx1'] | |
a = model(v) | |
puts(ARGV[0]) | |
test(a, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment