Created
May 17, 2019 01:13
-
-
Save vznvzn/1ee885669c6307c7d3541a133b9dc354 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 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) | |
end | |
def norm(l) | |
c = 100 | |
l2 = [] | |
c.times \ | |
{ | |
|x| | |
i = (x.to_f / (c - 1) * (l.size - 1)).to_i | |
l2 << l[i] | |
} | |
return l2 | |
end | |
def avg(l) | |
l.inject { |t, x| t + x }.to_f / l.size | |
end | |
def coef(x, y) | |
x1 = x2 = y1 = y2 = xy = 0.0 | |
n = x.size | |
n.times \ | |
{ | |
|i| | |
x1 += x[i] | |
y1 += y[i] | |
xy += x[i] * y[i] | |
x2 += x[i] ** 2 | |
y2 += y[i] ** 2 | |
} | |
d = n * x2 - x1 ** 2 | |
b = (y1 * x2 - x1 * xy) / d | |
m = (n * xy - x1 * y1) / d | |
b = 1 if (b.nan?) | |
m = 1 if (m.nan?) | |
return b, m | |
end | |
def fake(n) | |
l = [] | |
while (n > 1) | |
n = rand(2) == 0 ? n * 3 / 2 : n / 2 | |
l << n | |
end | |
return l | |
end | |
def spread(w) | |
l2 = [] | |
200.times \ | |
{ | |
n = dense(w, 0.5).to_i(2) | |
l = fake(n).map { |x| Math.log(x) } | |
b, m = coef((0...l.size).to_a, l) | |
l1 = [] | |
l.size.times \ | |
{ | |
|x| | |
y1 = l[x] | |
y2 = x * m + b | |
l1 << (y1 - y2).abs | |
} | |
l2 << norm(l1) | |
} | |
l1 = l2.transpose.map { |x| avg(x) } | |
return l1 | |
end | |
(1..10).each \ | |
{ | |
|x| | |
w = x * 100 | |
l1 = spread(w) | |
mx = l1.max | |
l1.size.times { |x| puts([x, w, l1[x], l1[x] / mx].join("\t")) } | |
puts | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment