def old_bootstrap_pd_stats(b, b_s, k, n_samp=int(1e3)): ''' Calculate, by bootstrapping, statistics of preferred directions. Parameters ---------- pd : array_like, shape (3) preferred directions b_s : float standard error of lin regress coefficients k : float modulation depth n_samp : int number of samples to use for bootstrap Returns ------- k_s : array_like standard errors of k, modulation depth, shape (?) kappa : array_like dispersion factors, shape (?) R : array_like length factor of pd distribution ??, shape (?) Notes ----- 1. Angles of preferred direction cones are determined as follows. 2. Regression coefficients (`b`) are recovered from pds and modulation depths. 3. Random populations of regression coefficients are constructed from distributions with the same mean and standard deviation as estimated from the regression procedure, with the same number of samples as the original data. 4. Preferred directions are calculated from these coefficient values (i.e. dividing by modulation depth). ''' warn(DeprecationWarning("Doesn't calculate using covariance matrix. " "Use ___ instead.")) assert (type(n_samp) == int) | (type(n_samp) == np.int_) assert (type(k) == np.float_) or (type(k) == float) assert b.shape == b_s.shape == (3,) #pd = np.asarray(pd) # reconstruct regression coefficients # Now generate n_samp samples from normal populations # (mean: b_k, sd: err_k). Will have shape (n_samp, 3). b_rnd = np.random.standard_normal(size=(n_samp,3)) b_rnd *= b_s b_rnd += b pd = unitvec(b) #io.savemat('bootstrap_b.mat', dict(b_rnd=b_rnd)) ks = norm(b_rnd, axis=1) k_s = np.std(ks, ddof=1) pds_rnd = b_rnd / ks[...,np.newaxis] kappa = ss.estimate_kappa(pds_rnd, mu=pd) R, S = ss.calc_R(pds_rnd) return k_s, kappa, R
def test_estimate_kappa(): mus = [[0., 0., 1.],[0., 1., 0.], [1., 0., 0.]] kappas = [1.,] for mu, kappa in zip(mus, kappas): n_pts = int(1e4) tolerance = 0.1 # acceptable deviation between set kappa (hopefully), # and estimated kappa. # Could really do with a way to define a # set of points with an established kappa. P_i = vmf_rvs(mu, kappa, n_pts) khat = estimate_kappa(P_i, mu=mu) diff = np.abs(khat - kappa) assert(diff < tolerance)
def bootstrap_pd_stats(b, cov, neural_data, model, ndim=3): ''' Parameters ---------- b : ndarray coefficients cov : ndarray covariance matrix neural_data : ndarray counts (GLM) or rates (OLS) model : string specification of fit model Returns ------- pd : ndarray preferred directions ca : ndarray confidence angle of preferred direction kd : ndarray modulation depths kdse : ndarray standard errors of modulation depths ''' # number of independent samples from original data d = 'd' if 'd' in model else 'v' nsamp = np.sum(~np.isnan(neural_data.sum(axis=1))) # compressing along last axis gives number of samples per bin # which is correct, since bootstrapping is done independently # for each bin # bootstrap a distribution of b values # using mean, covariance matrix from GLM (/ OLS). bootb = np.random.multivariate_normal(b, cov, (nsamp,)) bootdict = fit.unpack_many_coefficients(bootb, model, ndim=ndim) if 'X' in model: bootpd = unitvec(bootdict[d], axis=2) # has shape nsamp, nbin, ndim else: bootpd = unitvec(bootdict[d], axis=1) # has shape nsamp, ndim # get mean pd to narrow kappa estimation bdict = fit.unpack_coefficients(b, model, ndim=ndim) if 'X' in model: nbin = bdict[d].shape[0] pd = unitvec(bdict[d], axis=1) pdca = np.zeros((nbin)) for i in xrange(nbin): # estimate kappa k = ss.estimate_kappa(bootpd[:,i], mu=pd[i]) # estimate ca (preferred direction Confidence Angle) pdca[i] = ss.measure_percentile_angle_ex_kappa(k) # calculate the standard error of the bootstrapped PDs kd = norm(bootdict[d], axis=2) kdse = np.std(kd, axis=0, ddof=1) else: nbin = 1 pd = unitvec(bdict[d]) k = ss.estimate_kappa(bootpd, mu=pd) pdca = ss.measure_percentile_angle_ex_kappa(k) bootkd = norm(bootdict[d], axis=1) kd = np.mean(bootkd, axis=0) kdse = np.std(bootkd, axis=0, ddof=1) return pd, pdca, kd, kdse