datasciencecampus / obr-macro-model

A Python translation of the Office for Budget Responsiblity's (OBR's) UK macroeconomic model
MIT License
2 stars 0 forks source link

Dealing with equations from an OBR model specification #2

Open aeturrell opened 1 year ago

aeturrell commented 1 year ago

Currently, equations are available in Eviews format and are downloaded into data/model_spec as a CSV file that looks like:

number,variable,model_identifier,ons_identifier_code,equation,equation_type,group,list_ons_identifiers
1,HH (&NPISH) final consumption expenditure,CONS,ABJR+HAYO,dlog(CONS)  =  - 0.0978  * log(CONS(-1))  + 0.098  * log(RHHDI(-1))  + 0.137  * dlog(RHHDI)  + 0.190  * (dlog(GPW)  - dlog(PCE))  - 0.0096  * d(LFSUR)  - 0.00159  * d(R(-1))  - 0.00277,Econometrically estimated,Group 1: Consumption,"['ABJR', 'HAYO']"
2,Nominal HH (&NPISH) final consumption expenditure,CONSPS,RPQM,CONSPS  = CONS  * PCE  / 100,Identity,Group 1: Consumption,['RPQM']
3,HH final consumption expenditure: durable goods (CVM),CDUR,UTID,"dlog(CDUR)  = dlog(CONS)  - 0.6408491  * (dlog(PCDUR)  - dlog(PCE))  + 0.0378296  * dlog(PD)  + 0.4517152  * dlog(RHHDI)  + 0.3438288  * dlog(RHHDI(-1))  - 0.0421498  * log(CDUR(-1)  / CONS(-1))  - 0.0145656  * log(PCDUR(-1)  * ((((1  + R(-1)  / 100)^0.25)  - 1)  + ((1.25^0.25)  - 1)  - d(PCDUR(-1))  / PCDUR(-1))  / 100)  + 0.0313983  * log(NFWPE(-1)  / (PCE(-1)  / 100))  - 0.6203775  + 0.0636941  * ( @recode(@date  = @dateval(""2009:04"")  , 1  , 0)  - @recode(@date  = @dateval(""2010:01"")  , 1  , 0) )",Econometrically estimated,Group 1: Consumption,['UTID']
4,HH final consumption expenditure: durable goods (CP),CDURPS,UTIB,CDURPS  = (PCDUR  / 100)  * CDUR,Identity,Group 1: Consumption,['UTIB']

Should these Eviews equations be automatically turned into Python code through some natural language processing, or should they be manually re-coded?

ChatGPT3 had a pretty good first attempt at this. I asked it to convert the first equation from above and it came up with:

import numpy as np

def calculate_expression(CONS, RHHDI, GPW, PCE, LFSUR, R):
  return (0.0978 * np.log(CONS(-1))) + (0.098 * np.log(RHHDI(-1))) + (0.137 * np.log(np.diff(RHHDI))) + (0.190 * (np.log(np.diff(GPW)) - np.log(np.diff(PCE)))) - (0.0096 * np.diff(LFSUR)) - (0.00159 * np.diff(R(-1))) - 0.00277

While this isn't correct, it does inspire some more thought about the feasibility of automatic conversion. Note that the equations are complicated by needing, in some cases eg the $\Delta$ and $t-1$ operators, a vector of values.

aeturrell commented 1 year ago

pyparsing might be helpful for this, and there's an example that parses basic arithmetic that might be a good starting point. (Someone has packaged up some of this in plusminus, but it doesn't seem very maintained.)