Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 18, 2020 05:21
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/b2dbcd0de6db6e51de523d91e0197601 to your computer and use it in GitHub Desktop.
Save vznvzn/b2dbcd0de6db6e51de523d91e0197601 to your computer and use it in GitHub Desktop.
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 len(ns, p)
l = ns.split(p)
l = [] if (l.nil?)
l.shift if (l[0] == '')
return l.map { |x| x.length }
end
def len01(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def hist(l)
a = {}
l.each { |x| a[x] = a.fetch(x, 0) + 1 }
return a
end
def hdiff(h1, h2)
d = 0
(h1.keys | h2.keys).each { |x| d += (h1.fetch(x, 0) - h2.fetch(x, 0)).abs }
return d
end
def h2(ns)
l1, l0 = len01(ns)
return hist(l0), hist(l1)
end
def hdiff2(h2a, h2b)
return hdiff(h2a[0], h2b[0]) + hdiff(h2a[1], h2b[1])
end
def hdiff1(ns)
h2a = h2(ns)
t = 0
$c.times \
{
ns2 = dense(ns.length, 0.5)
h2b = h2(ns2)
d = hdiff2(h2a, h2b)
t += d
}
return t.to_f / $c
end
def hdiff0(w)
$h = {} if ($h.nil?)
return $h[w] if ($h.member?(w))
$h[w] = hdiff1(dense(w, 0.5))
return $h[w]
end
def hdiff0b(w)
t = 0
$c.times \
{
h2a = h2(dense(w, 0.5))
h2b = h2(dense(w, 0.5))
d = hdiff2(h2a, h2b)
t += d
}
return t.to_f / $c
end
def outa(f, l, a = {}, t = '')
f.puts('$dat << eof')
k = l[0].keys
f.puts(k.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.puts('eof')
f.puts("set colors classic; set key top left opaque; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
k -= ['t'] if (k.member?('t'))
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
opt = a.fetch(x, '')
ct = (opt == '-') ? "(-column('t')):" : "(column('t')):"
f.puts("'$dat' using #{ct}(column('#{x}')) with line lw 2 title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fn = nil, t = '')
outa(f = File.open(fn.nil? ? 'gnuplot.cmd' : fn, 'w'), l, a, t)
f.close
end
def h12(ns, t)
$h = nil
$c = t
h = hdiff1(ns)
h1 = h / hdiff0(ns.length)
h2 = h / hdiff0b(ns.length)
return h1, h2
end
def hadd(a, b)
(a.keys | b.keys).each { |x| a[x] = a.fetch(x, 0) + b.fetch(x, 0) }
end
def hdiv(a, d)
a.keys.each { |x| a[x] = a[x].to_f / d }
end
def hist2(ns)
l1, l0 = len01(ns)
return hist(l0), hist(l1)
end
def hdiff0c(nw, t01)
return hdiff2(hist2(dense(nw, 0.5)), t01)
end
def avg1(l)
return l[0], l.inject { |t, x| t + x }.to_f / l.size
end
def h34(ns, c)
t0 = {}
t1 = {}
c.times \
{
a0, a1 = hist2(dense(ns.length, 0.5))
hadd(t0, a0)
hadd(t1, a1)
}
hdiv(t0, c)
hdiv(t1, c)
h, hb = avg1((1..c).map {hdiff2(hist2(ns), [t0, t1])})
h0, h0b = avg1((1..c).map {hdiff0c(ns.length, [t0, t1])})
h3 = h / h0
h4 = hb / h0b
return h3, h4
end
def test()
ns = dense(200, 0.5)
l = []
(1..100).each \
{
|t|
h1, h2 = h12(ns, t)
h3, h4 = h34(ns, t)
l << {'t' => t, 'h1' => h1, 'h2' => h2, 'h3' => h3, 'h4' => h4}
}
outafn(l, {'h1' => '-', 'h3' => '-'})
end
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment