def main():
    
    np.seterr(all='warn')
    
    K = 50
    ks = np.linspace(0, K - 1, K)
    f_mean = lambda k: np.exp(-k * 0.2)
    noise_eff = 0.05
    
    true_stats = np.ndarray(K, fit_dtype)
    true_stats['mean'] = f_mean(ks) 
    true_stats['lower'] = true_stats['mean'] - noise_eff
    true_stats['upper'] = true_stats['mean'] + noise_eff 
    
    simulate = lambda: simulate_distribution(true_stats)
    
    # two realizations
    X1 = simulate()
    X1_order = scale_score(X1)
    X2 = simulate()
    X2_order = scale_score(X2)
    
    # Now estimate upper and lower bounds by simulations
    observed_stats = estimate_bounds_by_simulations(simulate, N=100)
    order_est = estimate_order_by_resimulating(observed_stats, N=1000)


    print('Simulating distribution...')
    orders = compute_order_samples(simulate, N=10000)
    print('..done')

    r = Report('orderdemo')
    f = r.figure('figures', cols=3)
    
    z = 0.6
    figparams = dict(figsize=(4 * z, 3 * z), mime=MIME_PDF)
    
    from matplotlib import rc
    rc('font', **{'family':'serif',
                  'serif':['Times', 'Times New Roman', 'Palatino'],
                   'size': 7.0})
    rc('text', usetex=True)

    with f.plot('true', caption='True distribution', **figparams) as pl:
        plot_rate_bars(pl, ks, true_stats, 'b', label='$p(X^k)$')
        #pl.plot(ks, true_stats['mean'], 'b.')
        pl.xlabel('$k$')
        pl.ylabel('$X^k$')
        pl.legend()
        #pl.title('Distribution of the random variables $X^k$')

    with f.plot('meanorder', caption='True distribution', **figparams) as pl:
        pl.plot(ks, scale_score(true_stats['mean']), 'r.',
                label='$\\mathsf{order}(\\mathsf{mean}(X^k))$')
        pl.xlabel('$k$')
        pl.ylabel('$\\mathsf{order}(\\mathsf{mean}(X^k))$')
        pl.legend()
        #pl.title('Order of the means of $X^k$')

    with f.plot('realizations', caption='Realizations', **figparams) as pl:
        pl.plot(X1, 'bx', label='$x_1$')
        pl.plot(X2, 'g.', label='$x_2$')
        pl.xlabel('$k$')
        pl.ylabel('$x_i$')
        pl.legend()
        #pl.title('Two realizations of the process')

    with f.plot('orders', caption='orders', **figparams) as pl:
        pl.plot(X1_order, 'rx', label='$\\mathsf{order}(x_1)$')
        pl.plot(X2_order, 'm.', label='$\\mathsf{order}(x_2)$')
        pl.xlabel('$k$')
        pl.ylabel('$\\mathsf{order}(x_i)$')
        pl.legend()
        #pl.title('Computed order from realizations of the process')

    with f.plot('estorder', caption='Estimated order', **figparams) as pl:
        plot_rate_bars(pl, ks, order_est, 'r', label='$p(\\mathsf{order}(X^k))$')
        #pl.plot(ks, order_est['mean'], 'r.')
        pl.xlabel('$k$')
        pl.ylabel('$\\mathsf{order}(X)$')
        pl.legend()
        #pl.title('Estimated confidence bounds order from resimulating')

    with f.plot('observed', caption='Observed statistics', **figparams) as pl:
        plot_rate_bars(pl, ks, observed_stats, 'b')
        pl.xlabel('$k$')
        pl.ylabel('$X^k$')
        
    def plot_histogram(k, pylab, color, label):
        dist = orders[k, :]
        values, _ = np.histogram(dist, bins=range(K + 1), normed=True)
        left = ks 
        pylab.bar(left, values, width=1, color=color, label=label)
        
    with f.plot('ordersex', caption='Some examples', **figparams) as pl:
        plot_histogram(10, pl, 'r', '$\\mathsf{order}(X^{10})$')
        plot_histogram(15, pl, 'g', '$\\mathsf{order}(X^{15})$')
        plot_histogram(40, pl, 'b', '$\\mathsf{order}(X^{40})$')
        pl.xlabel('$\\mathsf{order}$')
        pl.ylabel('probability')
        pl.legend(loc='upper left')
        
    rd = 'order_estimation_test2' 
    filename = os.path.join(rd, 'index.html')
    print('Writing to %r.' % filename)
    r.to_html(filename, resources_dir=rd)
