def descStats(data): """ Compute descriptive statistics of data """ dataList = list(data) logDataList = list(N.log10(dataList)) desc = dict() if len(dataList) == 0: desc['mean'] = 0 desc['median'] = 0 desc['logMean'] = 0 desc['logMedian'] = 0 elif len(dataList) < 2: desc['mean'] = dataList[0] desc['median'] = dataList[0] desc['logMean'] = logDataList[0] desc['logMedian'] = logDataList[0] else: desc['mean'] = mean(dataList) desc['median'] = median(dataList) desc['logMean'] = mean(logDataList) desc['logMedian'] = median(logDataList) if len(dataList) < 3: desc['stdev'] = 0 desc['sterr'] = 0 desc['logStdev'] = 0 desc['logSterr'] = 0 else: desc['stdev'] = std(dataList) desc['sterr'] = stderr(dataList) desc['logStdev'] = std(logDataList) desc['logSterr'] = stderr(logDataList) return desc
def test_2d(self): a = [[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [8.0, 12.0, 7.0]] b1 = array( (3.7859388972001824, 5.2915026221291814, 2.0816659994661335)) b2 = array((1.0, 2.0, 2.64575131106)) assert_array_almost_equal(stats.std(a), b1, 11) assert_array_almost_equal(stats.std(a, axis=0), b1, 11) assert_array_almost_equal(stats.std(a, axis=1), b2, 11)
def test_2d(self): a = [[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [8.0, 12.0, 7.0]] b1 = array((3.7859388972001824, 5.2915026221291814, 2.0816659994661335)) b2 = array((1.0,2.0,2.64575131106)) assert_array_almost_equal(stats.std(a),b1,11) assert_array_almost_equal(stats.std(a,axis=0),b1,11) assert_array_almost_equal(stats.std(a,axis=1),b2,11)
def errorfill(x, y, yerr = None, ax = None, color = None, err_type = 'sem', **kwargs): ax, fig = axis_check(ax) if color is None: color = ax._get_lines.color_cycle.next() if len(y.shape)==1: y_mean = y else: y_mean = y.mean(0) if yerr is None: if err_type == 'sem': yerr = 0.5*st.sem(y, 0) elif err_type == 'std': yerr = 0.5*st.std(y, 0) x_hi = y_mean + yerr x_lo = y_mean - yerr l = ax.plot(x, y_mean, color = color, **kwargs) l_up = ax.plot(x, y_mean+yerr, color = color, alpha = 0.2) l_lo = ax.plot(x, y_mean-yerr, color = color, alpha = 0.2) ax.fill_between(x, x_hi, x_lo, alpha = 0.2, color = color) return ax
def gStats(self, missingValue=0.0): """dict of {geneID: (min,max,mean,median,std,stderr, Shapiro-Wilk(w,p),normaltest_chisq (D'Agostino and Pearson),...} """ import scipy as S import scipy.stats as SS rv = {} for k, v in self.items(): # print k,v va = S.array(self.gValues(k, missingValue)) try: normaltest = SS.normaltest(va) except: normaltest = None try: shapiro = SS.shapiro(va) except: shapiro = None try: rv[k] = (va.min(), va.max(), va.mean(), SS.median(va), SS.std(va), SS.stderr(va), normaltest, shapiro) except: print k, va raise return rv
def _remove_outliers(data): return data if len(data) < 2: return data else: lmean = mean(data) limstdev = 3 * std(data) return [item for item in data if abs(item - lmean) < limstdev ]
def reindex(self): period = self.series[-self.period:] last = self.series[-1] try: dev = std(period) dev *= self.dev_factor dev += last except (TypeError, ZeroDivisionError, ): dev = None self.append(dev)
def reindex(self): periods = self.periods period = self.series[-periods:] vol = None if len(period) == periods: try: vol = std(period) / mean(period) vol *= 100 except TypeError: pass self.append(vol)
def __init__(self, samples): self.samples = numpy.asarray(samples) self.N = len(samples) self.median = stats.median(samples) self.min = numpy.amin(samples) self.max = numpy.amax(samples) self.mean = stats.mean(samples) self.std = stats.std(samples) self.var = self.std**2. self.skew = stats.skew(samples) self.kurtosis = stats.kurtosis(samples) self.range = self.max - self.min
def _getcs(self, data): if self.c: c = data[self.c] #print data, c sigma, mu = std(c), mean(c) c = N.clip(c, mu - 2 * sigma, mu + 2 * sigma) c = (max(c) - c) / (max(c) - min(c)) else: c = 'b' if self.s: s = data[self.s] else: s = 10.0 return c, s
def _getcs(self, data): if self.c: c = data[self.c] #print data, c sigma, mu = std(c), mean(c) c = N.clip(c, mu - 2*sigma, mu + 2*sigma) c = (max(c) - c)/(max(c) - min(c)) else: c = 'b' if self.s: s = data[self.s] else: s = 10.0 return c, s
def confidence(samples, confidence_level): """This function determines the confidence interval for a given set of samples, as well as the mean, the standard deviation, and the size of the confidence interval as a percentage of the mean. From javastats by Andy Georges. """ mean = stats.mean(samples) sdev = stats.std(samples) n = len(samples) df = n - 1 t = distributions.t.ppf((1+confidence_level)/2.0, df) interval = (interval_low, interval_high) = ( mean - t * sdev / math.sqrt(n) , mean + t * sdev / math.sqrt(n) ) interval_size = interval_high - interval_low interval_percentage = interval_size / mean * 100.0 return (interval, mean, sdev, interval_percentage)
def skewsField(sample, field): """ Checks whether the value of field in the passed in sample is significantly different from the value of field for the rest of the samples under consideration. """ savedSamples = samples.sampleList[:] samples.sampleList.remove(sample) try: flds = samples.getAllFlds(field) mean = stats.mean(flds) stddev = stats.std(flds) val = sample[field] if stddev == 0: devs = 0 else: devs = abs(val - mean) / stddev finally: #we should be fixing the sample list even when I crash! samples.sampleList = savedSamples if len(samples.sampleList) < 3: qual = confidence.Validity.plaus elif len(samples.sampleList) < 6: qual = confidence.Validity.prob else: qual = confidence.Validity.sound conf = __getConfidence((.5, 1, 2, 3, 5), devs, qual) samples.sampleList.sort(key=lambda x: samples.extractField(x, field)) plot = __getPlot('id', field) plot.plotLine(0, mean) plot.plotLine(0, mean-stddev) plot.plotLine(0, mean+stddev) plot.plotLine(0, sample[field]) return SimResult(conf, str(sample) + " has a different " + field + " from other samples", str(sample) + "'s value for " + field + ' is ' + str(devs) + ' standard deviations from the mean', plot)
def main(): if( len(sys.argv) != 3 ): printUsage() executable = sys.argv[1] no_times = int(sys.argv[2]) cmd = "time ./%s" % executable times = zeros(no_times, 'f'); for i in range(no_times): t = time(); os.system(cmd); t_store = time() - t; print "t_store= ", t_store times[i] = t_store; print "Mean time for operation = ", mean(times) print "Standard deviation for operation = ", std(times)
def fit_gaussians(data, initial_params, errs, profnm): numparams = len(initial_params) numgaussians = (len(initial_params)-1)/3 # Generate the parameter structure parinfo = [] params0 = [] for ii in range(numparams): params0.append(initial_params[ii]) parinfo.append({'value':initial_params[ii], 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}) other_args = {'data':data, 'errs':errs} # Now fit it mpfit_out = mpfit.mpfit(fit_function, params0, functkw=other_args, parinfo=parinfo, quiet=1) fit_params = mpfit_out.params fit_errs = mpfit_out.perror # degrees of freedom dof = len(data) - len(fit_params) # chi-squared for the model fit chi_sq = mpfit_out.fnorm print "------------------------------------------------------------------" print "Multi-Gaussian Fit by pygaussfit.py of '%s'"%profnm print "------------------------------------------------------------------" print "mpfit status:", mpfit_out.status print "gaussians:", numgaussians print "DOF:", dof print "chi-sq: %.2f" % chi_sq print "reduced chi-sq: %.2f" % (chi_sq/dof) residuals = data - gen_gaussians(fit_params, len(data)) print "residuals mean: %.3g"%mean(residuals) print "residuals stdev: %.3g"%std(residuals) print "--------------------------------------" print " const = %.5f +/- %.5f" % (fit_params[0], fit_errs[0]) for ii in range(numgaussians): print " phas%d = %.5f +/- %.5f" % (ii+1, fit_params[1+ii*3], fit_errs[1+ii*3]) print " fwhm%d = %.5f +/- %.5f" % (ii+1, fit_params[2+ii*3], fit_errs[2+ii*3]) print " ampl%d = %.5f +/- %.5f" % (ii+1, fit_params[3+ii*3], fit_errs[3+ii*3]) print "--------------------------------------" return fit_params, fit_errs, chi_sq, dof
def histo_plotter(x, SPECS): # the histogram of the data n, bins, patches = hist(x, 50, normed=0) setp(patches, 'facecolor', 'g', 'alpha', 0.75) for i in range(len(SPECS)): out_of_spec = 0 for j in range(len(bins)): if (bins[j] <= SPECS[i]): setp(patches[j], 'facecolor', 'r', 'alpha', 0.75) out_of_spec = out_of_spec + n[j] out_summary="#of Samples Below "+str(SPECS[i])+" :"+str(out_of_spec)+\ "\n From "+str(len(x))+" Samples " fp3.write(out_summary) #print patches # add a 'best fit' line mu = stats.mean(x) sigma = stats.std(x) maxfreq = max(n) minval = min(x) out_summary2 = "Minimum Value: " + fix(minval, 3) fp3.write(out_summary2) # print x, bins, n, mu, sigma y = normpdf(bins, mu, sigma) l = plot(bins, y, 'r--') #y = normpdf( bins) #l = plot(bins, n, 'r--') setp(l, 'linewidth', 1) xlabel('Clearance') ylabel('Count') title(r'$\rm{Histogram\ of\ Clearance}$') axis([bins[0], bins[49], 0.0, maxfreq]) text(.01 + bins[0], .9 * maxfreq, out_summary, color='r') text(.01 + bins[0], .8 * maxfreq, out_summary2, color='b') grid(True) #savefig('histogram_demo',dpi=72) show()
def test_stdX(self): y = stats.std(X) assert_almost_equal(y, 2.738612788)
def test_std(self): y = stats.std(self.testcase) assert_approx_equal(y,1.290994449)
def test_stdHUGE(self): y = stats.std(HUGE) assert_approx_equal(y, 2.738612788e12)
def test_basic(self): a = [3,4,5,10,-3,-5,6] b = [3,4,5,10,-3,-5,-6] assert_almost_equal(stats.std(a),5.2098807225172772,11) assert_almost_equal(stats.std(b),5.9281411203561225,11)
def test_stdBIG(self): y = stats.std(BIG) assert_almost_equal(y, 2.738612788)
def calculate_mean_std(metric): values = list(perf[metric].values()) perf['metrics'][metric]['mean'] = np.mean(values) perf['metrics'][metric]['std'] = std(values)
def cross_validation(X, y, n_trials=5, trial_splits=None, fname=None): """Perform model selection via 5-fold cross validation""" # filter samples with no annotations del_rid = np.where(y.sum(axis=1) == 0)[0] y = np.delete(y, del_rid, axis=0) X = np.delete(X, del_rid, axis=0) # range of hyperparameters C_range = 10.**np.arange(-1, 3) gamma_range = 10.**np.arange(-3, 1) # pre-generating kernels print("### Pregenerating kernels...") K_rbf = {} for gamma in gamma_range: K_rbf[gamma] = rbf_kernel(X, gamma=gamma) print("### Done.") # performance measures perf = dict() pr_micro = [] pr_macro = [] fmax = [] acc = [] if trial_splits is None: # shuffle and split training and test sets trials = ShuffleSplit(n_splits=n_trials, test_size=0.2, random_state=None) ss = trials.split(X) trial_splits = [] for train_idx, test_idx in ss: trial_splits.append((train_idx, test_idx)) it = 0 for jj in range(0, n_trials): train_idx = trial_splits[jj][0] test_idx = trial_splits[jj][1] it += 1 y_train = y[train_idx] y_test = y[test_idx] print("### [Trial %d] Perfom cross validation...." % (it)) print("Train samples=%d; #Test samples=%d" % (y_train.shape[0], y_test.shape[0])) # setup for neasted cross-validation splits = ml_split(y_train) # parameter fitting C_opt = None gamma_opt = None max_aupr = 0 for C in C_range: for gamma in gamma_range: # Multi-label classification cv_results = [] for train, valid in splits: clf = OneVsRestClassifier(svm.SVC(C=C, kernel='precomputed', probability=False), n_jobs=-1) K_train = K_rbf[gamma][ train_idx[train], :][:, train_idx[train]] K_valid = K_rbf[gamma][ train_idx[valid], :][:, train_idx[train]] y_train_t = y_train[train] y_train_v = y_train[valid] y_score_valid = np.zeros(y_train_v.shape, dtype=float) y_pred_valid = np.zeros_like(y_train_v) idx = np.where(y_train_t.sum(axis=0) > 0)[0] clf.fit(K_train, y_train_t[:, idx]) y_score_valid[:, idx] = clf.decision_function(K_valid) y_pred_valid[:, idx] = clf.predict(K_valid) perf_cv = evaluate_performance(y_train_v, y_score_valid, y_pred_valid) cv_results.append(perf_cv['m-aupr']) cv_aupr = np.median(cv_results) print("### gamma = %0.3f, C = %0.3f, AUPR = %0.3f" % (gamma, C, cv_aupr)) if cv_aupr > max_aupr: C_opt = C gamma_opt = gamma max_aupr = cv_aupr print("### Optimal parameters: ") print("C_opt = %0.3f, gamma_opt = %0.3f" % (C_opt, gamma_opt)) print("### Train dataset: AUPR = %0.3f" % (max_aupr)) print("### Using full training data...") clf = OneVsRestClassifier(svm.SVC(C=C_opt, kernel='precomputed', probability=False), n_jobs=-1) y_score = np.zeros(y_test.shape, dtype=float) y_pred = np.zeros_like(y_test) idx = np.where(y_train.sum(axis=0) > 0)[0] clf.fit(K_rbf[gamma_opt][train_idx, :][:, train_idx], y_train[:, idx]) # Compute performance on test set y_score[:, idx] = clf.decision_function( K_rbf[gamma_opt][test_idx, :][:, train_idx]) y_pred[:, idx] = clf.predict(K_rbf[gamma_opt][test_idx, :][:, train_idx]) perf_trial = evaluate_performance(y_test, y_score, y_pred) pr_micro.append(perf_trial['m-aupr']) pr_macro.append(perf_trial['M-aupr']) fmax.append(perf_trial['F1']) acc.append(perf_trial['acc']) print( "### Test dataset: AUPR['micro'] = %0.3f, AUPR['macro'] = %0.3f, F1 = %0.3f, Acc = %0.3f" % (perf_trial['m-aupr'], perf_trial['M-aupr'], perf_trial['F1'], perf_trial['acc'])) perf['m-aupr_avg'] = np.mean(pr_micro) perf['m-aupr_std'] = std(pr_micro) perf['M-aupr_avg'] = np.mean(pr_macro) perf['M-aupr_std'] = std(pr_macro) perf['F1_avg'] = np.mean(fmax) perf['F1_std'] = std(fmax) perf['acc_avg'] = np.mean(acc) perf['acc_std'] = std(acc) if fname is not None: fout = open(fname, 'w') fout.write("aupr[micro], aupr[macro], F_max, accuracy\n") for ii in range(0, n_trials): fout.write(pr_micro[ii], pr_macro[ii], fmax[ii], acc[ii]) fout.close() return perf
def test_stdTINY(self): y = stats.std(TINY) assert_almost_equal(y, 0.0)
def temporal_holdout(X, y, indx, bootstrap, fname, goterms=None, go_fname=None): """Perform temporal holdout validation""" X_train = X[indx['train'].tolist()] X_test = X[indx['test'].tolist()] X_valid = X[indx['valid'].tolist()] y_train = y['train'].tolist() y_test = y['test'].tolist() y_valid = y['valid'].tolist() if goterms is not None: goterms = goterms['terms'].tolist() # range of hyperparameters C_range = 10.**np.arange(-1, 3) gamma_range = 10.**np.arange(-3, 1) # pre-generating kernels print("### Pregenerating kernels...") K_rbf_train = {} K_rbf_test = {} K_rbf_valid = {} for gamma in gamma_range: K_rbf_train[gamma] = rbf_kernel(X_train, gamma=gamma) K_rbf_test[gamma] = rbf_kernel(X_test, X_train, gamma=gamma) K_rbf_valid[gamma] = rbf_kernel(X_valid, X_train, gamma=gamma) print("### Done.") print("Train samples=%d; #Test samples=%d" % (y_train.shape[0], y_test.shape[0])) # parameter fitting C_opt = None gamma_opt = None max_aupr = 0 for C in C_range: for gamma in gamma_range: # Multi-label classification clf = OneVsRestClassifier(svm.SVC(C=C, kernel='precomputed', probability=False), n_jobs=-1) clf.fit(K_rbf_train[gamma], y_train) y_score_valid = clf.decision_function(K_rbf_valid[gamma]) y_pred_valid = clf.predict(K_rbf_valid[gamma]) perf = evaluate_performance(y_valid, y_score_valid, y_pred_valid) micro_aupr = perf['m-aupr'] print("### gamma = %0.3f, C = %0.3f, AUPR = %0.3f" % (gamma, C, micro_aupr)) if micro_aupr > max_aupr: C_opt = C gamma_opt = gamma max_aupr = micro_aupr print("### Optimal parameters: ") print("C_opt = %0.3f, gamma_opt = %0.3f" % (C_opt, gamma_opt)) print("### Train dataset: AUPR = %0.3f" % (max_aupr)) print("### Computing performance on test dataset...") clf = OneVsRestClassifier(svm.SVC(C=C_opt, kernel='precomputed', probability=False), n_jobs=-1) clf.fit(K_rbf_train[gamma_opt], y_train) # Compute performance on test set y_score = clf.decision_function(K_rbf_test[gamma_opt]) y_pred = clf.predict(K_rbf_test[gamma_opt]) # performance measures for bootstrapping perf = dict() pr_micro = [] pr_macro = [] fmax = [] acc = [] # individual goterms pr_goterms = {} for i in range(0, len(goterms)): pr_goterms[goterms[i]] = [] for ind in bootstrap: perf_ind = evaluate_performance(y_test[ind], y_score[ind], y_pred[ind]) pr_micro.append(perf_ind['m-aupr']) pr_macro.append(perf_ind['M-aupr']) fmax.append(perf_ind['F1']) acc.append(perf_ind['acc']) for i in range(0, len(goterms)): pr_goterms[goterms[i]].append(perf_ind[i]) perf['m-aupr_avg'] = np.mean(pr_micro) perf['m-aupr_std'] = std(pr_micro) perf['M-aupr_avg'] = np.mean(pr_macro) perf['M-aupr_std'] = std(pr_macro) perf['F1_avg'] = np.mean(fmax) perf['F1_std'] = std(fmax) perf['acc_avg'] = np.mean(acc) perf['acc_std'] = std(acc) # trials fout = open(fname, 'w') fout.write("aupr[micro], aupr[macro], F_max, accuracy\n") for it in range(0, len(bootstrap)): fout.write(pr_micro[it], pr_macro[it], fmax[it], acc[it], "\n") fout.close() # write performance on individual GO terms if go_fname is not None: fout = open(go_fname, 'wb') print >> fout, "GO_id, AUPRs" for i in range(0, len(goterms)): print >> fout, goterms[i], sum(y_train[:, i]) / float( y_train.shape[0]), for pr in pr_goterms[goterms[i]]: print >> fout, pr, print >> fout fout.close() return perf
def test_stdZERO(self): y = stats.std(ZERO) assert_almost_equal(y, 0.0)
def test_std(self): y = stats.std(self.testcase) assert_approx_equal(y, 1.290994449)
def test_stdROUND(self): y = stats.std(ROUND) assert_approx_equal(y, 2.738612788)
def test_nanstd_some(self): """Check nanstd when some values only are nan.""" s = stats.nanstd(self.Xsome) assert_approx_equal(s, stats.std(self.Xsomet))
def table_fit_many(table, ff, fit_bounds): if fit_bounds is not None: if (stats.std(fits, axis=0) > stats.std(fit.boot)).any(): warnings.warn("BAD")
def test_nanstd_none(self): """Check nanstd when no values are nan.""" s = stats.nanstd(self.X) assert_approx_equal(s, stats.std(self.X))
cc.cycle(dt,fixnuc = 0) if run % 5 == 0: cc.display(dt) length_proj = S.array(cc.length_proj) trajs_theta = S.array(cc.trajs_theta) trans12 = int(cc.temps_1 * 0.5) trans23 = int(cc.temps_2 * 0.5) if len(length_proj) > trans23: longueur_moyenne.append(stats.mean(length_proj[trans12:trans23])) angle_moyen.append(stats.mean(trajs_theta[trans12:trans23])) angle_maxi.append(reduce(maximum,abs(trajs_theta[trans12:trans23]))) angle_onset.append(abs(trajs_theta[trans23])) ecartype.append(stats.std(trajs_theta[trans12:trans23])) duree1.append(cc.temps_1) duree2.append(cc.temps_2 - cc.temps_1) fname = 'cell'+str(run) cc.logging(fname,dt) else : print 'discarded cell # %d ' % run del cc angle_moyen = S.array(angle_moyen) longueur_moyenne= S.array(longueur_moyenne) ecartype = S.array(ecartype) duree2 = S.array(duree2)
def test_basic(self): a = [3, 4, 5, 10, -3, -5, 6] b = [3, 4, 5, 10, -3, -5, -6] assert_almost_equal(stats.std(a), 5.2098807225172772, 11) assert_almost_equal(stats.std(b), 5.9281411203561225, 11)
def zscore(line): '''replacement for scipy.stats.z, but uses N-1 degrees of freedom, instead of N degrees of freedom''' return (line - np.mean(line))/stats.std(line)
from scipy import stats import sys ephemerides = ReadEphemeridesLog(sys.argv[1]) comps = [] for e in ephemerides: c = ComparePysolarToUSNO(e) comps.append(c) az_errors = [c.az_error for c in comps] alt_errors = [c.alt_error for c in comps] print '---------------------' print 'Azimuth stats' print 'Mean error: ' + str(stats.mean(az_errors)) print 'Std dev: ' + str(stats.std(az_errors)) print 'Min error: ' + str(stats.tmin(az_errors, None)) print 'Max error: ' + str(stats.tmax(az_errors, None)) print '----------------------' print 'Altitude stats' print 'Mean error: ' + str(stats.mean(alt_errors)) print 'Std dev: '+ str(stats.std(alt_errors)) print 'Min error: ' + str(stats.tmin(alt_errors, None)) print 'Max error: ' + str(stats.tmax(alt_errors, None)) WriteComparisonsToCSV(comps, 'pysolar_v_usno.csv')
def calculate_mean_std(performance, metric): performance['metrics'][metric] = {} values = list(performance[metric].values()) performance['metrics'][metric]['mean'] = np.mean(values) performance['metrics'][metric]['std'] = std(values)
from scipy import stats import sys ephemerides = ReadEphemeridesLog(sys.argv[1]) comps = [] for e in ephemerides: c = ComparePysolarToUSNO(e) comps.append(c) az_errors = [c.az_error for c in comps] alt_errors = [c.alt_error for c in comps] print '---------------------' print 'Azimuth stats' print 'Mean error: ' + str(stats.mean(az_errors)) print 'Std dev: ' + str(stats.std(az_errors)) print 'Min error: ' + str(stats.tmin(az_errors, None)) print 'Max error: ' + str(stats.tmax(az_errors, None)) print '----------------------' print 'Altitude stats' print 'Mean error: ' + str(stats.mean(alt_errors)) print 'Std dev: ' + str(stats.std(alt_errors)) print 'Min error: ' + str(stats.tmin(alt_errors, None)) print 'Max error: ' + str(stats.tmax(alt_errors, None)) WriteComparisonsToCSV(comps, 'pysolar_v_usno.csv')
def test_stdLITTLE(self): y = stats.std(LITTLE) assert_approx_equal(y, 2.738612788e-8)
def plotB5(B5s,vpns,clrs=None,exps=[1],suffix=''): tt=0 for gg in range(len(B5s)): B5=B5s[gg]; exp=exps[gg];vpn=vpns[gg] titles=['Recalled Position','Selected Position','Selected Position'] posx=np.array([-6,-3,0,3,6,-6,-3,0],ndmin=2) seln=~np.isnan(B5[:,0,0]) a=np.zeros(B5.shape[1]); a[:5]=1 b=np.zeros(B5.shape[1]); if exp==1: b[5:10]=1 else: b[5:]=1 c=np.zeros(B5.shape[1]); c[10:]=1 sels=[a==1,b==1];kk=1 if exp==1: sels.append(c==1) if clrs is None or len(clrs)!=B5.shape[0]: clrs=getColors(B5.shape[0]) #if gg==1: clrs=getColors(28)[:18] #elif gg==2: clrs=getColors(28)[18:] res=[] for sel in sels: objs=[] v=[] subplot(len(B5s),3,gg*3+kk);plt.grid(axis='y');kk+=1 if exp==1: drawCircularAgent(pos=(0,0),eyes=kk<4) else: drawDartAgent(pos=(0,0)) plt.plot([-1,1],[0,0],color='#262626',label='_nolegend_',lw=0.5); plt.plot([0,0],[-1,1],color='#262626',lw=0.5,label='_nolegend_') dx=B5[:,sel,1]-posx[0,:sel.sum()] dy=B5[:,sel,2] mag=np.sqrt(np.power(dx,2)+np.power(dy,2)) if exp==1: phi=np.arctan2(dy,dx)-B5[:,sel,0] else: phi=np.arctan2(dy,dx)-np.pi*B5[:,sel,0]/180 xn=np.cos(phi)*mag yn=np.sin(phi)*mag for i in range(B5.shape[0]): plt.plot(xn[i,:],yn[i,:],'o',mfc=clrs[i], label='_nolegend_',mew=0.15,alpha=0.5,ms=3,mec='k') valid=np.sqrt(np.power(xn[i,:],2)+np.power(yn[i,:],2))<1.2 v.extend(valid.tolist()) if seln[i]: label=str(vpn[i]) else:label='_nolegend_' plt.plot(np.median(xn[i,valid]),np.median(yn[i,valid]),'x', label=label,mec=clrs[i],mew=1.5,ms=6,zorder=3) xn[i,~valid]=np.nan plt.gca().set_aspect('equal') plt.xlim([-1,1]);plt.ylim([-1,1]) #if kk>2: plt.gca().set_yticklabels([]) if gg==0: plt.title(titles[kk-2],fontsize=12) #print np.array(v).sum(),float(seln.sum()), len(v)/float(xn.shape[0]) res.append([xn]) xn=xn.flatten()[np.array(v)] #if gg==1: xxn=np.copy(xn) #elif gg==2: xn=np.concatenate([xxn,xn]) #xn=median(xn,1) m=np.mean(xn) plt.text(plt.xlim()[0]+0.1*(plt.xlim()[1]-plt.xlim()[0]), plt.ylim()[1]-0.1*(plt.ylim()[1]-plt.ylim()[0]), str(unichr(65+tt)),horizontalalignment='center',verticalalignment='center', fontdict={'weight':'bold'},fontsize=12);tt+=1 plt.plot([m,m],[-1,1],'--g',color='gray',label='_nolegend_',zorder=-2) sse=std(xn,bias=True)/xn.size**0.5 res[-1].extend([m,sse,xn.size]) er= sse* stats.t.ppf(0.975,xn.size) er=[m-2*er,m+2*er]#[sap(xn,25),sap(xn,75)] plt.gca().add_patch(plt.Rectangle([er[0],-5],er[1]-er[0],10, color='k',zorder=-2,alpha=0.1)) print " %.3f CI [%.3f, %.3f]"%(m,er[0],er[1]) if False:#kk==2: plt.legend(bbox_to_anchor=(0,0,1,1),loc=2,mode="expand",ncol=seln.sum()/2+1, bbox_transform=plt.gcf().transFigure,frameon=False) if exp==2: x0=(1-3**0.5/2)*AR for i in range(2): plt.plot([x0,x0],[-1,1],':',color='gray') return res
def least_squers_fit(x, y): beta = numpy.corrcoef(x, y) * st.std(y) / st.std(x) alpha = numpy.mean(y) - beta * numpy.mean(x) return alpha, beta