iode.Equation.estimate_step_wise
- Equation.estimate_step_wise(from_period: str | Period = None, to_period: str | Period = None, lec_condition: str = None, test: str = 'r2') bool[source]
Estimates an equation and searches for the best possible test values for all possible combinations of coefficients.
- Parameters:
- from_periodstr, optional
The starting period for the estimation. Defaults to the starting period of the current Variables sample.
- to_periodstr, optional
The ending period for the estimation. Defaults to the ending period of the current Variables sample.
- lec_conditionstr
Boolean condition written as a LEC expression which is evaluated for each combination of coefficients. Defaults to None (no condition).
- teststr, {r2|fstat}
Test name. It must be either ‘r2’ or ‘fstat’. Defaults to ‘r2’.
- Returns:
- bool
True if the estimation process has converged, False otherwise.
Examples
>>> from iode import SAMPLE_DATA_DIR, equations, scalars, variables, Equation, Scalar >>> equations.load(f"{SAMPLE_DATA_DIR}/fun.eqs") Loading .../fun.eqs 274 objects loaded >>> scalars.load(f"{SAMPLE_DATA_DIR}/fun.scl") Loading .../fun.scl 161 objects loaded >>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var") Loading .../fun.var 394 objects loaded >>> eq_ACAF = Equation("ACAF", "(ACAF / VAF[-1]) := acaf1 + acaf2 * GOSF[-1] + acaf4 * (TIME=1995)") >>> coefficient_names = eq_ACAF.coefficients >>> coefficient_names ['acaf1', 'acaf2', 'acaf4']
Estimate the equation for the test ‘r2’ with no condition
>>> # reset the coefficients >>> scalars[coefficient_names] = Scalar(0.9, 1.) >>> # estimate >>> success = eq_ACAF.estimate_step_wise("1980Y1", "2000Y1") Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 2.4346e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 , r2=0.575200 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 1.75111e-10) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf4 , r2=0.324238 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , r2=-1.058215 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , r2=0.793875 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , r2=0.793875 >>> success True >>> # r2 >>> eq_ACAF.tests["r2"] 0.7938754558563232 >>> # display the estimated coefficients >>> for name in coefficient_names: ... print(f"{name} -> {scalars[name]}") acaf1 -> Scalar(0.0150646, 1, 0.00118455) acaf2 -> Scalar(-6.90855e-06, 1, 1.07873e-06) acaf4 -> Scalar(-0.00915675, 1, 0.00209541)
Estimate the equation for the test ‘fstat’ with no condition
>>> # reset the coefficients >>> scalars[coefficient_names] = Scalar(0.9, 1.) >>> # estimate >>> success = eq_ACAF.estimate_step_wise("1980Y1", "2000Y1", test="fstat") Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 2.4346e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 , fstat=25.726931 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 1.75111e-10) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf4 , fstat=9.116395 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , fstat=-9.768702 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , fstat=34.662926 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , fstat=34.662926 >>> success True >>> # fstat >>> eq_ACAF.tests["fstat"] 34.662925720214844 >>> # display the estimated coefficients >>> for name in coefficient_names: ... print(f"{name} -> {scalars[name]}") acaf1 -> Scalar(0.0150646, 1, 0.00118455) acaf2 -> Scalar(-6.90855e-06, 1, 1.07873e-06) acaf4 -> Scalar(-0.00915675, 1, 0.00209541)
Estimate the equation for the test ‘r2’ with condition (acaf2 > 0)
>>> # write the condition (all coefficients > 0) >>> lec_condition = "acaf2 > 0" >>> lec_condition 'acaf2 > 0' >>> # reset the coefficients >>> scalars[coefficient_names] = Scalar(0.9, 1.) >>> # estimate >>> success = eq_ACAF.estimate_step_wise("1980Y1", "2000Y1", lec_condition) Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 2.4346e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 , r2=0.575200 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 1.75111e-10) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf4 , r2=0.324238 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , r2=-1.058215 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , r2=0.793875 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , r2=-1.058215 >>> success True >>> # r2 >>> eq_ACAF.tests["r2"] -1.0582152605056763 >>> # display the estimated coefficients >>> for name in coefficient_names: ... print(f"{name} -> {scalars[name]}") acaf1 -> Scalar(0, 0, 0) acaf2 -> Scalar(5.76933e-06, 1, 1.26806e-06) acaf4 -> Scalar(-0.0104115, 1, 0.00643766)
Estimate the equation for the test ‘fstat’ with condition (acaf2 > 0)
>>> # reset the coefficients >>> scalars[coefficient_names] = Scalar(0.9, 1.) >>> # estimate >>> success = eq_ACAF.estimate_step_wise("1980Y1", "2000Y1", lec_condition, "fstat") Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 2.4346e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 , fstat=25.726931 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 1.75111e-10) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf4 , fstat=9.116395 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , fstat=-9.768702 Estimating : iteration 1 (||eps|| = 1.55885) Estimating : iteration 2 (||eps|| = 5.30092e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf1 acaf2 acaf4 , fstat=34.662926 Estimating : iteration 1 (||eps|| = 1.27279) Estimating : iteration 2 (||eps|| = 4.13603e-08) Solution reached after 2 iteration(s). Creating results file ... ACAF: scalars : acaf2 acaf4 , fstat=-9.768702 >>> success True >>> # fstat >>> eq_ACAF.tests["fstat"] -9.768701553344727 >>> # display the estimated coefficients >>> for name in coefficient_names: ... print(f"{name} -> {scalars[name]}") acaf1 -> Scalar(0, 0, 0) acaf2 -> Scalar(5.76933e-06, 1, 1.26806e-06) acaf4 -> Scalar(-0.0104115, 1, 0.00643766)