iode.Equations.__setitem__
- Equations.__setitem__(key: str | List[str], value: str | Equation | Dict[str, Any] | List[str | Equation | Dict[str, Any]])[source]
Update/add a (subset of) equation(s) referenced by key from/to the Equations database.
The key can represent a single object name (e.g. “ACAF”) or a list of object names (“ACAF;ACAG;AOUC”) or a pattern (e.g. “A*”) or a list of sub-patterns (e.g. “A*;*_”).
If the key represents a list of object names or of sub-patterns, each name or sub-pattern is separated by a separator character which is either a whitespace ` , or a comma `,, or a semi-colon ;, or a tabulation t, or a newline n.
A (sub-)`pattern` is a list of characters representing a group of object names. It includes some special characters which have a special meaning:
* : any character sequence, even empty
? : any character (one and only one)
@ : any alphanumerical char [A-Za-z0-9]
& : any non alphanumerical char
| : any alphanumeric character or none at the beginning and end of a string
! : any non-alphanumeric character or none at the beginning and end of a string
`` : escape the next character
Note that the key can contain references to IODE lists which are prefixed with the symbol $.
- Parameters:
- key: str or list(str)
(the list of) name(s) of the equation(s) to update/add. The list of equations to update/add can be specified by a pattern or by a list of sub-patterns (e.g. “A*;*_”).
- value: str or Equation or dict(str, …) or pandas.DataFrame or Equations
If str, then it is interpreted as the LEC of the equation. The key ‘endogenous’ is added automatically and does not need to be specified. If pandas DataFrame, then only the columns ‘endogenous’, ‘lec’, ‘method’, ‘sample’, ‘block’, ‘instruments’ and ‘comment’ are used. The columns ‘endogenous’ and ‘lec’ are mandatory. The tests values as well the (estimation) date of equations are set/updated when performing the estimation.
See also
Examples
>>> import pandas as pd >>> from iode import SAMPLE_DATA_DIR >>> from iode import equations, variables, Equation, EqMethod >>> equations.load(f"{SAMPLE_DATA_DIR}/fun.eqs") Loading .../fun.eqs 274 objects loaded >>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var") Loading .../fun.var 394 objects loaded
>>> # a) add one equation >>> equations["BDY"] = "BDY := YN - YK" >>> equations["BDY"] Equation(endogenous = 'BDY', lec = 'BDY := YN - YK', method = 'LSQ', from_period = '1960Y1', to_period = '2015Y1')
>>> # b) update one equation >>> equations["ACAF"] Equation(endogenous = 'ACAF', lec = '(ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+\nacaf4*(TIME=1995)', method = 'LSQ', from_period = '1980Y1', to_period = '1996Y1', block = 'ACAF', tests = {corr = 1, dw = 2.32935, fstat = 32.2732, loglik = 83.8075, meany = 0.00818467, r2 = 0.821761, r2adj = 0.796299, ssres = 5.19945e-05, stderr = 0.00192715, stderrp = 23.5458, stdev = 0.0042699}, date = '12-06-1998') >>> # update only the LEC >>> equations["ACAF"] = "(ACAF/VAF[-1]) := acaf1 + acaf2 * GOSF[-1] + acaf4 * (TIME=1995)" >>> equations["ACAF"] Equation(endogenous = 'ACAF', lec = '(ACAF/VAF[-1]) := acaf1 + acaf2 * GOSF[-1] + acaf4 * (TIME=1995)', method = 'LSQ', from_period = '1980Y1', to_period = '1996Y1', block = 'ACAF', tests = {corr = 1, dw = 2.32935, fstat = 32.2732, loglik = 83.8075, meany = 0.00818467, r2 = 0.821761, r2adj = 0.796299, ssres = 5.19945e-05, stderr = 0.00192715, stderrp = 23.5458, stdev = 0.0042699}, date = '12-06-1998') >>> # update block and sample of a block of equations to estimation (dictionary) >>> estim_sample = "2000Y1:2010Y1" >>> block = "ACAF; ACAG; AOUC" >>> for eq_name in block.split(';'): ... equations[eq_name] = {"sample": estim_sample, "block": block} >>> (equations["ACAF"].sample, equations["ACAG"].sample, equations["AOUC"].sample) (Sample("2000Y1:2010Y1"), Sample("2000Y1:2010Y1"), Sample("2000Y1:2010Y1")) >>> (equations["ACAF"].block, equations["ACAG"].block, equations["AOUC"].block) ('ACAF; ACAG; AOUC', 'ACAF; ACAG; AOUC', 'ACAF; ACAG; AOUC') >>> # update lec, method, sample and block >>> eq = equations["ACAF"] >>> eq.lec = "(ACAF/VAF[-1]) := acaf2 * GOSF[-1] + acaf4 * (TIME=1995)" >>> eq.method = EqMethod.MAX_LIKELIHOOD >>> eq.sample = "1990Y1:2015Y1" >>> eq.block = "ACAF" >>> equations["ACAF"] Equation(endogenous = 'ACAF', lec = '(ACAF/VAF[-1]) := acaf2 * GOSF[-1] + acaf4 * (TIME=1995)', method = 'MAX_LIKELIHOOD', from_period = '1990Y1', to_period = '2015Y1', block = 'ACAF', tests = {corr = 1, dw = 2.32935, fstat = 32.2732, loglik = 83.8075, meany = 0.00818467, r2 = 0.821761, r2adj = 0.796299, ssres = 5.19945e-05, stderr = 0.00192715, stderrp = 23.5458, stdev = 0.0042699}, date = '12-06-1998')
>>> # c) add/update several equations at once >>> # 1) using a dict of values >>> eq_ACAF = Equation("ACAF", "(ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+ acaf4*(TIME=1995)", ... method=EqMethod.ZELLNER, from_period='1980Y1', to_period='1996Y1') >>> eq_ACAG = Equation("ACAG", "ACAG := ACAG[-1]+r VBBP[-1]+(0.006*VBBP[-1]*(TIME=2001)-0.008*(TIME=2008))", ... method=EqMethod.ZELLNER, from_period='1980Y1', to_period='1996Y1') >>> eq_AOUC = Equation("AOUC", "AOUC:=((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VAFF+VM))[-1]", ... method=EqMethod.ZELLNER, from_period='1980Y1', to_period='1996Y1') >>> values = {"ACAF": eq_ACAF, "ACAG": eq_ACAG, "AOUC": eq_AOUC} >>> equations["ACAF, ACAG, AOUC"] = values >>> equations["ACAF, ACAG, AOUC"] Workspace: Equations nb equations: 3 filename: ...fun.eqs name lec method sample ... ACAF (ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+ acaf4*(TIME=1995) ZELLNER 1980Y1:1996Y1 ... ACAG ACAG := ACAG[-1]+r VBBP[-1]+(0.006*VBBP[-1]*(TIME=2001)-0.008*(TIME=2008)) ZELLNER 1980Y1:1996Y1 ... AOUC AOUC:=((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VAFF+VM))[-1] ZELLNER 1980Y1:1996Y1 ...
>>> # 2) using a pandas DataFrame >>> data = [] >>> data.append(["ACAF", "(ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+ acaf4*(TIME=1995)", "GLS", '1980Y1:1996Y1']) >>> data.append(["ACAG", "ACAG := ACAG[-1]+r VBBP[-1]+(0.006*VBBP[-1]*(TIME=2001)-0.008*(TIME=2008))", "GLS", '1980Y1:1996Y1']) >>> data.append(["AOUC", "AOUC:=((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VAFF+VM))[-1]", "GLS", '1980Y1:1996Y1']) >>> df = pd.DataFrame(data, index=["ACAF", "ACAG", "AOUC"], columns=["endogenous", "lec", "method", "sample"]) >>> equations["ACAF, ACAG, AOUC"] = df >>> equations["ACAF, ACAG, AOUC"] Workspace: Equations nb equations: 3 filename: ...fun.eqs name lec method sample ... ACAF (ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+ acaf4*(TIME=1995) GLS 1980Y1:1996Y1 ... ACAG ACAG := ACAG[-1]+r VBBP[-1]+(0.006*VBBP[-1]*(TIME=2001)-0.008*(TIME=2008)) GLS 1980Y1:1996Y1 ... AOUC AOUC:=((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VAFF+VM))[-1] GLS 1980Y1:1996Y1 ...
>>> # 3) using another Equations database (subset) >>> equations_subset = equations["ACAF, ACAG, AOUC"].copy() >>> for eq_name in equations_subset.names: ... equations_subset[eq_name].method = EqMethod.MAX_LIKELIHOOD >>> equations["ACAF, ACAG, AOUC"] = equations_subset >>> equations["ACAF, ACAG, AOUC"] Workspace: Equations nb equations: 3 filename: ...fun.eqs name lec method sample ... ACAF (ACAF/VAF[-1]) :=acaf1+acaf2*GOSF[-1]+ acaf4*(TIME=1995) MAX_LIKELIHOOD 1980Y1:1996Y1 ... ACAG ACAG := ACAG[-1]+r VBBP[-1]+(0.006*VBBP[-1]*(TIME=2001)-0.008*(TIME=2008)) MAX_LIKELIHOOD 1980Y1:1996Y1 ... AOUC AOUC:=((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VAFF+VM))[-1] MAX_LIKELIHOOD 1980Y1:1996Y1 ...
>>> # d) working on a subset >>> # 1) get subset >>> equations_subset = equations["A*"] >>> equations_subset.names ['ACAF', 'ACAG', 'AOUC'] >>> # 2) add a equation to the subset >>> equations_subset["AOUC_"] = "AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1] + PM * (VM/(VAFF+VM))[-1]" >>> equations_subset["AOUC_"] Equation(endogenous = 'AOUC_', lec = 'AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1] + PM * (VM/(VAFF+VM))[-1]', method = 'LSQ', from_period = '1960Y1', to_period = '2015Y1') >>> # --> new equation also appears in the global workspace >>> "AOUC_" in equations True >>> equations["AOUC_"] Equation(endogenous = 'AOUC_', lec = 'AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1] + PM * (VM/(VAFF+VM))[-1]', method = 'LSQ', from_period = '1960Y1', to_period = '2015Y1') >>> # 3) update a equation in the subset >>> equations_subset["AOUC_"] = "AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1]" >>> equations_subset["AOUC_"] Equation(endogenous = 'AOUC_', lec = 'AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1]', method = 'LSQ', from_period = '1960Y1', to_period = '2015Y1') >>> # --> equation is also updated in the global workspace >>> equations["AOUC_"] Equation(endogenous = 'AOUC_', lec = 'AOUC_ := ((WCRH/QL)/(WCRH/QL)[1990Y1]) * (VAFF/(VM+VAFF))[-1]', method = 'LSQ', from_period = '1960Y1', to_period = '2015Y1')