41 merenych bodu (interval (-8,4))
import numpy as np
a0,b0,c0,d0=3.,4.,2.,1.
nlev=0.2
x=np.r_[-8:4:41j][1:]
ydata=lambda x:a0+b0*x+c0*x**2+d0*x**3+np.random.normal(0,nlev,size=x.shape)
amat=np.array([np.ones_like(x),x,x**2,x**3])
hess=amat.dot(amat.T)
cov=np.linalg.inv(hess)
def ahat(y):
pars=cov.dot(amat.dot(y))
s0=((y-amat.T.dot(pars))**2).sum()
return list(pars)+[s0]
pars=np.array([ahat(ydata(x)) for i in range(1000)]) #1000 iteraci
pars.mean(0)
#korelacni matice
errs=np.sqrt(cov.diagonal())
corx=cov/errs.reshape(4,1)/errs.reshape(1,4)
corx
#porovnani odhadu nejistoty s rozptylem iterovanych experimentu
errs/pars.std(0)[:4]
# generujeme sit parametru 11x11x11x11
npt=11
zbas=slice(-0.05,0.05,1j*npt)
zand=np.mgrid[zbas,zbas,zbas,zbas]
y=ydata(x) # jeden pokus generovani dat
zpars=ahat(y)
alpars=np.array([zpars[i]*(1+zand[i]).ravel() for i in range(4)]).T
#alchis=np.array([(difarr(c)**2).sum() for c in alpars]).reshape(npt,npt,npt,npt)
alpars.min(0),alpars.max(0)
alchi=np.array([((p.dot(amat)-y)**2).sum() for p in alpars])-zpars[-1]
%matplotlib inline
from matplotlib import pyplot as pl
gchi=alchi.reshape(npt,npt,npt,npt)
pl.imshow(gchi.mean(0).mean(2))
pl.colorbar()
alchi.min()
pl.imshow(gchi[6,:,:,6])
pl.colorbar()
pl.plot(pars[:,1],pars[:,3],'.')
pl.xlabel("param. 2")
pl.ylabel("param. 4");
jl=np.r_[:4].astype(int)
allids=np.concatenate([np.r_[[np.ones(3-i)*i,jl[jl>i]]].T for i in jl]).astype(int)
zandz=zand+np.array(zpars[:4]).reshape(4,1,1,1,1)
modmat=list((zand**2).reshape(4,npt**4))
#modmat=list(alpars.T**2)
for ids in allids:
modmat.append((zand[ids[0]]*zand[ids[1]]).ravel())
# modmat.append((alpars[:,ids[0]]*alpars[:,ids[1]]).ravel())
modmat=np.array(modmat)
modmat.shape
allids
pl.plot(gchi.mean(0).mean(2).mean(1))
pl.plot(gchi.mean(0).mean(2).mean(0))
pl.legend(["param. 2","param. 3"])
%matplotlib inline
from matplotlib import pyplot as pl
gchi=alchi.reshape(npt,npt,npt,npt)
pl.imshow(gchi.mean(0).mean(2))
pl.colorbar()
hessx=modmat.dot(modmat.T)
covx=np.linalg.inv(hessx)
parx=covx.dot(modmat.dot(alchi.ravel()))
parx
pl.plot(modmat[0].reshape(npt,npt*npt*npt).mean(1))
parx[:4]*errs**2
modmat.shape
dermat=np.ones((4,4))
dermat[jl,jl]=parx[:4]
for i in range(len(allids)):
dermat[allids[i][0],allids[i][1]]=parx[i+4]/2.
dermat[allids[i][1],allids[i][0]]=parx[i+4]/2.
#(covmat*100).astype(int)
covmat=np.linalg.inv(dermat)
covmat
cov/covmat
gvals=np.r_[a0,b0,c0,d0]
cov/covmat/gvals.reshape(1,4)/gvals.reshape(4,1)
dif = lambda p:((p.dot(amat)-y)**2).sum()
qpars=np.r_[zpars[:4]]
def estmat(msize=1000,npar=4,eran=0.05,loud=0):
pts=np.random.uniform(-eran,eran,size=npar*msize).reshape(npar,msize)
mpts=(1+pts)*qpars[:,np.newaxis]
chi0=dif(qpars)
allchi=[dif(b)-chi0 for b in mpts.T]
if loud==-2: return mpts
if loud==-1: return allchi
if loud: print(min(allchi),max(allchi))
jl=np.r_[:npar].astype(int)
#allids=np.concatenate([np.r_[[np.ones(npar-1-i)*i,jl[jl>i]]].T for i in jl]).astype(int)
#zandz=zand+np.array(zpars[:npar]).reshape(npar,1,1,1,1)
zand=pts
modmat=list((zand**2).reshape(npar,msize))
for ids in allids:
modmat.append((zand[ids[0]]*zand[ids[1]]).ravel())
modmat=np.array(modmat)
if loud==2: return modmat
hessx=modmat.dot(modmat.T)
covx=np.linalg.inv(hessx)
parx=covx.dot(modmat.dot(allchi))
if loud==3: return parx
if loud==4: return parx,((allchi-parx.dot(modmat))**2).sum()
dermat=np.ones((npar,npar))
dermat[jl,jl]=parx[:npar]
for i in range(len(allids)):
dermat[allids[i][0],allids[i][1]]=parx[i+npar]/2.
dermat[allids[i][1],allids[i][0]]=parx[i+npar]/2.
#(covmat*100).astype(int)
covmat=np.linalg.inv(dermat)
return covmat
covmat/estmat(1500)#*gvals.reshape(1,4)*gvals.reshape(4,1)/cov
zpars,ahat(y)
dif = lambda p:((p.dot(amat)-y)**2).sum()
qpars=np.r_[zpars[:4]]
covmat/estmat(100)
covmat/estmat(50)
rep=[]
for j in range(50):
y=ydata(x) # jeden pokus generovani dat
zpars=ahat(y)
dif = lambda p:((p.dot(amat)-y)**2).sum()
qpars=np.r_[zpars[:4]]
newmat=estmat(100)
nerrs=np.sqrt(newmat.diagonal())
newcor=newmat/nerrs.reshape(1,4)/nerrs.reshape(4,1)
iarr=np.r_[[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]]
ia,ib=iarr.T
rep.append(np.r_[nerrs*qpars,newcor[ia,ib]])
arep=np.array(rep).T
arep.mean(1)
arep.std(1)
arep.mean(1)
a=1.51
rho=lambda t:t**2*(abs(t)<a)+a*(abs(t)>a)*abs(t)
phi=lambda t:t*(abs(t)<a)+a*(abs(t)>a)*np.sign(t)
dphi=lambda t: (abs(t)<a)
dif = lambda p:((p.dot(amat)-y)**2).sum()
msel=np.random.uniform(size=y.shape[0])<0.1
y[msel]+=np.random.uniform(-10,10,sum(msel))
from scipy import optimize as op
dif=lambda p:rho(p.dot(amat)-y).sum()
op.fmin(dif,qpars)
dif2=lambda p:rho(p.dot(amat)-y).sum()
npt=11
zoo=np.ones((npt,4))*qpars
zoo[:,2]*=(1+np.r_[-0.015:0.015:1j*npt])
pl.plot((1+np.r_[-0.015:0.015:1j*npt]),[dif2(z) for z in zoo])
dif1=lambda p:((p.dot(amat)-y)**2).sum()
y=ydata(x)
qpars0=np.r_[ahat(y)[:4]]
#y[msel]+=np.random.uniform(-5,5,sum(msel))
dif=dif2
qpars=qpars0
estmat(50,eran=0.02)/covmat
dif=dif2
y[msel]+=np.random.uniform(-5,5,sum(msel))
allchi=np.array(estmat(50,eran=0.02,loud=-1))
allchi.min(),allchi.max()