def test_sampling_bias1(self):
    from spikecount.shannon import entropy1, bootstrap
    from numpy import pi, e, log2
    from numpy import array, polyfit

    print 
    print "Sampling bias 1d"
    x = generate_gaussian(sigma = 10, N=30000)
    N = x.size
    N = array([N/2**i for i in xrange(5)])
    print N, '\n'
    ss = []
    for i, n in enumerate(N):
      y = x[:n*(2**i)].reshape((2**i, n))
      smean = array([entropy1(z) for z in y]).mean()
      ss.append(smean)
      print n, smean, smean - log2(x.std()), log2(2*pi*e)/2
      
#    pl.plot(1.0/N, ss)
#    pl.show()
    
    sr = polyfit(1.0/N, ss, 1)
    s0 = sr[-1]
    print s0, s0-log2(x.std()), log2(2*pi*e)/2
    
    scompare = bootstrap(entropy1, x, debug=False)['predict']
    print scompare, scompare - log2(x.std()), log2(2*pi*e)/2
 def test_entropy1(self):
   from spikecount.shannon import entropy1
   from numpy import pi, e, log2
   
   print
   print "Test 1d entroy."
   x = generate_gaussian(sigma = 5)
   print entropy1(x), entropy1(x)-log2(x.std()), log2(2*pi*e)/2
   
   x = generate_gaussian(sigma = 30)
   print entropy1(x), entropy1(x)-log2(x.std()), log2(2*pi*e)/2
   
   x = generate_gaussian(sigma = 75)
   print entropy1(x), entropy1(x)-log2(x.std()), log2(2*pi*e)/2
 def test_mix_samples(self):
   from spikecount.shannon import mix_samples, entropy2, entropy1, log2, bootstrap
   from numpy import hstack
   print "\nTest mixing samples."
   c = 0.3
   x1, y1 = generate_correlated_gaussian(sigma=5, N=100000, c = c)
   x2, y2 = generate_correlated_gaussian(sigma=10, N=100000, c = c)
   print "Well mixed samples"
   z1 = mix_samples([x1,x2])
   z2 = mix_samples([y1,y2])
   print entropy1(z1) + entropy1(z2) - entropy2(z1, z2), -log2(1-c**2)/2
   print "Datasets pasted side by side"
   z1 = hstack((x1, x2))
   z2 = hstack((y1, y2))
   print entropy1(z1) + entropy1(z2) - entropy2(z1, z2), -log2(1-c**2)/2    
   
   print "Now testing shifting and mixing samples"
   x1, y1 = generate_correlated_gaussian(sigma=4, N=100000, c = c)
   x2, y2 = generate_correlated_gaussian(sigma=4, N=100000, c = c)
   z1 = mix_samples((x1,x2+1000))
   z2 = mix_samples((y1,y2+1000))
   print bootstrap(entropy1, z1)['predict'] + bootstrap(entropy1, z2)['predict'] - bootstrap(entropy2, z1, z2)['predict'], -log2(1-c**2)/2    
   print bootstrap(entropy1, x2)['predict'] + bootstrap(entropy1, y2)['predict'] - bootstrap(entropy2, x2, y2)['predict'], -log2(1-c**2)/2