def find_hist_model(equations): ''' takes a unrolled model and create a model which can be run for historic periode \n The model calculates residuals for equations with a <res= > clause and \n and the identities are also calculeted''' hist = [] for f in find_frml(equations): hist.append(find_res(f)) a, fr, n, udtryk = split_frml(f.upper()) if kw_frml_name(n, 'IDENT') or kw_frml_name(n, 'I'): # identites are just replicated in order to calculate backdata hist.append(f) return (' '.join(hist))
def exounroll(in_equations): ''' takes a model and makes a new model by enhancing frml's with <exo=,j=,jr=> in their frml name. :exo: the value can be fixed in to a value valuename_x by setting valuename_d=1 :jled: a additiv adjustment element is added to the frml :jrled: a multiplicativ adjustment element is added to the frml ''' nymodel = [] equations = in_equations[:] # we want do change the e for comment, command, value in find_statements(equations.upper()): # print('>>',comment,'<',command,'>',value) if comment: nymodel.append(comment) elif command == 'FRML': a, fr, n, udtryk = split_frml((command + ' ' + value).upper()) #print(fr,n, kw_frml_name(n.upper(),'EXO')) # we want a relatov adjustment if kw_frml_name(n, 'JRLED') or kw_frml_name(n, 'JR'): lhs, rhs = udtryk.split('=', 1) udtryk = lhs + \ '= (' + rhs[:-1] + ')*(1+' + lhs.strip() + '_JR )' + '$' if kw_frml_name(n, 'JLED') or kw_frml_name(n, 'J'): lhs, rhs = udtryk.split('=', 1) # we want a absolute adjustment udtryk = lhs + '=' + rhs[:-1] + '+' + lhs.strip() + '_J' + '$' if kw_frml_name(n, 'EXO'): lhs, rhs = udtryk.split('=', 1) endogen = lhs.strip() dummy = endogen + '_D' exogen = endogen + '_X' udtryk = lhs + \ '=(' + rhs[:-1] + ')*(1-' + dummy + ')+' + exogen + '*' + dummy + '$' nymodel.append(command + ' ' + n + ' ' + udtryk) else: nymodel.append(command + ' ' + value) equations = '\n'.join(nymodel) return equations
def __init__(self, mmodel, df=None, endovar=None, onlyendocur=False, timeit=False, silent=True, forcenum=False, per='', ljit=0, nchunk=None, endoandexo=False): self.df = df if type(df) == pd.DataFrame else mmodel.lastdf self.endovar = sorted(mmodel.endogene if endovar == None else endovar) self.endoandexo = endoandexo self.mmodel = mmodel self.onlyendocur = onlyendocur self.silent = silent self.maxdif = 9999999999999 self.forcenum = forcenum self.timeit = timeit self.per = per self.ljit = ljit self.nchunk = nchunk print(f'Prepare model til calculate derivatives for Newton solver') self.declared_endo_list0 = [ pt.kw_frml_name(self.mmodel.allvar[v]['frmlname'], 'ENDO', v) for v in self.endovar ] self.declared_endo_list = [ v[:-6] if v.endswith('___RES') else v for v in self.declared_endo_list0 ] # real endogeneous variables self.declared_endo_set = set(self.declared_endo_list) assert len(self.declared_endo_list) == len(self.declared_endo_set) self.placdic = {v: i for i, v in enumerate(self.endovar)} if self.endoandexo: self.exovar = [ v for v in sorted(mmodel.exogene) if not v in self.declared_endo_set ] self.exoplacdic = {v: i for i, v in enumerate(self.exovar)} else: self.exoplacdic = {} # breakpoint() self.diffendocur = self.modeldiff() self.diff_model = self.get_diffmodel()
def un_normalize(frml): '''This function makes sure that all formulas are unnormalized. if the formula is already decorated with <endo=name> this is keept else the lhs_varriable is used in <endo=> ''' frml_name, frml_index, frml_rest = findindex(frml.upper()) this_endo = pt.kw_frml_name(frml_name.upper(), 'ENDO') lhs, rhs = frml_rest.split('=') if this_endo: lhs_var = this_endo.strip() frml_name_out = frml_name else: lhs_var = lhs.strip() frml_name_out = f'<endo={lhs_var}>' if frml_name == '<>' else f'{frml_name[:-1]},endo={lhs_var}>' print(this_endo) new_rest = f'{lhs_var}___res = ( {rhs.strip()} ) - ( {lhs.strip()} )' return f'{frml_name_out} {frml_index if len(frml_index) else ""} {new_rest}'
def find_res(f): ''' Finds the expression which calculates the residual in a formel. FRML <res=a,endo=b> x=a*b+c $ ''' from sympy import solve, sympify a, fr, n, udtryk = split_frml(f.upper()) udres = '' tres = kw_frml_name(n, 'RES') if tres: lhs, rhs = udtryk.split('=', 1) res = lhs.strip() + '_J' if tres == 'J' else lhs.strip() + \ '_JR' if tres == 'JR' else tres # we take the the $ out kat = sympify('Eq(' + lhs + ',' + rhs[0:-1] + ')') res_frml = sympify('res_frml') res_frml = solve(kat, res) udres = 'FRML ' + 'RES' + ' ' + \ res.ljust(25) + ' = ' + str(res_frml)[1:-1] + ' $' return udres
def __init__(self, mmodel, df=None, endovar=None, onlyendocur=False, timeit=False, silent=True, forcenum=False, per='', ljit=0, nchunk=None, endoandexo=False): """ Args: mmodel (TYPE): Model to analyze. df (TYPE, optional): Dataframe. if None mmodel.lastdf will be used endovar (TYPE, optional): if set defines which endogeneous to include . Defaults to None. onlyendocur (TYPE, optional): Only calculate for the curren endogeneous variables. Defaults to False. timeit (TYPE, optional): writeout time informations . Defaults to False. silent (TYPE, optional): Defaults to True. forcenum (TYPE, optional): Force differentiation to be numeric else try sumbolic (slower) Defaults to False. per (TYPE, optional): Period for which to calculte the jacobi . Defaults to ''. ljit (TYPE, optional): Trigger just in time compilation of the differential coiefficient. Defaults to 0. nchunk (TYPE, optional): Chunks for which the model is written - relevant if ljit == True. Defaults to None. endoandexo (TYPE, optional): Calculate for both endogeneous and exogeneous . Defaults to False. Returns: None. """ self.df = df if type(df) == pd.DataFrame else mmodel.lastdf self.endovar = sorted(mmodel.endogene if endovar == None else endovar) self.endoandexo = endoandexo self.mmodel = mmodel self.onlyendocur = onlyendocur self.silent = silent self.maxdif = 9999999999999 self.forcenum = forcenum self.timeit = timeit self.per = per self.ljit = ljit self.nchunk = nchunk if not self.silent: print(f'Prepare model for calculate derivatives for Newton solver') self.declared_endo_list0 = [ pt.kw_frml_name(self.mmodel.allvar[v]['frmlname'], 'ENDO', v) for v in self.endovar ] self.declared_endo_list = [ v[:-6] if v.endswith('___RES') else v for v in self.declared_endo_list0 ] # real endogeneous variables self.declared_endo_set = set(self.declared_endo_list) assert len(self.declared_endo_list) == len(self.declared_endo_set) self.placdic = {v: i for i, v in enumerate(self.endovar)} self.newmodel = mmodel.__class__ if self.endoandexo: self.exovar = [ v for v in sorted(mmodel.exogene) if not v in self.declared_endo_set ] self.exoplacdic = {v: i for i, v in enumerate(self.exovar)} else: self.exoplacdic = {} # breakpoint() self.diffendocur = self.modeldiff() self.diff_model = self.get_diffmodel()
'where': ['land', 'sea'] }, 'we have brought you your {weed} from {where} ') sub_frml({ 'weed': ['scrubbery', 'herring'], 'where': ['land', 'sea'] }, 'we have brought you your {weed} from {where} ') a = {'bankdic': {'bank': ['Danske', 'Nordea'], 'danske': ['yes', 'no']}} sub_frml(a['bankdic'], 'Dette er {bank}') sub_frml(a['bankdic'], 'Dette er {bank}', sep=' and ') sub_frml(a['bankdic'], 'Dette er {bank}', plus='+', sep='') sub_frml(a['bankdic'], 'Dette er {bank}', xvar='danske', lig='yes') sub_frml(a['bankdic'], 'Dette er {bank}', xvar='danske', lig='no') sub_frml(a['bankdic'], 'Dette er {bank}') list_extract( 'list bankdic = bank : Danske , Nordea / danske : yes , no $') kw_frml_name('<res=abe>', 'res') kw_frml_name('<res=abe,animal>', 'animal') find_res('FRML <res=x> ib =x+y+v $') find_res('FRML <res=J> ib =x+y+v + ib_j $') find_res('FRML <res=JR> ib =(x+y+v + ib_j)*(1+ ib_JR) $)') find_hist_model('FRML <res=x> ib =x+y+v $ frml <ident> y=c+i+x-m $') (exounroll('frml <j> ib=x+y $ frml <jr> ib=x+y $ frml <j,jr')) find_arg('log', 'x=log(a+b)+77') find_arg('log', 'x=log(a+log(b))+77') #%% x = (dounloop('''list bankdic = bank : Danske , Nordea / ko : yes , no $ do bankdic ko = no $ frml x {bank}_income = {bank}_a +{bank}_b $ enddo $ frml x ialt=sum(bankdic,{bank}_income $'''))