def main():
    
    np.seterr(all='warn')
    
    n = 100
    z = np.linspace(-1, 1, n)
    z_order = np.array(range(n))
    alpha = 0.1
    base = 0.3
    noise_eff = 0.05
    noise_est = noise_eff
    f_L = lambda z: np.exp(-np.abs(+1 - np.maximum(z, 0)) / alpha) + base
    f_R = lambda z: np.exp(-np.abs(-1 - np.minimum(z, 0)) / alpha) + base
    
    rate_L0 = f_L(z) 
    rate_R0 = f_R(z) 
    
    simulate_L = lambda: f_L(z) + np.random.uniform(-1, 1, n) * noise_eff 
    simulate_R = lambda: f_R(z) + np.random.uniform(-1, 1, n) * noise_eff
    
    rate_L = simulate_L()
    rate_R = simulate_R()
    

    T = 100
    ord1 = np.zeros((n, T))
    for k in range(T):
        ord1[:, k] = scale_score(simulate_L())
    order_L_sim = np.ndarray(n, fit_dtype) 
    for i in range(n):
        order_L_sim[i]['mean'] = np.mean(ord1[i, :])
        l, u = np.percentile(ord1[i, :], [5, 95])
        order_L_sim[i]['upper'] = u
        order_L_sim[i]['lower'] = l
    
    
    rate_L_est = np.ndarray(n, fit_dtype) 
    rate_L_est['upper'] = rate_L + noise_est
    rate_L_est['lower'] = rate_L - noise_est
    rate_R_est = np.ndarray(n, fit_dtype) 
    rate_R_est['upper'] = rate_R + noise_est
    rate_R_est['lower'] = rate_R - noise_est

    # estimate according to naive procedure
    z_naive = estimate_stimulus_naive(rate_L, rate_R)
    
    res = estimate_stimulus(rate_L_est, rate_R_est)
    L_order = res.L_order
    R_order = res.R_order

        
    scale_rate = max(rate_L.max(), rate_R.max()) * 1.2
    cL = 'r'
    cR = 'b'
    
    r = Report()
    f = r.figure(cols=3)
    with r.plot('noiseless') as pylab:
        pylab.plot(z, rate_L0, '%s-' % cL)
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='noiseless rates')
    
    with r.plot('observed_rates') as pylab:
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.plot(z, rate_L0, '%s-' % cL)
        plot_rate_bars(pylab, z, rate_L_est, '%s' % cL)
        plot_rate_bars(pylab, z, rate_R_est, '%s' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='true_rates')
  
    with r.plot('M') as pylab:
        pylab.plot(z, rate_L0, '%s-' % cL)
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
        
    with r.plot('z_naive') as pylab:
        pylab.plot(z_naive, rate_L, '%s.' % cL)
        pylab.plot(z_naive, rate_R, '%s.' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='Stimulus estimated in naive way.')
    
    
    with r.plot('simulated_order_stats') as pylab:
        pylab.plot([0, 0], [n, n], 'k-')
        pylab.plot([0, n], [n, 0], 'k-')
        pylab.plot(z_order, order_L_sim['mean'], '%s.' % cL)
        plot_rate_bars(pylab, z_order, order_L_sim, '%s' % cL)
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='Orders as found by simulation')
  
    
    with r.plot('estimated_order') as pylab:
        pylab.plot(z, L_order['mean'], '%s.' % cL)
        pylab.plot(z, R_order['mean'], '%s.' % cR)
        pylab.axis((-1, 1, -n / 2, n * 3 / 2))
    r.last().add_to(f, caption='estimated_order')
  
    with r.plot('estimated_order_order') as pylab:
        
        pylab.plot([0, 0], [n, n], 'k-')
        plot_rate_bars(pylab, z_order, L_order, '%s' % cL)
        plot_rate_bars(pylab, z_order, R_order, '%s' % cR)
        pylab.plot([0, 0], [n, n], 'k-')
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='estimated_order_order')
  
    with r.plot('estimated_order_bar') as pylab:
        pylab.plot(z, L_order['mean'], '%s.' % cL)
        pylab.plot(z, R_order['mean'], '%s.' % cR)
        plot_rate_bars(pylab, z, L_order, '%s' % cL)
        plot_rate_bars(pylab, z, R_order, '%s' % cR)     
        pylab.axis((-1, 1, -n / 2, n * 3 / 2))
    r.last().add_to(f, caption='estimated_order_bar') 

    with r.plot('estimated_order_both') as pylab:
        pylab.plot([0, 0], [n, n], 'g-')
        pylab.plot(z_order, res.order['mean'], 'k.')
        plot_rate_bars(pylab, z_order, res.order, 'k')
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='estimated_order_order') 

    with r.plot('z_better') as pylab:
        pylab.plot(res.z['mean'], rate_L, '%s.' % cL)
        pylab.plot(res.z['mean'], rate_R, '%s.' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='Stimulus estimated in a better way.')
    
  
    filename = 'order_estimation.html' 
    print('Writing to %r.' % filename)
    r.to_html(filename)
def compute_order(samples):
    order = np.zeros(samples.shape)
    for t in range(samples.shape[1]):
        order[:, t] = scale_score(samples[:, t])
    return order