def fitconfmc(xdata,ydata,errx,erry,covxy,nboot=1000,bces='ort',linestyle='',conf=1.,confcolor='gray',xplot=None,front=False,**args):
"""
This is a wrapper that given the input data performs the BCES
fit, get the orthogonal parameters and plot the best-fit line and
confidence band (generated using MC). I decided to put together these
commands in a method because I have been using them very frequently.
Assumes you initialized the plot window before calling this method.
This method is more stable than fitconf, which is plagued with numerical
instabilities when computing the gradient.
Usage:
>>> a1,b1,erra1,errb1,cov1=nemmen.fitconf(x[i],y[i],errx[i],erry[i],covxy[i],nboot,bces,linestyle='k',confcolor='LightGrey')
Explanation of some arguments:
- xplot: if provided, will compute the confidence band in the X-values provided
with xplot
- front: if True, then will plot the confidence band in front of the data
points; otherwise, will plot it behind the points
- conf: size of confidence band to be plotted in standard deviations
"""
# Selects the desired BCES method
i=whichbces(bces)
# Performs the BCES fit
a,b,erra,errb,cov=bcesp(xdata,errx,ydata,erry,covxy,nboot)
# Plots best-fit
if xplot==None:
x=numpy.linspace(xdata.min(),xdata.max(),100)
else:
x=xplot
pylab.plot(x,a[i]*x+b[i],linestyle,**args)
fitm=numpy.array([ a[i],b[i] ]) # array with best-fit parameters
covm=numpy.array([ (erra[i]**2,cov[i]), (cov[i],errb[i]**2) ]) # covariance matrix
# Plots confidence band
lcb,ucb,y=confbandmc(x,fitm,covm,10000,conf)
if front==True:
zorder=10
else:
zorder=None
pylab.fill_between(x, lcb, ucb, alpha=0.3, facecolor=confcolor, zorder=zorder)
return a,b,erra,errb,cov
评论列表
文章目录