def cost(p, x, y): """Cost function for LM least-squares fit""" mu, sigma, A = p return A * g(mu, sigma, x) - y
mvi2mot[(mviname, framei0)] = mot[recname] allmotion = np.hstack(list(mvi2mot.values())) allmotion = np.hstack([allmotion, -allmotion]) # make it symmetric around 0 motionbins = np.arange(-300, 300+MOTIONBINW, MOTIONBINW) # deg/s, symmetric around 0 midbins = motionbins[:-1] + MOTIONBINW / 2 motioncount = np.histogram(allmotion, bins=motionbins)[0] k = kurtosis(allmotion) # kurtosistest() seems to use the method of Anscombe & Glynn (1983), # http://biomet.oxfordjournals.org/content/70/1/227 z, p = kurtosistest(allmotion) pstring = 'p < %g' % ceilsigfig(p) # normally distributed signal with same std as data, to check that its kurtosis is 0: #nsamples = 10000000 #normal = scipy.random.normal(0, allmotion.std(), nsamples) #normalcount = np.histogram(normal, bins=motionbins)[0] normalcount = core.g(0, allmotion.std(), midbins) # generate normal distrib directly # normalize to get same probability mass: normalcount = normalcount / normalcount.sum() * motioncount.sum() plot(midbins, normalcount, marker=None, ls='-', c='0.7', lw=2) plot(midbins, motioncount, marker=None, ls='-', c='k', lw=2) text(0.98, 0.98, 'k = %.1f' % k, # kurtosis horizontalalignment='right', verticalalignment='top', transform=gca().transAxes, color='k') text(0.98, 0.90, '%s' % pstring, # p-value of null (normal) hypothesis of kurtosis test horizontalalignment='right', verticalalignment='top', transform=gca().transAxes, color='k') #k = kurtosis(normal) #z, p = kurtosistest(normal) #text(0.98, 0.82, 'k=%.1f' % k, # kurtosis # horizontalalignment='right', verticalalignment='top', # transform=gca().transAxes, color='e')
edges = np.logspace(logstart, logend, nbins+1) # nbins+1 points in log space y = hist(allrates, bins=edges, color='k')[0] logx = np.log10(edges)[:-1] # left bin edges logx = logx + (logx[1] - logx[0]) / 2 # middle of bins # do least-squares LM fit of a lognormal distribution to the data: modellogmean = -1 modellogsigma = 1 A = max(y) p = modellogmean, modellogsigma, A result = leastsq(cost, p, args=(logx, y), full_output=True) p, cov_p, infodict, mesg, ier = result mu, sigma, A = p modelx = np.logspace(logstart, logend, 200) # 200 points in log space modely = A*g(mu, sigma, np.log10(modelx)) # Figure 2: plot lognormal model in magenta: plot(modelx, modely, 'm-', linewidth=3) # Figure 2: finish up axvline(x=MINRATE, c='e', ls='--', marker=None) # plot threshold xscale('log') xlabel('mean firing rate (Hz)') ylabel('neuron count') # arrow doesn't display correctly on log axis, use annotate instead: #arrow(10**logmean, 30, 0, -4, head_width=0.1, head_length=2, length_includes_head=True) annotate('', xy=(10**logmean, 26), xycoords='data', xytext=(10**logmean, 29), textcoords='data', arrowprops=dict(fc='k', ec='none', width=1, frac=0.5)) annotate('', xy=(10**mu, 26), xycoords='data',
sp = slogmean, slogstd, sA # parameter list dp = dlogmean, dlogstd, dA def cost(p, x, y): """Cost function for LM least-squares fit""" mu, sigma, A = p return A * g(mu, sigma, x) - y # fit the 3 parameters for both synched and desynched distribs: sresult = leastsq(cost, sp, args=(logmidbins, shist), full_output=True) dresult = leastsq(cost, dp, args=(logmidbins, dhist), full_output=True) sp, cov_p, infodict, mesg, ier = sresult dp, cov_p, infodict, mesg, ier = dresult smu, ssigma, sA = sp dmu, dsigma, dA = dp # generate fit distributions from fit parameters modelx = np.logspace(logmin, logmax, 200) # 200 points in log space smodel = sA*g(smu, ssigma, log10(modelx)) dmodel = dA*g(dmu, dsigma, log10(modelx)) # plot the fit distributions: plot(modelx, smodel, 'r--', lw=1, alpha=0.5) plot(modelx, dmodel, 'b--', lw=1, alpha=0.5) # display geometric means and percentages of response events with <= 1 spike/trial: text(0.02, 0.984, '$\mu$ = %.1f' % smean, # synched horizontalalignment='left', verticalalignment='top', transform=gca().transAxes, color='r') text(0.02, 0.904, '$\mu$ = %.1f' % dmean, # desynched horizontalalignment='left', verticalalignment='top', transform=gca().transAxes, color='b') ''' text(0.98, 0.82, 'p < %.1g' % ceilsigfig(p, 1), horizontalalignment='right', verticalalignment='top', transform=gca().transAxes, color='k')
def model(self, p, ts): """Temporal sum of Gaussians""" #try: V0, V1, s0, s1, Voff = p return V0*g(self.t0, s0, ts) + V1*g(self.t1, s1, ts) + Voff
edges = np.logspace(logstart, logend, nbins + 1) # nbins+1 points in log space y = hist(allrates, bins=edges, color='k')[0] logx = np.log10(edges)[:-1] # left bin edges logx = logx + (logx[1] - logx[0]) / 2 # middle of bins # do least-squares LM fit of a lognormal distribution to the data: modellogmean = -1 modellogsigma = 1 A = max(y) p = modellogmean, modellogsigma, A result = leastsq(cost, p, args=(logx, y), full_output=True) p, cov_p, infodict, mesg, ier = result mu, sigma, A = p modelx = np.logspace(logstart, logend, 200) # 200 points in log space modely = A * g(mu, sigma, np.log10(modelx)) # Figure 2: plot lognormal model in magenta: plot(modelx, modely, 'm-', linewidth=3) # Figure 2: finish up axvline(x=MINRATE, c='e', ls='--', marker=None) # plot threshold xscale('log') xlabel('mean firing rate (Hz)') ylabel('neuron count') # arrow doesn't display correctly on log axis, use annotate instead: #arrow(10**logmean, 30, 0, -4, head_width=0.1, head_length=2, length_includes_head=True) annotate('', xy=(10**logmean, 26), xycoords='data', xytext=(10**logmean, 29),
allmotion = np.hstack(list(mvi2mot.values())) allmotion = np.hstack([allmotion, -allmotion]) # make it symmetric around 0 motionbins = np.arange(-300, 300 + MOTIONBINW, MOTIONBINW) # deg/s, symmetric around 0 midbins = motionbins[:-1] + MOTIONBINW / 2 motioncount = np.histogram(allmotion, bins=motionbins)[0] k = kurtosis(allmotion) # kurtosistest() seems to use the method of Anscombe & Glynn (1983), # http://biomet.oxfordjournals.org/content/70/1/227 z, p = kurtosistest(allmotion) pstring = 'p < %g' % ceilsigfig(p) # normally distributed signal with same std as data, to check that its kurtosis is 0: #nsamples = 10000000 #normal = scipy.random.normal(0, allmotion.std(), nsamples) #normalcount = np.histogram(normal, bins=motionbins)[0] normalcount = core.g(0, allmotion.std(), midbins) # generate normal distrib directly # normalize to get same probability mass: normalcount = normalcount / normalcount.sum() * motioncount.sum() plot(midbins, normalcount, marker=None, ls='-', c='0.7', lw=2) plot(midbins, motioncount, marker=None, ls='-', c='k', lw=2) text( 0.98, 0.98, 'k = %.1f' % k, # kurtosis horizontalalignment='right', verticalalignment='top', transform=gca().transAxes, color='k') text( 0.98, 0.90,
# Do least-squares LM fit of a lognormal distribution to the data. # Everything is done in log space: # set initial parameters: sA, dA = max(shist), max(dhist) # Gaussian amplitude sp = slogmean, slogstd, sA # parameter list dp = dlogmean, dlogstd, dA # fit the 3 parameters for both synched and desynched distribs: sresult = leastsq(cost, sp, args=(logmidbins, shist), full_output=True) dresult = leastsq(cost, dp, args=(logmidbins, dhist), full_output=True) sp, cov_p, infodict, mesg, ier = sresult dp, cov_p, infodict, mesg, ier = dresult smu, ssigma, sA = sp dmu, dsigma, dA = dp # generate fit distributions from fit parameters modelx = np.logspace(logmin, logmax, 200) # 200 points in log space smodel = sA * g(smu, ssigma, log10(modelx)) dmodel = dA * g(dmu, dsigma, log10(modelx)) # plot the fit distributions: plot(modelx, smodel, 'r--', lw=1, alpha=0.5) plot(modelx, dmodel, 'b--', lw=1, alpha=0.5) xscale('log') xlim(xmin=10**logmin, xmax=10**logmax) ylim(ymax=ymax) xticks(ticks) yticks(range(0, 40 + 10, 10)) xlabel('mean firing rate (Hz)') ylabel('unit count') # display geometric means and p value: text( 0.02, 0.98,
def model(self, p, ts): """Temporal sum of Gaussians""" #try: V0, V1, s0, s1, Voff = p return V0 * g(self.t0, s0, ts) + V1 * g(self.t1, s1, ts) + Voff
def cost(p, x, y): mu, sigma, A = p return A * g(mu, sigma, x) - y