Example #1
0
def test_bootstrap_pd_stats():
    '''
    Notes
    -----
    create a population of 3d data points
    with a known mean and certain spread

    take a sample of n points from that population
    get the mean and std error of that sample

    use bootstrap_pd_stats to get kappa
    95% of time, real mean pd should lie within kappa of sample means
    '''
    n_trials = 100
    pop_size = 1e6
    smp_size = 40
    alpha = 0.05 # used to be 0.95 here
    pds = [[0., 0., 1.], [0.,1.,0.], [1.,0.,0.]]
    sss = [[0.1, 0.1, 0.1], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5]]

    for pd in pds:
        for ss in sss:
            print "--------------------"
            print "pd: ", pd
            print "ss: ", ss
            failures = 0
            for i in range(n_trials):
                pop = np.random.standard_normal(size=(pop_size,3)) * ss + pd
                smp_inds = np.random.randint(pop_size, size=smp_size)
                smp = pop[smp_inds]
                smp_mean = np.mean(smp, axis=0)
                smp_k = np.sqrt(np.sum(smp_mean**2))
                smp_pd = smp_mean / smp_k
                smp_stderr = np.std(smp, axis=0, ddof=1) / np.sqrt(smp_size)
                
                # estimate k_s, kappa using bootstrap_pd_stats
                k_s, kappa, R = cpd.bootstrap_pd_stats( \
                    smp_pd, smp_stderr, smp_k, n_samp=smp_size)
                interangle = np.arccos(np.dot(pd, smp_pd))
                theta = sphere_stat.measure_percentile_angle_ex_kappa( \
                    kappa)
                print "%2d- interangle: %6.4f theta %6.4f" % \
                    (i, interangle, theta),
                if interangle > theta:
                    failures += 1
                    print '*'
                else:
                    print '-'
            print "failures: %.2f" % (failures / float(n_trials))
            assert failures < (alpha * n_trials)