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
python类fill_between()的实例源码
def fitconfpred(xdata,ydata,errx,erry,covxy,nboot=1000,bces='ort',linestyle='',conf=0.68,confcolor='LightGrey',predcolor='Khaki',xplot=None,front=False,**args):
"""
This is a wrapper that given the input data performs the BCES
fit, get the orthogonal parameters and plot (i) the best-fit line,
(ii) confidence band and (iii) prediction band.
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.
Usage:
>>> a1,b1,erra1,errb1,cov1=nemmen.fitconfpred(x[i],y[i],errx[i],erry[i],covxy[i],nboot,bces,linestyle='k',confcolor='LightGrey')
"""
# 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
def func(x): return x[1]*x[0]+x[2]
if front==True:
zorder=10
else:
zorder=None
# Plots prediction band
lpb,upb,xpb=predbandnl(xdata,ydata,func,fitm,covm,2,conf,x)
pylab.fill_between(xpb, lpb, upb, facecolor=predcolor,edgecolor='', zorder=zorder)
# Plots confidence band
lcb,ucb,xcb=confbandnl(xdata,ydata,func,fitm,covm,2,conf,x)
pylab.fill_between(xcb, lcb, ucb, facecolor=confcolor,edgecolor='', zorder=zorder)
return a,b,erra,errb,cov
def fitpred(xdata,ydata,errx,erry,covxy,nboot=1000,bces='ort',linestyle='',conf=0.68,predcolor='Khaki',xplot=None,front=False,**args):
"""
This is a wrapper that given the input data performs the BCES
fit, get the orthogonal parameters and plot (i) the best-fit line and
(ii) prediction band.
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.
Usage:
>>> a1,b1,erra1,errb1,cov1=nemmen.fitpred(x[i],y[i],errx[i],erry[i],covxy[i],nboot,bces,linestyle='k',predcolor='LightGrey')
"""
# 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
def func(x): return x[1]*x[0]+x[2]
if front==True:
zorder=10
else:
zorder=None
# Plots prediction band
lpb,upb,xpb=predbandnl(xdata,ydata,func,fitm,covm,2,conf,x)
pylab.fill_between(xpb, lpb, upb, facecolor=predcolor,edgecolor='', zorder=zorder)
return a,b,erra,errb,cov