def fit_ureals(self, coeffs, cov, dof, label): """ Takes the coefficients, *coeffs*, and covariances, *cov*, together with degrees of freedom, *dof*, in the array output from *fit* and returns a list of function coefficients as correlated GTC ureals. The ureal is named with *label*. If one of the uncertainties is zero, a set of unlinked ureals is created. This should only occur if a transformer has had all errors set to zero (e.g. no VT) or the input data has been fabricated to be a perfect fit. """ uncert = [] for i in range(len(coeffs)): uncert.append(np.sqrt(cov[i, i])) flag = 1 #assume no zero uncertainties for x in uncert: if x == 0.0: flag = 0 if flag == 1: a = gtc.multiple_ureal(coeffs, uncert, dof, label) # and now set the correlations between all coefficients for i in range(len(coeffs)): for j in range(i): gtc.set_correlation(cov[i, j]/np.sqrt(cov[i, i]*cov[j, j]), a[j], a[i]) else: #have a zero uncertainty so create separate ureals print 'A fit has returned zero uncertainty for a function coefficient. This should only occur if input data has been purposely set to a perfect fit.' a = [] for i in range(len(coeffs)): a.append(gtc.ureal(coeffs[i], uncert[i], dof, label[i])) return a
result = minimize(fitfun,[0.05,0.001])#, method = 'Nelder-Mead')#need to start with good guess hessian = result.hess_inv #inverse Hessian is estimate of variance/covariance matrix sum_square = result.fun#final rss #scaling the inverse hessian by multiplying by the residuals sa0 = math.sqrt(hessian[0,0]*sum_square) sa1 = math.sqrt(hessian[1,1]*sum_square) a = result.x ## zs = GTC.ucomplex(a[0]+1j*a[1],(hessian[0,0]*sum_square,hessian[0,1]*sum_square,hessian[1,0]*sum_square,hessian[1,1]*sum_square),138) zs = GTC.ucomplex(a[0]+1j*a[1],(hessian[0,0]*sum_square,hessian[1,1]*sum_square),df=138,label='zs') #use fitted impedance coefficients to define behviour of core impedance #should look at GTC output when model inputs have uncertainty from the fits a1 = GTC.ureal(11.7204144319303,0.346683597109428,df=34,label ='a1',dependent = True) a2 = GTC.ureal(-0.0273771774408702,0.0136282110753501,df=34,label ='a2',dependent = True) a3 = GTC.ureal(29.7364068940134,0.695696459888704,df=34,label ='a3',dependent = True) GTC.set_correlation(-0.826126693986887, a1, a2) GTC.set_correlation(-0.881067593505553, a1, a3) GTC.set_correlation(0.551078899199967, a2, a3) a4 = GTC.ureal(3.90272331971033,0.0859519946028212,34,label ='a4',dependent = True) a5 = GTC.ureal(0.000243735060998679,0.0000527749402101903,34,label ='a5',dependent = True) a6 = GTC.ureal(5.8544055409592,0.501146466659668,34,label ='a6',dependent = True) GTC.set_correlation(-0.679730943215757, a4, a5) GTC.set_correlation(-0.562016044432279, a4, a6) GTC.set_correlation(0.303221134084321, a5, a6) this_core_gtc = [a1,a2,a3,a4,a5,a6] this_CT_gtc = modelCT.CT(this_core_gtc) x = GTC.ureal(5,0.05,df=5,label='excitation') #excitation set with a precision of 1% core_imp = this_CT_gtc.coreZgtc(x, 50) ## bdn = GTC.ucomplex(0.16 +1j*0.12,(0.01,0.01),df=5, label='burden')