def compareTimes(table_names = ['FinalEstimate_4x100_N=100','FinalEstimate_4x100_N=1000']): regime_labels = {'subT': 'Sub-threshold', 'superT': 'Supra-threshold', 'superSin': 'Supra-sinusoidal', 'crit': 'Critical'} for table_name, N in zip(table_names, [100, 1000]): analyzer = DataAnalyzer(table_name); print table_name for regime in analyzer.getAllRegimes(): print regime_labels[regime] for mthd in [ 'Fortet', 'FP']: walltimes = analyzer.getWallTimes(regime, mthd) mu, sigma = mean(walltimes), std(walltimes) print '& %.2f'%mu, r'$\pm$ %.2f'%sigma print r'\\'
def boxPlotBox(): table_file_name = 'FinalEstimate_4x100_N=100' Methods = ['Initializer', 'FP', 'Fortet'] regime = 'crit' analyzer = DataAnalyzer(table_file_name) mpl.rcParams['figure.figsize'] = 17, 6 method_labels = ['Initializer', 'Numerical FP', 'Fortet'] xlabel_font_size = 12 abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true figure() lgs = GridSpec(1, 3) abgs = {} for mthd in Methods: abgs[mthd] = array(analyzer.getAllEstimates(regime, mthd)) # method_xpos = dict.fromkeys(Methods, [1,2,3]) param_tags = [r' \alpha ', r' \beta ', r' \gamma '] for params_idx, param_tag in enumerate(param_tags): print params_idx, param_tag ax_low = subplot(lgs[params_idx]); hold(True) # for mthd in Methods: param_ests = [ abgs[mthd][:,params_idx] for mthd in Methods] param_true = abg_true[params_idx] boxplot(param_ests, positions = [1,3,5]) hlines(param_true, 0,5, linestyles='dashed') title(r'$%s = %.2f$'%(param_tag, param_true)) ylabel(r'$%s$'%param_tag, fontsize = xlabel_font_size) xtickNames = setp(ax_low, xticklabels = method_labels) setp(xtickNames, rotation=60, fontsize=xlabel_font_size) # subplots_adjust(left=0.1, right=0.95, top=0.9, bottom=0.35, wspace = 0.65, hspace = 0.2)
def postVisualizer(): N_phi = 20 print 'N_phi = ', N_phi phi_norms = linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi) theta = 20 base_name = 'sinusoidal_spike_train_N=1000_critical_theta=%d'%theta T_thresh = 64. analyzer = DataAnalyzer('ThetaEstimate_4x100_N=1000') sample_id = 32 regime_label = base_name + '%d'%theta file_name = base_name + '_%d'%sample_id print file_name binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms) binnedTrain.pruneBins(None, N_thresh = 1, T_thresh=T_thresh) regime_name = 'theta%d'%theta abg_true = analyzer.getTrueParamValues(regime_name); print abg_true abg_fortet = analyzer.getEstimates(sample_id, regime_name, 'Fortet')[0] print abg_fortet visualizeData_vs_Fortet(abg_fortet, binnedTrain, theta,title_tag = 'Fortet: estimates', save_fig_name='theta20_Fortet_estimates') visualizeData_vs_Fortet(abg_true, binnedTrain, theta,title_tag = 'Fortet: true', save_fig_name='theta20_Fortet_true') abg_fp = analyzer.getEstimates(sample_id, regime_name, 'FP')[0] print abg_fp dx = .025; dt = FPMultiPhiSolver.calculate_dt(dx, abg_true, -1.0) phis = binnedTrain.bins.keys(); S = FPMultiPhiSolver(binnedTrain.theta, phis, dx, dt, binnedTrain.getTf(), X_min = -1.0) visualizeData_vs_FP(S, abg_fp, binnedTrain,title_tag = 'FP: estimates', save_fig_name='theta20_FP_estimates') visualizeData_vs_FP(S, abg_true, binnedTrain,title_tag = 'FP: true', save_fig_name='theta20_FP_true')
def tabulateEstimates(table_file_names, methods): for table_file_name in table_file_names: print table_file_name analyzer = DataAnalyzer(table_file_name) regimeNames = analyzer.getAllRegimes() param_names = [r'\a',r'\b',r'\g'] for regime in regimeNames: print r'\subfloat[%s]{\begin{tabular}{|c|ccc|} '%regime_labels[regime] abg_true = analyzer.getTrueParamValues(regime) print 'Parameter' for mthd in methods: print r'&', mthd print r'\\ \hline' for idx in xrange(3): print r'$%s=%.2f$' %(param_names[idx],abg_true[idx]) for mthd in methods: abg_est = array(analyzer.getAllEstimates(regime, mthd)) est = sort(abg_est[:, idx]) print r'& $%.2f : [%.2f, %.2f]$' %(mean(est), est[2], est[-3]) print r'\\' print r' \end{tabular}}\\' print 64*'#'
def crossAnalyze(table_file_name, fig_id = '', Method1 = 'Fortet', Method2 = 'FP', regimeNames = None): analyzer = DataAnalyzer(table_file_name) mpl.rcParams['figure.figsize'] = 17, 5*4 mpl.rcParams['figure.subplot.left'] = .15 mpl.rcParams['figure.subplot.right'] = .975 mpl.rcParams['figure.subplot.bottom'] = .05 mpl.rcParams['figure.subplot.top'] = .95 mpl.rcParams['figure.subplot.wspace'] = .25 mpl.rcParams['figure.subplot.hspace'] = .375 fig = figure(); if None==regimeNames: regimeNames = analyzer.getAllRegimes() plot_scale = 1.05; regime_tags = {'superT':'Supra-Threshold', 'crit':'Critical', 'subT':'Sub-Threshold', 'superSin':'Super-Sinusoidal', 'theta1':r'$\Omega=1$', 'theta5':r'$\Omega=5$', 'theta10':r'$\Omega=10$', 'theta20':r'$\Omega=20$'} xlabel_font_size = 32 for regime_idx,regime in enumerate(regimeNames): abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true abgs = {}; abgs[Method1] = array( analyzer.getAllEstimates(regime, Method1) ) abgs[Method2] = array( analyzer.getAllEstimates(regime, Method2) ) for param_idx, pname in zip(arange(3), [r'\alpha', r'\beta', r'\gamma']): axu_lim = amax( r_[ abgs[Method1][:, param_idx], abgs[Method2][:, param_idx]]) axl_lim = amin( r_[ abgs[Method1][:, param_idx], abgs[Method2][:, param_idx]]) plot_buffer = (plot_scale -1.)*(axu_lim - axl_lim); axu_lim += plot_buffer axl_lim -= plot_buffer ax = subplot(len(regimeNames),3, regime_idx*3 + param_idx+1, aspect='equal') plot(abgs[Method1][:, param_idx], abgs[Method2][:, param_idx], linestyle='None', color= 'k', marker= 'o', markersize = 7 ) if 0 == param_idx: text(-.4, 0.5, regime_tags[regime], horizontalalignment='center', verticalalignment='center', transform = ax.transAxes, rotation='vertical', size = xlabel_font_size) # axu_lim, axl_lim = xlim() hlines(abg_true[param_idx], axl_lim, axu_lim, linestyles = 'dashed' ) # axu_lim, axl_lim = ylim() vlines(abg_true[param_idx], axl_lim, axu_lim, linestyles = 'dashed' ) ax.xaxis.set_major_locator(MaxNLocator(6)) ax.yaxis.set_major_locator(MaxNLocator(6)) xlim((axl_lim, axu_lim)); ylim((axl_lim, axu_lim)); xlabel(Method1,fontsize = xlabel_font_size); ylabel(Method2,fontsize = xlabel_font_size); title('$'+ pname + ' = %.2f$'%abg_true[param_idx], fontsize = 32) t = add_inner_title(ax, chr(ord('A') + regime_idx*3 + param_idx), loc=2, size=dict(size=ABCD_LABEL_SIZE)) t.patch.set_ec("none") t.patch.set_alpha(0.5) if '' != fig_id: file_name = os.path.join(FIGS_DIR, fig_id + '_cross_compare_joint.pdf') print 'saving to ', file_name savefig(file_name)
def postProcessThetaBoxPlots(table_file_name, fig_id = '', Methods = ['Initializer', 'FP', 'Fortet'], regimeNames = ['theta1', 'theta5', 'theta10', 'theta20'] ): analyzer = DataAnalyzer(table_file_name) if None==regimeNames: regimeNames = analyzer.getAllRegimes() mpl.rcParams['figure.figsize'] = 17, 12 figure() subplots_adjust(left=0.1, right=0.975, top=0.9, bottom=0.15, wspace = 0.3, hspace = 0.01) lgs = GridSpec(4,3) method_labels = ['Initializer', 'Numerical FP', 'Fortet'] xlabel_font_size = 24 sample_size_texts = ['N=100', 'N=1000'] yupper = 3*[-inf]; ylower = 3*[inf] for params_idx in xrange(3): for mthd in Methods: for regime in regimeNames: Omega = int(regime[5:]) ests = array(analyzer.getAllEstimates(regime, mthd))[:,params_idx] if (2 == params_idx): ests /= sqrt( 1. + Omega*Omega) ylower[params_idx] = amin([ylower[params_idx], amin(ests)]) yupper[params_idx] = amax([yupper[params_idx], amax(ests)]) for regime_idx, regime in enumerate(regimeNames): abg_true = analyzer.getTrueParamValues(regime) Omega = int(regime[5:]) print regime, abg_true, Omega abgs = {} param_tags = [r' \alpha ', r' \beta ', r' \gamma / \sqrt{1+ \Omega}'] for mthd in Methods: abgs[mthd] = array(analyzer.getAllEstimates(regime, mthd)) for params_idx, param_tag in enumerate(param_tags): print params_idx, param_tag subplot_idx = regime_idx*3 + params_idx; ax = subplot(lgs[subplot_idx]); hold(True) # for mthd in Methods: param_ests = [ abgs[mthd][:,params_idx] for mthd in Methods] param_true = abg_true[params_idx] if params_idx == 2: param_true /= sqrt(1. + Omega*Omega) for idx in xrange(len(param_ests)): param_ests[idx] /= sqrt(1. + Omega*Omega) boxplot(param_ests, positions = [1,3,5]) hlines(param_true, 0,5, linestyles='dashed') ylabel(r'$\hat{%s}$'%param_tag, fontsize = xlabel_font_size) if (regime_idx ==0): title(r'$%s = %.1f$'%(param_tag, param_true), fontsize = 32) if (params_idx == 2): title(r'$\gamma = .5\cdot \sqrt{1+ \Omega}$', fontsize = 32) if(regime_idx == 3): xtickNames = setp(ax, xticklabels = method_labels) setp(xtickNames, rotation=30, fontsize=xlabel_font_size) ylim((ylower[params_idx], yupper[params_idx])) ymin, ymax = ylim() activeticks = linspace(ymin, ymax, 7)[1:-1] yticks(activeticks, ['%.2f'%tick for tick in activeticks]) if 0 == params_idx: text(-.25, 0.5, r'$\Omega = %s$' %regime[5:], horizontalalignment='center', verticalalignment='center', transform = ax.transAxes, rotation='vertical', size = xlabel_font_size) tag_loc = 3; # if (median(param_ests[0]) > param_true): # tag_loc = 3; xlim((.0,5.5 )) t = add_inner_title(ax, chr(ord('A') + subplot_idx), loc=tag_loc, size=dict(size=ABCD_LABEL_SIZE)) t.patch.set_ec("none") t.patch.set_alpha(0.5) file_name = os.path.join(FIGS_DIR, fig_id + 'thetas_est_rel_errors' +'.pdf') get_current_fig_manager().window.showMaximized() print 'saving to ', file_name savefig(file_name)
def postProcessJointBoxPlot(table_file_names, fig_id = '', Methods = ['Initializer', 'Nelder-Mead', 'Fortet'], regimeNames = ['superT', 'subT', 'crit', 'superSin']): analyzers = []; for table_file_name in table_file_names: analyzer = DataAnalyzer(table_file_name) analyzers.append(analyzer) analyzer = analyzers[0] if None==regimeNames: regimeNames = analyzer.getAllRegimes() for regime in regimeNames: abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true mpl.rcParams['figure.figsize'] = 17, 6 method_labels = ['Initializer', 'Numerical FP', 'Fortet'] xlabel_font_size = 24 sample_size_texts = ['N=100', 'N=1000'] for regime in regimeNames: yupper = 3*[-inf]; ylower = 3*[inf] for params_idx in xrange(3): for mthd in Methods: for analyzer in analyzers: ests = array(analyzer.getAllEstimates(regime, mthd))[:,params_idx] ylower[params_idx] = amin([ylower[params_idx], amin(ests)]) yupper[params_idx] = amax([yupper[params_idx], amax(ests)]) abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true figure() subplots_adjust(left=0.1, right=0.975, top=0.9, bottom=0.25, wspace = 0.25, hspace = 0.01) lgs = GridSpec(2, 3) for sample_size_idx, analyzer in enumerate(analyzers): abgs = {} for mthd in Methods: abgs[mthd] = array(analyzer.getAllEstimates(regime, mthd)) # method_xpos = dict.fromkeys(Methods, [1,2,3]) param_tags = [r' \alpha ', r' \beta ', r' \gamma '] for params_idx, param_tag in enumerate(param_tags): print params_idx, param_tag subplot_idx = sample_size_idx*3 + params_idx; ax = subplot(lgs[subplot_idx]); hold(True) # for mthd in Methods: param_ests = [ abgs[mthd][:,params_idx] for mthd in Methods] param_true = abg_true[params_idx] boxplot(param_ests, positions = [1,3,5]) hlines(param_true, 0,5, linestyles='dashed') ylabel(r'$\hat{%s}$'%param_tag, fontsize = xlabel_font_size) if (sample_size_idx ==0): title(r'$%s = %.2f$'%(param_tag, param_true), fontsize = 32) if(sample_size_idx ==1): xtickNames = setp(ax, xticklabels = method_labels) setp(xtickNames, rotation=30, fontsize=xlabel_font_size) ylim((ylower[params_idx], yupper[params_idx])) ymin, ymax = ylim() activeticks = linspace(ymin, ymax, 7)[1:-1] yticks(activeticks, ['%.2f'%tick for tick in activeticks]) if 0 == params_idx: text(-.25, 0.5, sample_size_texts[sample_size_idx], horizontalalignment='center', verticalalignment='center', transform = ax.transAxes, rotation='vertical', size = xlabel_font_size) tag_loc = 3; # if (median(param_ests[0]) > param_true): # tag_loc = 3; xlim((.0, 5.5)) t = add_inner_title(ax, chr(ord('A') + subplot_idx), loc=tag_loc, size=dict(size=ABCD_LABEL_SIZE)) t.patch.set_ec("none") t.patch.set_alpha(0.5) file_name = os.path.join(FIGS_DIR, fig_id + regime+'_est_rel_errors_joint' +'.pdf') get_current_fig_manager().window.showMaximized() print 'saving to ', file_name savefig(file_name)
def flagWarnings(table_names): for table_name in table_names: analyzer = DataAnalyzer(table_name); print table_name print analyzer.getAllWarnings()
def alphaVsGamma(table_file_name, fig_id='', Methods=['Initializer', 'FP', 'Fortet']): analyzer = DataAnalyzer(table_file_name) mpl.rcParams['figure.figsize'] = 17, 5*4 mpl.rcParams['figure.subplot.left'] = .1 mpl.rcParams['figure.subplot.right'] = .975 mpl.rcParams['figure.subplot.bottom'] = .025 mpl.rcParams['figure.subplot.top'] = .95 mpl.rcParams['figure.subplot.wspace'] = .25 mpl.rcParams['figure.subplot.hspace'] = .375 fig = figure(); plot_scale = 1.05; regimeNames = analyzer.getAllRegimes() regime_tags = {'superT':'Supra-Threshold', 'crit':'Critical', 'subT':'Sub-Threshold', 'superSin':'Super-Sinusoidal', 'theta1':r'$\Omega=1$', 'theta5':r'$\Omega=5$', 'theta10':r'$\Omega=10$', 'theta20':r'$\Omega=20$'} xlabel_font_size = 32 for regime_idx,regime in enumerate(regimeNames): abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true for method_idx, method in enumerate(Methods): abgs = array( analyzer.getAllEstimates(regime, method) ) axu_lim = amax( r_[ abgs[:, 0], abg_true[0]]) axl_lim = amin( r_[ abgs[:, 0], abg_true[0]]) plot_buffer = 5*(plot_scale -1.)*(axu_lim - axl_lim); axu_lim += plot_buffer axl_lim -= plot_buffer ayu_lim = amax( r_[ abgs[:, 2], abg_true[2]]) ayl_lim = amin( r_[ abgs[:, 2], abg_true[2]]) plot_buffer = (plot_scale -1.)*(ayu_lim - ayl_lim); ayu_lim += plot_buffer ayl_lim -= plot_buffer ax = subplot(len(regimeNames),3, regime_idx*len(Methods) + method_idx+1) plot(abgs[:, 0], abgs[:, 2], linestyle='None', color= 'k', marker= 'o', markersize = 7 ) if 0 == method_idx: text(-.3, 0.5, regime_tags[regime], horizontalalignment='center', verticalalignment='center', transform = ax.transAxes, rotation='vertical', size = xlabel_font_size) # axu_lim, axl_lim = xlim() hlines(abg_true[2], axl_lim, axu_lim, linestyles = 'dashed' ) # axu_lim, axl_lim = ylim() vlines(abg_true[0], ayl_lim, ayu_lim, linestyles = 'dashed' ) xlim((axl_lim, axu_lim)); ylim((ayl_lim, ayu_lim)); xlabel(r'$\alpha$',fontsize = xlabel_font_size); ylabel(r'$\gamma$',fontsize = xlabel_font_size); title(method , fontsize = 32) #+ ' : $\\Omega= %.1f$' %sqrt((2.0*abg_true[2])**2 -1.) t = add_inner_title(ax, chr(ord('A') + regime_idx*len(Methods) + method_idx), loc=2, size=dict(size=ABCD_LABEL_SIZE)) t.patch.set_ec("none") t.patch.set_alpha(0.5) if '' != fig_id: file_name = os.path.join(FIGS_DIR, fig_id + '_alphagamma_compare_joint.pdf') print 'saving to ', file_name savefig(file_name)
def crossAnalyzeJoint(table_file_name1, table_file_name2, fig_id = '', Method1 = 'Fortet', Method2 = 'FP', regimeNames = None): analyzer = DataAnalyzer(table_file_name1) analyzer2 = DataAnalyzer(table_file_name2) mpl.rcParams['figure.figsize'] = 17, 5*4 mpl.rcParams['figure.subplot.left'] = .05 mpl.rcParams['figure.subplot.right'] = .975 mpl.rcParams['figure.subplot.bottom'] = .025 mpl.rcParams['figure.subplot.top'] = .95 mpl.rcParams['figure.subplot.wspace'] = .1 mpl.rcParams['figure.subplot.hspace'] = .375 fig = figure(); if None==regimeNames: regimeNames = analyzer.getAllRegimes() for regime_idx,regime in enumerate(regimeNames): abg_true = analyzer.getTrueParamValues(regime) print regime, abg_true abgs = {}; abgs[Method1] = array( analyzer.getAllEstimates(regime, Method1) ) abgs[Method2] = array( analyzer.getAllEstimates(regime, Method2) ) abgs2 = {} abgs2[Method1] = array( analyzer2.getAllEstimates(regime, Method1) ) abgs2[Method2] = array( analyzer2.getAllEstimates(regime, Method2) ) plot_scale = 1.05; for param_idx, pname in zip(arange(3), [r'\alpha', r'\beta', r'\gamma']): axu_lim = amax( r_[ abgs[Method1][:, param_idx], abgs[Method2][:, param_idx]]) axl_lim = amin( r_[ abgs[Method1][:, param_idx], abgs[Method2][:, param_idx]]) plot_buffer = (plot_scale -1.)*(axu_lim - axl_lim); axu_lim += plot_buffer axl_lim -= plot_buffer ax = subplot(len(regimeNames),3, regime_idx*3 + param_idx+1, aspect='equal') plot(abgs[Method1][:, param_idx], abgs[Method2][:, param_idx], linestyle='None', color= 'k', marker= 'x', markersize = 7, label='$N=100$' ) plot(abgs2[Method1][:, param_idx], abgs2[Method2][:, param_idx], linestyle='None', color= 'k', marker= 'd', markersize = 7, label='$N=1000$' ) if param_idx == 0: legend(loc='lower left') # axu_lim, axl_lim = xlim() hlines(abg_true[param_idx], axl_lim, axu_lim, linestyles = 'dashed' ) # axu_lim, axl_lim = ylim() vlines(abg_true[param_idx], axl_lim, axu_lim, linestyles = 'dashed' ) ax.xaxis.set_major_locator(MaxNLocator(6)) ax.yaxis.set_major_locator(MaxNLocator(6)) xlim((axl_lim, axu_lim)); ylim((axl_lim, axu_lim)); xlabel(Method1,fontsize = 24); ylabel(Method2,fontsize = 24); title('$'+ pname + ' = %.2f$'%abg_true[param_idx], fontsize = 32) t = add_inner_title(ax, chr(ord('A') + regime_idx*3 + param_idx), loc=2, size=dict(size=20)) t.patch.set_ec("none") t.patch.set_alpha(0.5) # markers = ['x', '+', '^']: # for marker_idx, table_file_name in table_file_names[1:]: # analyzer = DataAnalyzer(table_file_name) if '' != fig_id: file_name = os.path.join(FIGS_DIR, fig_id + '_cross_compare_joint.pdf') print 'saving to ', file_name savefig(file_name)
def Harness(sample_id=13, regime_name='superSin', N_spikes = 1000, visualize=False): from scipy.stats.distributions import norm N_phi = 20; phi_norms = linspace(1/(2.*N_phi), 1. - 1/ (2.*N_phi), N_phi) base_name = 'sinusoidal_spike_train_N=%d_'%N_spikes regime_label = base_name + regime_name # T_thresh = 128.; file_name = regime_label + '_' + str(sample_id) print file_name binnedTrain = BinnedSpikeTrain.initFromFile(file_name, phi_norms) # print 'Warning: pruning bins' # binnedTrain.pruneBins(None, N_thresh = 100) bins = binnedTrain.bins; phis = bins.keys() N_phi = len(phis) alpha, beta, gamma, theta = binnedTrain._Train._params.getParams() def loss_function_simple(abg, visualize, fig_tag = ''): error = .0; if visualize: figure() for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): Is = bins[phi_m]['Is'] uniqueIs = bins[phi_m]['unique_Is'] a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m,binnedTrain.theta) LHS_numerator = movingThreshold(uniqueIs[1:]) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*uniqueIs[1:])) LHS = 1 - norm.cdf(LHS_numerator / LHS_denominator) RHS = zeros_like(LHS) N = len(Is) for rhs_idx in xrange(1,len(uniqueIs)): t = uniqueIs[rhs_idx] lIs = Is[Is<t] taus = t - lIs; numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*taus)) RHS[rhs_idx-1] = sum(1. - norm.cdf(numerator/denominator)) / N weight = len(Is) lerror = dot((LHS - RHS)**2 , diff(uniqueIs)) * weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1);hold(True) ts = uniqueIs[1:]; plot(ts, LHS, 'b'); plot(ts, RHS, 'rx'); # annotate('$\phi$ = %.2g'%(phi_m), ((min(ts), max(LHS)/2.)), ) annotate('lerror = %.3g'%lerror,((min(ts), max(LHS)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2, 1); title(fig_tag) return error def loss_function_nonvectorized(abg, visualize=False): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): # for phi_m in phis: Is = bins[phi_m]['Is'] N = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); # def RHS(t): # lIs = Is[Is<t]; # taus = t - lIs; # numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.) # denominator = b * sqrt(1. - exp(-2*taus)) # return sum(1. - norm.cdf(numerator/denominator)) / N def RHS(ts): if False == iterable(ts): ts = [ts] rhs = empty_like(ts) for t, t_indx in zip(ts, xrange(size(ts))): lIs = Is[Is<t]; taus = t - lIs; numerator = (movingThreshold(t) - movingThreshold(lIs)* exp(-taus)) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*taus)) rhs[t_indx] = sum(1. - norm.cdf(numerator/denominator)) / N return rhs integrand = lambda t: (LHS(t) - RHS(t)) **2 from scipy.integrate import quad, quadrature, fixed_quad # quadrature, quad_error = quad(integrand, a= 1e-8, b=Tf+1e-8, limit = 50) quadrature, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8, tol=5e-03, rtol=1.49e-04, maxiter = 64, vec_func = True) # val , err_msg = fixed_quad( integrand, a= 1e-8, b=Tf+1e-8, # n = 12) # print 'quadrature = ',quadrature # print 'val = ',val # print 'difference = ', quadrature - val weight = len(Is) #VISUALIZE FOR NOW: if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1);hold(True) ts = linspace(1e-8, Tf+1e-8, 100) ; lhs = empty_like(ts); rhs = empty_like(ts); for t, t_indx in zip(ts, xrange(len(ts))): lhs[t_indx] = LHS(t); rhs[t_indx] = RHS(t); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); error += quadrature* weight; return error def loss_function_quadGaussian(abg, visualize=False, fig_tag = ''): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): # for phi_m in phis: Is = bins[phi_m]['Is'] unique_Is = bins[phi_m]['unique_Is'] N_Is = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); def RHS(ts): if False == iterable(ts): ts = array([ts]) # rhs = empty_like(ts) # Is.reshape((len(Is),1)) lIs = tile(Is, len(ts) ).reshape((len(ts), len(Is))).transpose() lts = tile(ts, (len(Is),1 ) ) mask = lIs < lts taus = (lts - lIs); #*mask #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway: numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*abs(taus))) rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is return rhs integrand = lambda t: (LHS(t) - RHS(t)) **2 from scipy.integrate import quad, quadrature, fixed_quad # valcheck, quad_error = quad(integrand, a= 1e-8, b=Tf+1e-8, limit = 64) val, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8, tol=5e-03, rtol=1.49e-04, maxiter = 64, vec_func = True) weight = len(Is) lerror = val* weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True) # ts = linspace(1e-8, Tf+1e-8, 100) ; ts = unique_Is[1:]; lhs = LHS(ts); rhs = RHS(ts); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2,1); title(fig_tag) return error def loss_function_L1(abg, visualize=False, fig_tag = ''): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): # for phi_m in phis: Is = bins[phi_m]['Is'] unique_Is = bins[phi_m]['unique_Is'] N_Is = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m, theta) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); def RHS(ts): if False == iterable(ts): ts = array([ts]) # rhs = empty_like(ts) # Is.reshape((len(Is),1)) lIs = tile(Is, len(ts) ).reshape((len(ts), len(Is))).transpose() lts = tile(ts, (len(Is),1 ) ) mask = lIs < lts taus = (lts - lIs); #*mask #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway: numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*abs(taus))) rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is return rhs integrand = lambda t: abs(LHS(t) - RHS(t)) from scipy.integrate import quad, quadrature, fixed_quad print unique_Is val, quad_error = quad(integrand, a= 1e-8, b=Tf+1., limit = 64, points = sort(unique_Is) ) # val, quad_error = quadrature(integrand, a= 1e-8, b=Tf+1e-8, # tol=5e-03, rtol=1.49e-04, # maxiter = 64, # vec_func = True) # val , err_msg = fixed_quad( integrand, a= 1e-8, b=Tf+1e-8, weight = len(Is) lerror = val* weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True) # ts = linspace(1e-8, Tf+1e-8, 100) ; ts = unique_Is[1:]; lhs = LHS(ts); rhs = RHS(ts); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2,1); title(fig_tag) return error def loss_function_manualquad(abg, visualize=False, fig_tag = ''): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): Is = bins[phi_m]['Is'] unique_Is = bins[phi_m]['unique_Is'] N_Is = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); def RHS(ts): if False == iterable(ts): ts = array([ts]) lIs = tile(Is, len(ts) ).reshape((len(ts), len(Is))).transpose() lts = tile(ts, (len(Is),1 ) ) mask = lIs < lts taus = (lts - lIs); #*mask #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway: numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*abs(taus))) rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is return rhs integrand = lambda t: (LHS(t) - RHS(t)) **2 dt = 1e-2; ts_manual = arange(1e-8, Tf+1e-2, dt ) integrand = LHS(ts_manual) - RHS(ts_manual) val = dot(integrand, integrand)*dt; weight = len(Is) lerror = val* weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True) # ts = linspace(1e-8, Tf+1e-8, 100) ; ts = unique_Is[1:]; lhs = LHS(ts); rhs = RHS(ts); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2,1); title(fig_tag) return error def loss_function_supnormalized(abg, visualize=False, fig_tag = ''): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): Is = bins[phi_m]['Is'] unique_Is = bins[phi_m]['unique_Is'] N_Is = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); def RHS(ts): if False == iterable(ts): ts = array([ts]) lIs = tile(Is, len(ts) ).reshape((len(ts), len(Is))).transpose() lts = tile(ts, (len(Is),1 ) ) mask = lIs < lts taus = (lts - lIs); #*mask #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway: numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*abs(taus))) rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is return rhs dt = 1e-3; ts_manual = arange(1e-8, Tf+1e-2, dt) lhs = LHS(ts_manual) difference = abs(lhs - RHS(ts_manual))/amax(lhs) val = amax(difference); weight = len(Is) lerror = val* weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True) # ts = linspace(1e-8, Tf+1e-8, 100) ; ts = unique_Is[1:]; lhs = LHS(ts); rhs = RHS(ts); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2,1); title(fig_tag) return error def loss_function_sup(abg, visualize=False, fig_tag = ''): error = .0; if visualize: figure(); for (phi_m, phi_idx) in zip(phis, xrange(N_phi)): Is = bins[phi_m]['Is'] unique_Is = bins[phi_m]['unique_Is'] N_Is = len(Is); Tf = amax(Is); a,b,g = abg[0], abg[1], abg[2] movingThreshold = getMovingThreshold(a,g, phi_m, binnedTrain.theta) def LHS(ts): LHS_numerator = movingThreshold(ts) *sqrt(2.) LHS_denominator = b * sqrt(1 - exp(-2*ts)) return 1 - norm.cdf(LHS_numerator / LHS_denominator); def RHS(ts): if False == iterable(ts): ts = array([ts]) lIs = tile(Is, len(ts) ).reshape((len(ts), len(Is))).transpose() lts = tile(ts, (len(Is),1 ) ) mask = lIs < lts taus = (lts - lIs); #*mask #NOTE BELOW WE use abs(taus) since for non-positive taus we will mask away anyway: numerator = (movingThreshold(lts) - movingThreshold(lIs)* exp(-abs(taus))) * sqrt(2.) denominator = b * sqrt(1. - exp(-2*abs(taus))) rhs = sum( (1. - norm.cdf(numerator/denominator))*mask, axis=0) / N_Is return rhs dt = 1e-3; ts_manual = arange(1e-8, Tf+1e-2, dt) lhs = LHS(ts_manual) difference = abs(lhs - RHS(ts_manual)) val = amax(difference); weight = len(Is) lerror = val* weight; error += lerror if visualize: subplot(ceil(len(phis)/2),2, phi_idx+1); hold(True) # ts = linspace(1e-8, Tf+1e-8, 100) ; ts = unique_Is[1:]; lhs = LHS(ts); rhs = RHS(ts); plot(ts, lhs, 'b'); plot(ts, rhs, 'rx'); annotate('lerror = %.3g'%lerror,((min(ts), max(lhs)/2.)), ) if visualize: subplot(ceil(len(phis)/2),2,1); title(fig_tag) return error #EXPERIMENT: # Analyzer = DataAnalyzer() def outlinept(): pass analyzer = DataAnalyzer('FvsWF_4x16'); abg_true = analyzer.getTrueParamValues(regime_name) loss_function_L1(abg_true, visualize=True) return quad_estimated = analyzer.getEstimates(sample_id, regime_name, 'QuadFortet')[0] simple_estimated = analyzer.getEstimates(sample_id, regime_name, 'Fortet')[0] for abg, tag, L in zip(3*[abg_true, quad_estimated], ['sup_true_params' , 'sup_estimated_params', 'supnormailzed_true_params', 'supnormailzed_estimated_params', 'manualquad_true_params' , 'manualquad_estimated_params'], 2*[loss_function_sup]+ 2*[loss_function_supnormalized] + 2*[loss_function_manualquad]): start = time.clock() loss = L(abg,visualize, fig_tag = regime + '_' + tag); end = time.clock() print tag, ':%.2f,%.2f,%.2f:' %(abg[0],abg[1],abg[2]), 'error = %.4f'%loss , ' | compute time = ', end - start