def emcee_delay_estimator(t, lc1, e1, lc2, e2, output_tag): t0 = datetime.datetime.now() print 'Process Started on', t0 crude_sig1, crude_tau1, crude_avg_lc1 = crude_lc_param_estimate(t, lc1) crude_sig2, crude_tau2, crude_avg_lc2 = crude_lc_param_estimate(t, lc2) print '' print 'CRUDE LIGHT CURVE PARAMETERS' print '----------------------------' print 'parameter lc1 \tlc2' print '----------------------------' print 'sigma %+1.2f \t%+1.2f' % (crude_sig1, crude_sig2) print 'tau %+1.2f \t%+1.2f' % (crude_tau1, crude_tau2) print 'avg_mag %+1.2f \t%+1.2f' % (crude_avg_lc1, crude_avg_lc2) print '' print '' print 'BEGINNING LIGHT CURVE PARAMETER OPTIMIZATION' print '' op_sig1, op_tau1, op_avg_lc1 = optimize_lc_param_estimate(t, lc1, e1) op_sig2, op_tau2, op_avg_lc2 = optimize_lc_param_estimate(t, lc2, e2) op_sig1 = abs(op_sig1) op_sig2 = abs(op_sig2) print '' print 'OPTIMIZED LIGHT CURVE PARAMETERS' print '--------------------------------' print 'parameter lc1 \tlc2' print '--------------------------------' print 'sigma %+1.2f \t%+1.2f' % (op_sig1, op_sig2) print 'tau %+1.2f \t%+1.2f' % (op_tau1, op_tau2) print 'avg_mag %+1.2f \t%+1.2f' % (op_avg_lc1, op_avg_lc2) print '' print '\nProcess Started on', t0 print 'It is now ', datetime.datetime.now() #ESTIMATE DELAY print '' print 'time series is %1.2f days long' % (t[len(t) - 1] - t[0]) print 'with %d data points' % (len(t)) nbins = int(len(t) / 50.) fig = figure() counts, bin_vals, somemore = hist(log10(diff(t)), nbins) xlabel('Time Between Measurments, log10(days)') fig.savefig("delta_t_histogram_%s.png" % (output_tag)) # ESTIMATE THE MODE OF THE DISTRIBUTION n_max = argmax(counts) mode = bin_vals[n_max] print 'The most common sample time distance is %1.2f days within the range of [%1.2f,%1.2f] days' % ( 10**mode, 10**(0.5 * (bin_vals[n_max - 1] + bin_vals[n_max])), 10 **(0.5 * (bin_vals[n_max] + bin_vals[n_max + 1]))) print '' delta_delay = (10**mode / 50.) print 'Scanning for initial delay estimate with delay step %1.2f' % ( delta_delay) #del_array=arange(-1000.,1000.1,delta_delay) del_array = arange(-1000., 1000.1, 10.) count = -1 ll = [] max_val = -1.e10 c_sig = 0.5 * (op_sig1 + op_sig2) c_tau = 0.5 * (op_tau1 + op_tau2) c_avg_lc = (op_avg_lc1) delta_mag = (op_avg_lc2 - op_avg_lc1) for delta in del_array: count += 1 ct, cx, ce = merge(t, lc1, e1, lc2, e2, delta, delta_mag) val = kelly_ll([c_sig, c_tau, c_avg_lc], ct, cx, ce) if (val > max_val): max_val = val if (count == int(0.01 * len(del_array))): print '1% done' if (count == int(0.10 * len(del_array))): print '10% done' if (count == int(0.25 * len(del_array))): print '25% done' if (count == int(0.50 * len(del_array))): print '50% done' if (count == int(0.75 * len(del_array))): print '75% done' ll.append(val) print 'DONE' print '\nProcess Started on', t0 print 'It is now ', datetime.datetime.now() print '' #PLOT DELAY SEARCH fig = figure() subplot(311) plot(del_array, ll, 'b-') xlabel('delay, days') ylabel('likelihood') title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f' % (c_sig, c_tau, c_avg_lc)) k_max = argmax(ll) plot([del_array[k_max], del_array[k_max]], [min(ll), max(ll)], 'k--') print 'BEST DELAY=%1.5e' % (del_array[k_max]) subplot(312) delta_delay = del_array[1] - del_array[0] k_lo = k_max - int(50 / (delta_delay) + 1) k_up = k_max + int(50 / (delta_delay) + 1) plot(del_array[k_lo:k_up], ll[k_lo:k_up], 'b-') xlabel('delay, days') ylabel('likelihood') #title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f'%(c_sig, c_tau, c_avg_lc)) plot([del_array[k_max], del_array[k_max]], [min(ll), max(ll)], 'k--') ylim(min(ll[k_lo:k_up]), ll[k_max] + 100.) subplot(313) delta_delay = del_array[1] - del_array[0] k_lo = k_max - int(5 / (delta_delay) + 1) k_up = k_max + int(5 / (delta_delay) + 1) plot(del_array[k_lo:k_up], ll[k_lo:k_up], 'b-') xlabel('delay, days') ylabel('likelihood') #title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f'%(c_sig, c_tau, c_avg_lc)) plot([del_array[k_max], del_array[k_max]], [min(ll), max(ll)], 'k--') ylim(max(ll) - 1000., max(ll)) fig.savefig("delay_search_%s.png" % (output_tag)) delay = del_array[k_max] #ESTIMATES OF QSR LIGHT CURVE delta_mag = (op_avg_lc2 - op_avg_lc1) sigma = abs(c_sig) tau = c_tau avg_mag = op_avg_lc1 print '' print 'INITIAL PARAMETERS (average of lc1 and lc2)' print '-------------------------------------------' print 'delay = %1.2f' % (delay) print 'delta_mag = %1.2f' % (delta_mag) print 'avg_mag = %1.2f' % (avg_mag) print 'tau = %1.2f' % (tau) print 'sigma = %1.2f' % (sigma) print '' ct, cx, ce = merge(t, lc1, e1, lc2, e2, delay, delta_mag) c_sig, c_tau, c_avg_lc = optimize_lc_param_estimate(ct, cx, ce) sigma = abs(c_sig) tau = c_tau avg_mag = c_avg_lc print '' print 'INITIAL PARAMETERS (optimization of merged light curve)' print '-------------------------------------------------------' print 'delay = %1.2f' % (delay) print 'delta_mag = %1.2f' % (delta_mag) print 'avg_mag = %1.2f' % (avg_mag) print 'tau = %1.2f' % (tau) print 'sigma = %1.2f' % (sigma) print '' #exit() #PLOT LIGHT CURVES fig = figure() ax = subplot(311) errorbar(t, lc1, e1, fmt='b.', ms=3, label='light curve 1') errorbar(t, lc2, e2, fmt='r.', ms=3, label='light curve 2') legend(loc=1) xlabel('time, day') ylabel('magnitude') #plot(ct,cx,'k.', ms=3) ax = subplot(312) errorbar(t, lc1, e1, fmt='b.', ms=3, label='light curve 1') errorbar(t - delay, lc2 - delta_mag, e2, fmt='r.', ms=3, label='del. and mag. shift lc2') xlabel('time, day') ylabel('magnitude') legend(loc=1) #plot(ct,cx,'k.', ms=3) ax = subplot(313) ct, cx, ce = merge(t, lc1, e1, lc2, e2, delay, delta_mag) errorbar(ct, cx, ce, fmt='b.', ms=3, label='merged light curve') xlabel('time, day') ylabel('magnitude') legend(loc=1) fig.savefig("op_light_curves_%s.png" % (output_tag)) #show() def lnprior(_theta): _delay, _delta_mag, _sigma, _tau, _avg_mag = _theta if delay - 50. < _delay < delay + 50. and delta_mag - 1. < _delta_mag < delta_mag + 1. and sigma * 0.1 < _sigma < sigma * 10. and tau * 0.1 < _tau < tau * 10. and avg_mag - 1. < _avg_mag < avg_mag + 1.: return 0.0 return -np.inf def lnprob(theta, t, lc1, err1, lc2, err2): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + kelly_delay_ll(theta, t, lc1, err1, lc2, err2) print '' print '-------------' print 'RUNNING EMCEE' print '-------------' print '' #RUN EMCEE ndim, nwalkers = 5, 100 n_burn_in_iterations = 100 n_iterations = 1000 print 'pos' true_vals = [delay, delta_mag, sigma, tau, avg_mag] #print 'np.random.randn(ndim)',np.random.randn(ndim) pos = [true_vals + 1e-4 * np.random.randn(ndim) for i in range(nwalkers)] #pos = [[uniform(delay-100,delay+100.),uniform(delta_mag-1.,delta_mag+1.), uniform(0.,sigma*10.), uniform(tau*0.1, tau*10.), uniform(avg_mag-0.2+avg_mag-0.2)] for i in range(nwalkers)] #pos = [true_vals + [uniform(-1.e3, 1.e3), uniform(-10.,10.), uniform(0.1,10.), uniform(0.1,800.), uniform(-50.,50.)] for i in range(nwalkers)] #uniform([low, high, size]) r = np.random.randn(ndim) #pos = [true_vals + [10**mode*r[0], 0.1*delta_mag*r[1], abs((sigma*r[2]+sigma)), ] for i in range(nwalkers)] print 'sampler' sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(t, lc1, e1, lc2, e2)) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'burn-in sampler.run_mcmc' #sampler.run_mcmc(pos, 500) sampler.run_mcmc(pos, n_burn_in_iterations) samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) print("\tMean acceptance fraction: {0:.3f}".format( np.mean(sampler.acceptance_fraction))) mc_delay, mc_delta_mag, mc_sigma, mc_tau, mc_avg_mag = map( lambda v: (v[1], v[2] - v[1], v[1] - v[0]), zip(*np.percentile(samples, [16, 50, 84], axis=0))) print '\tErrors based on 16th, 50th, and 84th percentile' print '\tParam \tMC_rec\terr_up\terr_low' print '\tdelay \t%1.2e\t+%1.2e\t-%1.2e' % (mc_delay[0], mc_delay[1], mc_delay[2]) print '\tdelta_mag \t%1.2e\t+%1.2e\t-%1.2e' % ( mc_delta_mag[0], mc_delta_mag[1], mc_delta_mag[2]) print '\tsigma \t%1.2e\t+%1.2e\t-%1.2e' % (mc_sigma[0], mc_sigma[1], mc_sigma[2]) print '\ttau \t%1.2e\t+%1.2e\t-%1.2e' % (mc_tau[0], mc_tau[1], mc_tau[2]) print '\tavg_mag \t%1.2e\t+%1.2e\t-%1.2e' % ( mc_avg_mag[0], mc_avg_mag[1], mc_avg_mag[2]) #fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"], #truths=[mc_delay[0], mc_delta_mag[0], mc_sigma[0], mc_tau[0], mc_avg_mag[0]]) fig = triangle.corner( samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"]) fig.savefig("burn_in_triangle_%s.png" % (output_tag)) sampler.reset() print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'sampler.run_mcmc' #sampler.run_mcmc(pos, 100) sampler.run_mcmc(pos, n_iterations) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'sampler.chain' samples = sampler.chain[:, int(0.1 * n_iterations):, :].reshape((-1, ndim)) print len(samples) print("Mean acceptance fraction: {0:.3f}".format( np.mean(sampler.acceptance_fraction))) mc_delay, mc_delta_mag, mc_sigma, mc_tau, mc_avg_mag = map( lambda v: (v[1], v[2] - v[1], v[1] - v[0]), zip(*np.percentile(samples, [16, 50, 84], axis=0))) print 'Errors based on 16th, 50th, and 84th percentile' print 'Param \tMC_rec\terr_up\terr_low' print 'delay \t%1.2e\t+%1.2e\t-%1.2e' % (mc_delay[0], mc_delay[1], mc_delay[2]) print 'delta_mag \t%1.2e\t+%1.2e\t-%1.2e' % ( mc_delta_mag[0], mc_delta_mag[1], mc_delta_mag[2]) print 'sigma \t%1.2e\t+%1.2e\t-%1.2e' % (mc_sigma[0], mc_sigma[1], mc_sigma[2]) print 'tau \t%1.2e\t+%1.2e\t-%1.2e' % (mc_tau[0], mc_tau[1], mc_tau[2]) print 'avg_mag \t%1.2e\t+%1.2e\t-%1.2e' % (mc_avg_mag[0], mc_avg_mag[1], mc_avg_mag[2]) #figure(2) #for i in range(ndim): #figure() #hist(sampler.flatchain[:,i], 100, color="k", histtype="step") #title("Dimension {0:d}".format(i)) #fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"], #truths=[mc_delay[0], mc_delta_mag[0], mc_sigma[0], mc_tau[0], mc_avg_mag[0]]) fig = triangle.corner( samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"]) fig.savefig("triangle_%s.png" % (output_tag)) fig = figure() ax = subplot(311) errorbar(t, lc1, e1, fmt='b.', ms=3, label='light curve 1') errorbar(t, lc2, e2, fmt='r.', ms=3, label='light curve 2') legend(loc=1) xlabel('time, day') ylabel('magnitude') #plot(ct,cx,'k.', ms=3) ax = subplot(312) errorbar(t, lc1, e1, fmt='b.', ms=3, label='light curve 1') errorbar(t - mc_delay[0], lc2 - mc_delta_mag[0], e2, fmt='r.', ms=3, label='del. and mag. shift lc2') xlabel('time, day') ylabel('magnitude') legend(loc=1) #plot(ct,cx,'k.', ms=3) ax = subplot(313) ct, cx, ce = merge(t, lc1, e1, lc2, e2, mc_delay[0], mc_delta_mag[0]) errorbar(ct, cx, ce, fmt='b.', ms=3, label='merged light curve') xlabel('time, day') ylabel('magnitude') legend(loc=1) fig.savefig("mc_light_curves_%s.png" % (output_tag)) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now()
def kelly_delay_ll(theta, time_array, flux_array1, ph_err_array1, flux_array2, ph_err_array2): delay, delta_mag, sig, tau, avg_mag = theta t, x, e = merge(time_array, flux_array1, ph_err_array1, flux_array2, ph_err_array2, delay, delta_mag) return kelly_ll([sig, tau, avg_mag], t, x, e)
def optimize_lc_param_estimate(_t, _x, _e): nll = lambda *args: -kelly_ll(*args) result = op.fmin(nll, crude_lc_param_estimate(_t, _x), args=(_t, _x, _e)) return result
def emcee_delay_estimator(t, lc1, e1, lc2, e2, output_tag): t0 = datetime.datetime.now() print 'Process Started on', t0 crude_sig1, crude_tau1, crude_avg_lc1 = crude_lc_param_estimate(t,lc1) crude_sig2, crude_tau2, crude_avg_lc2 = crude_lc_param_estimate(t,lc2) print '' print 'CRUDE LIGHT CURVE PARAMETERS' print '----------------------------' print 'parameter lc1 \tlc2' print '----------------------------' print 'sigma %+1.2f \t%+1.2f'%(crude_sig1, crude_sig2) print 'tau %+1.2f \t%+1.2f'%(crude_tau1, crude_tau2) print 'avg_mag %+1.2f \t%+1.2f'%(crude_avg_lc1, crude_avg_lc2) print '' print '' print 'BEGINNING LIGHT CURVE PARAMETER OPTIMIZATION' print '' op_sig1, op_tau1, op_avg_lc1 = optimize_lc_param_estimate(t,lc1,e1) op_sig2, op_tau2, op_avg_lc2 = optimize_lc_param_estimate(t,lc2,e2) op_sig1=abs(op_sig1) op_sig2=abs(op_sig2) print '' print 'OPTIMIZED LIGHT CURVE PARAMETERS' print '--------------------------------' print 'parameter lc1 \tlc2' print '--------------------------------' print 'sigma %+1.2f \t%+1.2f'%(op_sig1, op_sig2) print 'tau %+1.2f \t%+1.2f'%(op_tau1, op_tau2) print 'avg_mag %+1.2f \t%+1.2f'%(op_avg_lc1, op_avg_lc2) print '' print '\nProcess Started on', t0 print 'It is now ', datetime.datetime.now() #ESTIMATE DELAY print '' print 'time series is %1.2f days long'%(t[len(t)-1]-t[0]) print 'with %d data points'%(len(t)) nbins=int(len(t)/50.) fig=figure() counts, bin_vals, somemore =hist(log10(diff(t)), nbins) xlabel('Time Between Measurments, log10(days)') fig.savefig("delta_t_histogram_%s.png"%(output_tag)) # ESTIMATE THE MODE OF THE DISTRIBUTION n_max=argmax(counts) mode=bin_vals[n_max] print 'The most common sample time distance is %1.2f days within the range of [%1.2f,%1.2f] days'%(10**mode, 10**(0.5*(bin_vals[n_max-1]+bin_vals[n_max])), 10**(0.5*(bin_vals[n_max]+bin_vals[n_max+1]))) print '' delta_delay=(10**mode/50.) print 'Scanning for initial delay estimate with delay step %1.2f'%(delta_delay) #del_array=arange(-1000.,1000.1,delta_delay) del_array=arange(-1000.,1000.1,10.) count=-1 ll=[] max_val = -1.e10 c_sig = 0.5*(op_sig1+op_sig2) c_tau = 0.5*(op_tau1+op_tau2) c_avg_lc = (op_avg_lc1) delta_mag=(op_avg_lc2-op_avg_lc1) for delta in del_array: count+=1 ct, cx, ce = merge(t, lc1, e1, lc2, e2, delta, delta_mag) val = kelly_ll([c_sig, c_tau, c_avg_lc] , ct, cx, ce) if(val>max_val): max_val=val if(count==int(0.01*len(del_array))): print '1% done' if(count==int(0.10*len(del_array))): print '10% done' if(count==int(0.25*len(del_array))): print '25% done' if(count==int(0.50*len(del_array))): print '50% done' if(count==int(0.75*len(del_array))): print '75% done' ll.append(val) print 'DONE' print '\nProcess Started on', t0 print 'It is now ', datetime.datetime.now() print '' #PLOT DELAY SEARCH fig=figure() subplot(311) plot(del_array,ll,'b-') xlabel('delay, days') ylabel('likelihood') title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f'%(c_sig, c_tau, c_avg_lc)) k_max=argmax(ll) plot([del_array[k_max],del_array[k_max]],[min(ll),max(ll)],'k--') print 'BEST DELAY=%1.5e'%(del_array[k_max]) subplot(312) delta_delay=del_array[1]-del_array[0] k_lo = k_max - int(50/(delta_delay)+1) k_up = k_max + int(50/(delta_delay)+1) plot(del_array[k_lo:k_up],ll[k_lo:k_up],'b-') xlabel('delay, days') ylabel('likelihood') #title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f'%(c_sig, c_tau, c_avg_lc)) plot([del_array[k_max],del_array[k_max]],[min(ll),max(ll)],'k--') ylim(min(ll[k_lo:k_up]),ll[k_max]+100.) subplot(313) delta_delay=del_array[1]-del_array[0] k_lo = k_max - int(5/(delta_delay)+1) k_up = k_max + int(5/(delta_delay)+1) plot(del_array[k_lo:k_up],ll[k_lo:k_up],'b-') xlabel('delay, days') ylabel('likelihood') #title('Delay Likelihood Scan\nsigma=%1.2f, tau=%1.2f, avg_mag=%1.2f'%(c_sig, c_tau, c_avg_lc)) plot([del_array[k_max],del_array[k_max]],[min(ll),max(ll)],'k--') ylim(max(ll)-1000.,max(ll)) fig.savefig("delay_search_%s.png"%(output_tag)) delay = del_array[k_max] #ESTIMATES OF QSR LIGHT CURVE delta_mag = (op_avg_lc2-op_avg_lc1) sigma = abs(c_sig) tau = c_tau avg_mag=op_avg_lc1 print '' print 'INITIAL PARAMETERS (average of lc1 and lc2)' print '-------------------------------------------' print 'delay = %1.2f'%(delay) print 'delta_mag = %1.2f'%(delta_mag) print 'avg_mag = %1.2f'%(avg_mag) print 'tau = %1.2f'%(tau) print 'sigma = %1.2f'%(sigma) print '' ct, cx, ce = merge(t, lc1, e1, lc2, e2, delay, delta_mag) c_sig, c_tau, c_avg_lc = optimize_lc_param_estimate(ct,cx,ce) sigma = abs(c_sig) tau = c_tau avg_mag = c_avg_lc print '' print 'INITIAL PARAMETERS (optimization of merged light curve)' print '-------------------------------------------------------' print 'delay = %1.2f'%(delay) print 'delta_mag = %1.2f'%(delta_mag) print 'avg_mag = %1.2f'%(avg_mag) print 'tau = %1.2f'%(tau) print 'sigma = %1.2f'%(sigma) print '' #exit() #PLOT LIGHT CURVES fig = figure() ax=subplot(311) errorbar(t,lc1,e1, fmt='b.', ms=3, label='light curve 1') errorbar(t,lc2,e2, fmt='r.', ms=3, label='light curve 2') legend(loc=1) xlabel('time, day') ylabel('magnitude') #plot(ct,cx,'k.', ms=3) ax=subplot(312) errorbar(t,lc1,e1, fmt='b.', ms=3, label='light curve 1') errorbar(t-delay, lc2-delta_mag, e2, fmt='r.', ms=3, label='del. and mag. shift lc2') xlabel('time, day') ylabel('magnitude') legend(loc=1) #plot(ct,cx,'k.', ms=3) ax=subplot(313) ct, cx, ce = merge(t, lc1, e1, lc2, e2, delay, delta_mag) errorbar(ct,cx, ce,fmt='b.', ms=3, label = 'merged light curve') xlabel('time, day') ylabel('magnitude') legend(loc=1) fig.savefig("op_light_curves_%s.png"%(output_tag)) #show() def lnprior(_theta): _delay, _delta_mag, _sigma, _tau, _avg_mag = _theta if delay-50.<_delay<delay+50. and delta_mag-1.<_delta_mag<delta_mag+1. and sigma*0.1<_sigma<sigma*10. and tau*0.1<_tau<tau*10. and avg_mag-1.<_avg_mag<avg_mag+1.: return 0.0 return -np.inf def lnprob(theta, t, lc1, err1, lc2, err2): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + kelly_delay_ll(theta, t, lc1, err1, lc2, err2) print '' print '-------------' print 'RUNNING EMCEE' print '-------------' print '' #RUN EMCEE ndim, nwalkers = 5, 100 n_burn_in_iterations = 100 n_iterations = 1000 print 'pos' true_vals=[delay, delta_mag, sigma, tau, avg_mag] #print 'np.random.randn(ndim)',np.random.randn(ndim) pos = [true_vals + 1e-4*np.random.randn(ndim) for i in range(nwalkers)] #pos = [[uniform(delay-100,delay+100.),uniform(delta_mag-1.,delta_mag+1.), uniform(0.,sigma*10.), uniform(tau*0.1, tau*10.), uniform(avg_mag-0.2+avg_mag-0.2)] for i in range(nwalkers)] #pos = [true_vals + [uniform(-1.e3, 1.e3), uniform(-10.,10.), uniform(0.1,10.), uniform(0.1,800.), uniform(-50.,50.)] for i in range(nwalkers)] #uniform([low, high, size]) r=np.random.randn(ndim) #pos = [true_vals + [10**mode*r[0], 0.1*delta_mag*r[1], abs((sigma*r[2]+sigma)), ] for i in range(nwalkers)] print 'sampler' sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(t, lc1, e1, lc2, e2)) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'burn-in sampler.run_mcmc' #sampler.run_mcmc(pos, 500) sampler.run_mcmc(pos, n_burn_in_iterations) samples = sampler.chain[:, 50:, :].reshape((-1, ndim)) print("\tMean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction))) mc_delay, mc_delta_mag, mc_sigma, mc_tau, mc_avg_mag = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]), zip(*np.percentile(samples, [16, 50, 84],axis=0))) print '\tErrors based on 16th, 50th, and 84th percentile' print '\tParam \tMC_rec\terr_up\terr_low' print '\tdelay \t%1.2e\t+%1.2e\t-%1.2e'%(mc_delay[0], mc_delay[1], mc_delay[2]) print '\tdelta_mag \t%1.2e\t+%1.2e\t-%1.2e'%(mc_delta_mag[0], mc_delta_mag[1], mc_delta_mag[2]) print '\tsigma \t%1.2e\t+%1.2e\t-%1.2e'%(mc_sigma[0], mc_sigma[1], mc_sigma[2]) print '\ttau \t%1.2e\t+%1.2e\t-%1.2e'%(mc_tau[0], mc_tau[1], mc_tau[2]) print '\tavg_mag \t%1.2e\t+%1.2e\t-%1.2e'%(mc_avg_mag[0], mc_avg_mag[1], mc_avg_mag[2]) #fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"], #truths=[mc_delay[0], mc_delta_mag[0], mc_sigma[0], mc_tau[0], mc_avg_mag[0]]) fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"]) fig.savefig("burn_in_triangle_%s.png"%(output_tag)) sampler.reset() print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'sampler.run_mcmc' #sampler.run_mcmc(pos, 100) sampler.run_mcmc(pos, n_iterations) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now() print 'sampler.chain' samples = sampler.chain[:, int(0.1*n_iterations):, :].reshape((-1, ndim)) print len(samples) print("Mean acceptance fraction: {0:.3f}".format(np.mean(sampler.acceptance_fraction))) mc_delay, mc_delta_mag, mc_sigma, mc_tau, mc_avg_mag = map(lambda v: (v[1], v[2]-v[1], v[1]-v[0]), zip(*np.percentile(samples, [16, 50, 84],axis=0))) print 'Errors based on 16th, 50th, and 84th percentile' print 'Param \tMC_rec\terr_up\terr_low' print 'delay \t%1.2e\t+%1.2e\t-%1.2e'%(mc_delay[0], mc_delay[1], mc_delay[2]) print 'delta_mag \t%1.2e\t+%1.2e\t-%1.2e'%(mc_delta_mag[0], mc_delta_mag[1], mc_delta_mag[2]) print 'sigma \t%1.2e\t+%1.2e\t-%1.2e'%(mc_sigma[0], mc_sigma[1], mc_sigma[2]) print 'tau \t%1.2e\t+%1.2e\t-%1.2e'%(mc_tau[0], mc_tau[1], mc_tau[2]) print 'avg_mag \t%1.2e\t+%1.2e\t-%1.2e'%(mc_avg_mag[0], mc_avg_mag[1], mc_avg_mag[2]) #figure(2) #for i in range(ndim): #figure() #hist(sampler.flatchain[:,i], 100, color="k", histtype="step") #title("Dimension {0:d}".format(i)) #fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"], #truths=[mc_delay[0], mc_delta_mag[0], mc_sigma[0], mc_tau[0], mc_avg_mag[0]]) fig = triangle.corner(samples, labels=["delay", "delta_mag", "$\sigma$", r"$\tau$", "avg_mag"]) fig.savefig("triangle_%s.png"%(output_tag)) fig = figure() ax=subplot(311) errorbar(t,lc1,e1, fmt='b.', ms=3, label='light curve 1') errorbar(t,lc2,e2, fmt='r.', ms=3, label='light curve 2') legend(loc=1) xlabel('time, day') ylabel('magnitude') #plot(ct,cx,'k.', ms=3) ax=subplot(312) errorbar(t,lc1,e1, fmt='b.', ms=3, label='light curve 1') errorbar(t-mc_delay[0], lc2-mc_delta_mag[0], e2, fmt='r.', ms=3, label='del. and mag. shift lc2') xlabel('time, day') ylabel('magnitude') legend(loc=1) #plot(ct,cx,'k.', ms=3) ax=subplot(313) ct, cx, ce = merge(t, lc1, e1, lc2, e2, mc_delay[0], mc_delta_mag[0]) errorbar(ct,cx, ce,fmt='b.', ms=3, label = 'merged light curve') xlabel('time, day') ylabel('magnitude') legend(loc=1) fig.savefig("mc_light_curves_%s.png"%(output_tag)) print 'Process Started on', t0 print 'It is currently ', datetime.datetime.now()
figure() subplot(111) errorbar(t, X_data, yerr=error, fmt='k.', label='2% error') plot(t, X, 'rs', ms=4, mec='r', label='Light Curve') legend(loc=2) xlabel('time, days') ylabel('Rel. Mag.') #show() figure() subplot(311) avg_array = arange(avg_mag - 50., avg_mag + 50., 1.) ll_avg_array = [] for m in avg_array: ll_avg_array.append( kelly_ll([sigma, tau, m], t, X_data, error * ones(len(t)))) print len(avg_array), len(ll_avg_array) plot(avg_array, ll_avg_array, 'k-') plot([avg_mag, avg_mag], [max(ll_avg_array) - 10., max(ll_avg_array) + 1.], 'k--') ylim(max(ll_avg_array) - 5., max(ll_avg_array) + 1.) xlabel('avg magnitude, mag') ylabel('log likelihood') subplot(312) tau_array = 10**arange(-1., 5., 0.01) #tau_array=arange(tau-50.,tau+50.,0.1) ll_tau_array = [] for m in tau_array: ll_tau_array.append( kelly_ll([sigma, m, avg_mag], t, X_data, error * ones(len(t))))
def lnprob(theta, t, lc, err): lp = lnprior(theta) if not np.isfinite(lp): return -np.inf return lp + kelly_ll(theta, t, lc, err)
def kelly_delay_ll(theta, time_array, flux_array1, ph_err_array1, flux_array2, ph_err_array2): delay, delta_mag, sig, tau, avg_mag = theta t, x, e = merge(time_array, flux_array1, ph_err_array1, flux_array2, ph_err_array2, delay, delta_mag) return kelly_ll([sig,tau,avg_mag], t, x, e)
figure() subplot(111) errorbar(t,X_data,yerr=error, fmt='k.', label='2% error') plot(t,X, 'rs', ms=4, mec='r', label='Light Curve') legend(loc=2) xlabel('time, days') ylabel('Rel. Mag.') #show() figure() subplot(311) avg_array=arange(avg_mag-50.,avg_mag+50.,1.) ll_avg_array=[] for m in avg_array: ll_avg_array.append(kelly_ll([sigma, tau, m], t, X_data, error*ones(len(t)))) print len(avg_array), len(ll_avg_array) plot(avg_array,ll_avg_array, 'k-') plot([avg_mag,avg_mag],[max(ll_avg_array)-10.,max(ll_avg_array)+1.],'k--') ylim(max(ll_avg_array)-5.,max(ll_avg_array)+1.) xlabel('avg magnitude, mag') ylabel('log likelihood') subplot(312) tau_array=10**arange(-1.,5.,0.01) #tau_array=arange(tau-50.,tau+50.,0.1) ll_tau_array=[] for m in tau_array: ll_tau_array.append(kelly_ll([sigma, m, avg_mag], t, X_data, error*ones(len(t)))) semilogx(tau_array,ll_tau_array, 'k-') plot([tau,tau],[max(ll_tau_array)-10.,max(ll_tau_array)+1.],'k--')