def test_simple_withB(self): x_vars = ['N', 'R'] b_vars = ['W'] x_truth = pn.Series([300., 60.], index=x_vars) x_a = pn.Series([200., 50.], index=x_vars) x_cov = pn.DataFrame( [[200.**2, 0.], [0., 50.**2]], index=x_vars, columns=x_vars, ) b_vars = ['W'] b_param = pn.Series(15, index=b_vars) b_cov = pn.DataFrame([[5**2]], index=b_vars, columns=b_vars) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(pn.concat((x_truth, b_param))) + \ np.random.normal(loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, b_vars=b_vars, b_p=b_param, S_b=b_cov, ) oe.doRetrieval() oe.chiSquareTest() oe.linearityTest() assert np.all( np.isclose(oe.x_op.values, np.array([242.23058144, 58.80698325]))) assert np.all( np.isclose(oe.x_op_err.values, np.array([38.45975929, 2.11433569]))) assert np.all( np.isclose( oe.y_op.values, np.array([ 8.59387481e-05, 2.35930374e-04, 6.25082165e-04, 1.59826496e-03, 3.94383617e-03, 9.39176836e-03, 2.15841176e-02, 4.78717872e-02, 1.02466854e-01, 2.11663310e-01, 4.21955044e-01, 8.11792809e-01, 1.50724078e+00, 2.70071334e+00, 4.67017078e+00, 7.79373174e+00, 1.25521050e+01, 1.95094978e+01, 2.92640235e+01, 4.23623849e+01, 5.91813932e+01, 7.97899855e+01, 1.03817366e+02, 1.30361709e+02, 1.57975050e+02, 1.84750395e+02, 2.08516617e+02, 2.27119478e+02, 2.38740727e+02, 2.42190476e+02, 2.37107902e+02, 2.24023415e+02, 2.04267474e+02, 1.79747741e+02, 1.52646230e+02, 1.25102832e+02, 9.89479086e+01, 7.55273967e+01, 5.56366318e+01, 3.95526479e+01, 2.71361802e+01, 1.79671951e+01, 1.14807503e+01, 7.07976235e+00, 4.21333028e+00, 2.41986283e+00, 1.34126444e+00, 7.17458014e-01, 3.70371013e-01, 1.84516797e-01 ]))) assert np.isclose(oe.dgf, 1.9612330067312422) assert np.all( np.isclose( oe.chi2Results['chi2value'].values, np.array([47.36895951, 32.34974831, 0.21562209, 0.07752859]))) assert np.isclose(oe.trueLinearity, 0.04230584990874298) assert np.all( np.isclose(oe.linearity, np.array([0.029214156840052827, 0.0065951044149309464])))
N = N0 * np.exp(-lam * D**mu) Z = 1e18 * np.sum(N*D**6) return [10*np.log10(Z)] #define names for X and Y x_vars = ["N0log","lam"] y_vars = ["Ze"] #prior knowledge. Note that the provided numbers do not have any scientific meaning. #first guess for X x_ap = [3,4100] #covariance matrix for X x_cov = np.array([[1,0],[0,10]]) #covariance matrix for Y y_cov = np.array([[1]]) #measured observation of Y y_obs = [10] #additional data to forward function forwardKwArgs = {"D":np.logspace(-4,-2,50)} #create optimal estimation object oe = pyOE.optimalEstimation(x_vars, x_ap, x_cov, y_vars, y_cov, y_obs, forward,forwardKwArgs=forwardKwArgs) #run the retrieval oe.doRetrieval( maxIter=10, maxTime=10000000.0) #plot the result oe.plotIterations() plt.savefig("oe_result.png")
def test_simple(self): x_vars = ['N', 'R', 'W'] x_truth = pn.Series([300., 60., 10.], index=x_vars) x_a = pn.Series([200., 50., 15.], index=x_vars) x_cov = pn.DataFrame( [[200.**2, 0., 0.], [0., 50.**2, 0.], [0., 0., 5.**2]], index=x_vars, columns=x_vars, ) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(x_truth) + np.random.normal( loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, ) oe.doRetrieval() oe.chiSquareTest() oe.linearityTest() assert np.all( np.isclose(oe.x_op.values, np.array([213.51862975, 57.92756234, 13.40863054]))) assert np.all( np.isclose(oe.x_op_err.values, np.array([40.63426603, 2.29118537, 2.6609677]))) assert np.all( np.isclose( oe.y_op.values, np.array([ 3.17145649e-06, 1.10062999e-05, 3.65341959e-05, 1.15993429e-04, 3.52243388e-04, 1.02312321e-03, 2.84242260e-03, 7.55309564e-03, 1.91971583e-02, 4.66685743e-02, 1.08514493e-01, 2.41338486e-01, 5.13382485e-01, 1.04455458e+00, 2.03281029e+00, 3.78388728e+00, 6.73682327e+00, 1.14722273e+01, 1.86859835e+01, 2.91111794e+01, 4.33789731e+01, 6.18264567e+01, 8.42839875e+01, 1.09898412e+02, 1.37060838e+02, 1.63497447e+02, 1.86545249e+02, 2.03579028e+02, 2.12499301e+02, 2.12157114e+02, 2.02597144e+02, 1.85048114e+02, 1.61663372e+02, 1.35087208e+02, 1.07967351e+02, 8.25365465e+01, 6.03497901e+01, 4.22066478e+01, 2.82332960e+01, 1.80641665e+01, 1.10547748e+01, 6.47079287e+00, 3.62276965e+00, 1.93999035e+00, 9.93651387e-01, 4.86792795e-01, 2.28102406e-01, 1.02233026e-01, 4.38256322e-02, 1.79696985e-02 ]))) assert np.isclose(oe.dgf, (2.6733916350055233)) assert np.isclose(oe.trueLinearity, 1.1025635773075495) assert np.all( np.isclose( oe.linearity, np.array([ 0.04508784837582387, 0.02804107536933482, 0.0191628880252254 ]))) assert np.all( np.isclose(oe.chi2Results['chi2value'], np.array([67.748733, 41.148962, 2.908089, 0.176378])))
#prior knowledge. Note that the provided numbers do not have any scientific meaning. #first guess for X x_ap = [3, 4100] #covariance matrix for X x_cov = np.array([[1, 0], [0, 10]]) #covariance matrix for Y y_cov = np.array([[1]]) #measured observation of Y y_obs = [10] #additional data to forward function forwardKwArgs = {"D": np.logspace(-4, -2, 50)} #create optimal estimation object oe = pyOE.optimalEstimation(x_vars, x_ap, x_cov, y_vars, y_cov, y_obs, forward, forwardKwArgs=forwardKwArgs) #run the retrieval oe.doRetrieval(maxIter=10, maxTime=10000000.0) #plot the result oe.plotIterations() plt.savefig("oe_result.png")
def test_simple_withB(self): x_vars = ['N', 'R'] b_vars = ['W'] x_truth = pn.Series([300., 60.], index=x_vars) x_a = pn.Series([200., 50.], index=x_vars) x_cov = pn.DataFrame( [[200.**2, 0.], [0., 50.**2]], index=x_vars, columns=x_vars, ) b_vars = ['W'] b_param = pn.Series(15, index=b_vars) b_cov = pn.DataFrame([[5**2]], index=b_vars, columns=b_vars) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(pn.concat((x_truth, b_param))) + \ np.random.normal(loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, b_vars=b_vars, b_p=b_param, S_b=b_cov, ) oe.doRetrieval() chi2passed, chi2value, chi2critical = oe.chiSquareTest() linearity, trueLinearityChi2, trueLinearityChi2Critical = oe.linearityTest( ) print('np', np.__version__) print('pn', pn.__version__) print('pyOE', pyOE.__version__) print('scipy', scipy.__version__) assert np.all( np.isclose(oe.x_op.values, np.array([255.30992222, 58.68130862]))) assert np.all( np.isclose(oe.x_op_err.values, np.array([38.59979542, 2.0071929]))) assert np.all( np.isclose( oe.y_op.values, np.array([ 9.66145500e-05, 2.64647052e-04, 6.99600329e-04, 1.78480744e-03, 4.39431462e-03, 1.04411743e-02, 2.39423053e-02, 5.29835436e-02, 1.13155184e-01, 2.33220288e-01, 4.63891718e-01, 8.90482377e-01, 1.64965245e+00, 2.94929357e+00, 5.08864283e+00, 8.47313942e+00, 1.36158624e+01, 2.11156461e+01, 3.16025417e+01, 4.56455107e+01, 6.36256959e+01, 8.55904756e+01, 1.11116039e+02, 1.39215145e+02, 1.68327329e+02, 1.96417962e+02, 2.21190355e+02, 2.40386232e+02, 2.52122389e+02, 2.55194702e+02, 2.49281652e+02, 2.34999746e+02, 2.13797631e+02, 1.87714060e+02, 1.59055663e+02, 1.30064833e+02, 1.02642934e+02, 7.81729764e+01, 5.74569615e+01, 4.07555802e+01, 2.78990827e+01, 1.84310971e+01, 1.17508929e+01, 7.23017763e+00, 4.29324314e+00, 2.46025669e+00, 1.36061037e+00, 7.26182114e-01, 3.74038011e-01, 1.85927808e-01 ]))) assert np.isclose(oe.dgf, 1.9611398655015124) assert np.isclose(oe.trueLinearity, 0.039634853402863594) assert np.all(np.array(oe.linearity) < 1) assert np.all(chi2passed)
def test_simple(self): x_vars = ['N', 'R', 'W'] x_truth = pn.Series([300., 60., 10.], index=x_vars) x_a = pn.Series([200., 50., 15.], index=x_vars) x_cov = pn.DataFrame( [[200.**2, 0., 0.], [0., 50.**2, 0.], [0., 0., 5.**2]], index=x_vars, columns=x_vars, ) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(x_truth) + \ np.random.normal(loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, ) oe.doRetrieval() chi2passed, chi2value, chi2critical = oe.chiSquareTest() linearity, trueLinearityChi2, trueLinearityChi2Critical = oe.linearityTest( ) print('np', np.__version__) print('pn', pn.__version__) print('pyOE', pyOE.__version__) print('scipy', scipy.__version__) assert np.all( np.isclose(oe.x_op.values, np.array([230.75639479, 58.49351178, 12.32118448]))) assert np.all( np.isclose(oe.x_op_err.values, np.array([42.940902, 2.05667214, 2.4442318]))) assert np.all( np.isclose( oe.y_op.values, np.array([ 8.07132255e-08, 3.57601384e-07, 1.50303019e-06, 5.99308237e-06, 2.26697545e-05, 8.13499728e-05, 2.76937671e-04, 8.94377132e-04, 2.74014386e-03, 7.96416170e-03, 2.19594168e-02, 5.74401494e-02, 1.42535928e-01, 3.35542210e-01, 7.49348775e-01, 1.58757726e+00, 3.19080133e+00, 6.08385266e+00, 1.10045335e+01, 1.88833313e+01, 3.07396995e+01, 4.74716852e+01, 6.95478498e+01, 9.66600005e+01, 1.27445324e+02, 1.59409810e+02, 1.89156040e+02, 2.12931252e+02, 2.27390663e+02, 2.30366793e+02, 2.21401803e+02, 2.01862876e+02, 1.74600622e+02, 1.43267982e+02, 1.11523535e+02, 8.23565110e+01, 5.76956909e+01, 3.83444793e+01, 2.41755486e+01, 1.44598524e+01, 8.20475101e+00, 4.41652793e+00, 2.25533253e+00, 1.09258243e+00, 5.02125028e-01, 2.18919049e-01, 9.05459987e-02, 3.55278561e-02, 1.32246067e-02, 4.66993202e-03 ]))) assert np.isclose(oe.dgf, (2.7132392503933556)) assert np.isclose(oe.trueLinearity, 0.41529831393972894) assert np.all(np.array(oe.linearity) < 1) assert np.all(chi2passed)
def test_simple_withB(self): x_vars = ['N', 'R'] b_vars = ['W'] x_truth = pn.Series([300., 60.], index=x_vars) x_a = pn.Series([200., 50.], index=x_vars) x_cov = pn.DataFrame([[200.**2, 0.], [0., 50.**2]], index=x_vars, columns=x_vars, ) b_vars = ['W'] b_param = pn.Series(15, index=b_vars) b_cov = pn.DataFrame([[5**2]], index=b_vars, columns=b_vars) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(pn.concat((x_truth, b_param))) + \ np.random.normal(loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, b_vars=b_vars, b_p=b_param, S_b=b_cov, ) oe.doRetrieval() oe.chiSquareTest() oe.linearityTest() assert np.all(np.isclose(oe.x_op.values, np.array( [252.7093795, 58.47319368]))) assert np.all(np.isclose(oe.x_op_err.values, np.array( [37.6333103, 1.93583752]))) assert np.all( np.isclose( oe.y_op.values, np.array([1.06378560e-04, 2.90316569e-04, 7.64624051e-04, 1.94349087e-03, 4.76733284e-03, 1.12856574e-02, 2.57831911e-02, 5.68466519e-02, 1.20957147e-01, 2.48379984e-01, 4.92220927e-01, 9.41373507e-01, 1.73748990e+00, 3.09486011e+00, 5.32008016e+00, 8.82579321e+00, 1.41301814e+01, 2.18323330e+01, 3.25544958e+01, 4.68468310e+01, 6.50590746e+01, 8.71954782e+01, 1.12781655e+02, 1.40780137e+02, 1.69590969e+02, 1.97161665e+02, 2.21207909e+02, 2.39517496e+02, 2.50283520e+02, 2.52397869e+02, 2.45639119e+02, 2.30710732e+02, 2.09120439e+02, 1.82929432e+02, 1.54429088e+02, 1.25815181e+02, 9.89225733e+01, 7.50613173e+01, 5.49661574e+01, 3.88448091e+01, 2.64928709e+01, 1.74374700e+01, 1.10763416e+01, 6.78996647e+00, 4.01695841e+00, 2.29342984e+00, 1.26366513e+00, 6.71950008e-01, 3.44826269e-01, 1.70774153e-01]) ) ) assert np.isclose(oe.dgf, 1.9630943621484518) assert np.isclose(oe.chi2, 60.41671208566697) assert np.isclose(oe.trueNonlinearity, 0.02364625922873849) assert np.all( np.isclose( oe.nonlinearity, np.array([1.93185672e-05, 6.04938579e-03]) ) )
def test_simple(self): x_vars = ['N', 'R', 'W'] x_truth = pn.Series([300., 60., 10.], index=x_vars) x_a = pn.Series([200., 50., 15.], index=x_vars) x_cov = pn.DataFrame([[200.**2, 0., 0.], [0., 50.**2, 0.], [0., 0., 5.**2]], index=x_vars, columns=x_vars, ) y_vars = ['z%02i' % i for i in range(50)] y_cov = pn.DataFrame(np.identity(50) * 100**2, index=y_vars, columns=y_vars) np.random.seed(1) y_obs = forward_simple(x_truth) + np.random.normal(loc=0, scale=100, size=50) oe = pyOE.optimalEstimation( x_vars, x_a, x_cov, y_vars, y_obs, y_cov, forward_simple, forwardKwArgs={}, x_truth=x_truth, ) oe.doRetrieval() oe.chiSquareTest() oe.linearityTest() assert np.all(np.isclose(oe.x_op.values, np.array( [216.67732376, 58.16498463, 12.62412649]))) assert np.all(np.isclose(oe.x_op_err.values, np.array( [40.71776776, 2.07152667, 2.51450318]))) assert np.all( np.isclose( oe.y_op.values, np.array([2.69557604e-07, 1.10375135e-06, 4.29823492e-06, 1.59187253e-05, 5.60693694e-05, 1.87820198e-04, 5.98353942e-04, 1.81289754e-03, 5.22381278e-03, 1.43153242e-02, 3.73090348e-02, 9.24753702e-02, 2.17990408e-01, 4.88706293e-01, 1.04197601e+00, 2.11284066e+00, 4.07450649e+00, 7.47278561e+00, 1.30343468e+01, 2.16219701e+01, 3.41114745e+01, 5.11805517e+01, 7.30312399e+01, 9.91086650e+01, 1.27912745e+02, 1.57005639e+02, 1.83280358e+02, 2.03477240e+02, 2.14839926e+02, 2.15731413e+02, 2.06020779e+02, 1.87114697e+02, 1.61623318e+02, 1.32769822e+02, 1.03727515e+02, 7.70704350e+01, 5.44604078e+01, 3.65993362e+01, 2.33918607e+01, 1.42185585e+01, 8.21950397e+00, 4.51892248e+00, 2.36278069e+00, 1.17492779e+00, 5.55645982e-01, 2.49910453e-01, 1.06898080e-01, 4.34865176e-02, 1.68243617e-02, 6.19044403e-03]) ) ) assert np.isclose(oe.dgf, 2.7039260453696317) assert np.isclose(oe.trueNonlinearity, 0.5892181071502255) assert np.all( np.isclose( oe.nonlinearity, np.array([0.0172169, 0.01304629, 0.01061978]) ) ) assert np.isclose(oe.chi2, 64.21108547675885)