Created
May 4, 2016 02:56
-
-
Save vznvzn/2d3576b3dbce355de49eee95b7623369 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
def rand1() | |
return rand() * [-1, 1][rand(2)] | |
end | |
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d, (0.5 - d), (0.5 - d).abs, s.length, c, s.size - c | |
end | |
def d2(s) | |
return [0, 0, 0] if (s.length == 0) | |
d(s)[0..2] | |
end | |
def stat(l) | |
return 0, 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 | |
end | |
def weight(n, l) | |
s = n.to_s(2) | |
d1, d2, d3, w, c1, c0 = d(s) | |
m = s.length / 2 | |
da = d2(s[0...m]) | |
db = d2(s[m..-1]) | |
m1 = w ** 2 | |
m2 = w ** 0.5 | |
a1, sd1 = stat(s.split('/0+/').map { |x| x.length }) | |
a0, sd0 = stat(s.split('/1+/').map { |x| x.length }) | |
l2 = [m1, m2, d1, d2, d3, w, c0, c1, a1, sd1, a0, sd0] + da + db | |
t = 0.0 | |
l2.each_with_index { |x, i| t += x * l[i] } | |
raise(l2.join(' ')) if (t.nan?) | |
return t | |
end | |
def seq(z) | |
n = z['n'] | |
l = [n] | |
while (n != 1) | |
l << n | |
n = f1(n) | |
end | |
return l | |
end | |
def f1(n) | |
n = (n * 3 + 1) / 2 while (n % 2 == 1) | |
n /= 2 while (n % 2 == 0) | |
return n | |
end | |
def stat2(l, z) | |
c = 0 | |
m = l[0] | |
mx = j = 0 | |
l2 = [] | |
(1...l.size).each \ | |
{ | |
|i| | |
c += 1 if (l[i] < l[i - 1]) | |
if (l[i] < m) then | |
l2 << i - j | |
m, j = [l[i], i] | |
end | |
mx = [mx, i - j].max | |
} | |
r = c.to_f / (l.size + 1) | |
a, sd = stat(l2) | |
return {'r' => r, 'mx' => mx, 'c' => l2.size, | |
'd' => l2.size.to_f / (l.size + 1), 'a' => a, 'sd' => sd, | |
}.merge(z) | |
end | |
def linear(c) | |
(0..99).each \ | |
{ | |
|i| | |
x = Kernel.eval($l[i]) | |
w = (1..18).map { rand1() } | |
puts(([i, c] + stat2(seq(x).map { |n| weight(n, w) }, x).to_a).flatten.join("\t")) | |
} | |
puts | |
end | |
$l = File.open('db.txt').readlines | |
10.times { |c| $stderr.puts(c); linear(c); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment