Beispiel #1
0
def getexcelmodel(name):
    ''' Creates a model instance from a excel sheet
    SUM is replaced by SUM_EXCEL which is a function in the modelclass 
    
    In the excel formulars this function accepts ordinary operators and SUM in excel sheets 
    
    input:
        :name: Location of a excel sheeet
    
    returns:
        :model: A model instance with the formulars of the excel sheet
        :para: A list of values in the sheet which matches exogeneous variables in the model 

    '''
    eqdic = findequations(name)
    eqdic2 = {i: eq.replace('SUM(', 'SUM_EXCEL(') for i, eq in eqdic.items()}
    fdic = {i: 'Frml xx ' + eqdic2[i] + r' $ ' for i, eq in eqdic2.items()}
    f = '\n'.join([eq for i, eq in fdic.items()])
    _mmodel = mc.model(f)
    zz = findvalues(name)
    para = [z for z in zz if z[0] in _mmodel.exogene
            ]  # find all values which match a exogeneous variable in model
    return _mmodel, para
Beispiel #2
0
            }
            I = np.eye(np.shape(A[0])[0])
            AINV = linalg.inv(I - A[0])
            F = {
                lag: calculate_mat(model, lag, endo=False)
                for lag in val3d.keys()
            }

#        AINV={lag:linalg.inv(I-A[lag]) for lag in {0 , -8} }
#%%
        fmodel = '''
frml <> a = c(-1) + b  + 0.5 *d1 $ 
frml <> d1 = x + 3 * a(-1)+ c **2 + 0.22*a(-1) + 0.55*a(-2) $ 
frml <> d3 = x + 3 * a(-1)+c **3 + 33*x(-2)$  
Frml <> x = 0.5 * a $'''
        mmodel = model(fmodel)
        df = pd.DataFrame(
            {
                'B': [1, 1, 1, 1],
                'C': [1, 2, 3, 4],
                'E': [4, 4, 4, 4]
            },
            index=[2018, 2019, 2020, 2021])
        res = mmodel(df, antal=100)
        matdir = calculate_allmat(mmodel, mmodel.basedf, 2018)
        # Display
        #        for l,m in matdir.items():
        #            print(f'Lag:{l} \n {m} \n')
        xx = get_compagnion(mmodel, mmodel.basedf, 2020)
        zzz = np.linalg.eig(xx)
        eigplot(zzz[0])
Beispiel #3
0
    plusterm = udtryk_parse(f'({rhv}+{delta/2})', funks=model.funks)
    minusterm = udtryk_parse(f'({rhv}-{delta/2})', funks=model.funks)
    plus = itertools.chain.from_iterable(
        [plusterm if t == vterm else [t] for t in rhsterms])
    minus = itertools.chain.from_iterable(
        [minusterm if t == vterm else [t] for t in rhsterms])
    eplus = f'({"".join(tout(t) for t in plus)})'
    eminus = f'({"".join(tout(t) for t in minus)})'
    expression = f'({eplus}-{eminus})/{delta}'
    if not silent:
        print(expression)
    return expression


if __name__ == '__main__':
    #%%
    def recode(condition, yes, no):
        '''Function which recratetes the functionality of @recode from eviews '''
        return yes if condition else no

    ftest = '''\
    a= b*2*c**3
    b = a(-1)
    d = recode(a>3,a(-1))
    c= (a>3)
     '''
    mtest = model(ftest, funks=[recode])
    dmodel = diffmodel(mtest, forcenum=1)
    zz = dmodel.equations
    print(zz)
Beispiel #4
0
    ddd = totdif(m2)
    eee = totdif(m2)
    ddd.totexplain('D2',vtype='all',top=0.8,use='growth');
    eee.totexplain('D2',vtype='all',top=0.8);

    if  False and ( not 'mtotal' in locals() ) :
        # get the model  
        with open(r"models\mtotal.fru", "r") as text_file:
            ftotal = text_file.read()
       
        #get the data 
        base0   = pd.read_pickle(r'data\base0.pc')    
        base    = pd.read_pickle(r'data\base.pc')    
        adve0   = pd.read_pickle(r'data\adve0.pc')      
    #%%    
        mtotal  = mc.model(ftotal)
        
#        prune(mtotal,base)
        #%%    
        baseny        = mtotal(base0   ,'2016q1','2018q4',samedata=False)
        adverseny     = mtotal(adve0   ,'2016q1','2018q4',samedata=True)
    #%%
        diff  = mtotal.exodif()   # exogeneous variables which are different between baseny and adverseny 
    #%%
        assert 1==2  # just for stopping in test situations 
        #%%  
        adverseny            = mtotal(adve0   ,'2016q1','2018q4',samedata=True)   # to makew sure we have the right adverse.
        countries            = {c.split('__')[2]  for c in diff.columns}  # list of countries 
        countryexperiments   = {e: [c for c in diff.columns if ('__'+e+'__') in c]   for e in countries } # dic of experiments 
        assert len(diff.columns) == sum([len(c) for c in countryexperiments.values()]) , 'Not all exogeneous chocks variables are accountet for'
        countryimpact        = attribution(mtotal,countryexperiments,save='countryimpactxx',maxexp=30000,showtime = 1)    
Beispiel #5
0
with open('model/smec.frm', 'rt') as f:
    rsmec0 = f.read()
with open('model/varlist.dat', 'rt') as f:
    vsmec = f.read()

# breakpoint()
rsmec, asmec = rsmec0.split('AFTER $')
esmec00 = re.sub('//.*$', '', rsmec,
                 flags=re.MULTILINE).replace('Dif(', 'diff(')
esmec0 = re.sub('^[" "]*$\n', '', esmec00, flags=re.MULTILINE).replace(
    ';', '$').replace('[', '(').replace(']', ')')
esmec1 = frml_code_translate(esmec0)
esmec2 = mp.normalize(esmec1)
esmec3 = adam_exounroll(esmec2)
fsmec = mp.explode(esmec3)
msmec = model(fsmec)

msmec.normalized = True

fnsmec = mp.un_normalize_model(fsmec)
mnsmec = model(fnsmec)
mnsmec.normalized = False

#%% get the data
e19df = pd.read_excel('data/SMEC_lang.xlsx', index_col=0).T.pipe(
    lambda df: df.rename(columns={v: v.upper()
                                  for v in df.columns}))
# e19df.index = dates
dates = pd.period_range(start=e19df.index[0], end=e19df.index[-1], freq='Y')
baseline = insertModelVar(e19df, [msmec, mnsmec]).fillna(0.0)
Beispiel #6
0
 def makemodel(self, eq, **kwargs):
     self.modelopt = {**self.modelopt, **kwargs}
     self.eq = eq
     self.model = model(self.eq, **kwargs)
     return self.copy()
Beispiel #7
0
 def __getitem__(self, select):
     m = model()
     m.lastdf = self._obj
     #        print(select)
     vars = m.vlist(select)
     return self._obj.loc[:, vars]
Beispiel #8
0
     index=[2017, 2018, 2019, 2020])
 df4 = pd.DataFrame({
     'Z': [223., 333],
     'TY': [203., 303.]
 },
                    index=[2018, 2019])
 ftest = ''' 
     FRMl <>  ii = x+z $
     frml <>  c=0.8*yd $
     FRMl <>  i = ii+iy $ 
     FRMl <>  x = f(2) $ 
     FRMl <>  y = c + i + x+ i(-1)$ 
     FRMl <>  yX = 0.6 * y $
     FRML <>  dogplace = y *4 $'''
 #%%
 m2 = model(ftest, funks=[f])
 aa = df.mf(ftest, funks=[f]).solve()
 aa.mf.solve()
 aa.mf.drawmodel()
 aa.mf.Y.draw()
 aa.mf.istopo
 aa.mf['*']
 df2.mfcalc(ftest, funks=[f])
 df2.mf.Y.draw(all=True, pdf=0)
 #%%
 df4 = pd.DataFrame({
     'Z': [223., 333],
     'YD': [203., 303.]
 },
                    index=[2018, 2019])
 x = df2.mfupdate(df4)
Beispiel #9
0
    res = draw_adjacency_matrix(model.totgraph_nolag,
                                list(chain(model.endogene, model.exogene)),
                                [model.endogene, model.exogene],
                                ['Endogeneous', 'Exogeneous'],
                                title='Dependencies',
                                size=size)
    return res


if __name__ == '__main__':
    #%%
    ftest = ''' FRMl <>  y = c + i + x $ 
FRMl <>  yd = 0.6 * y + y0 $
FRMl <>  c= 0.6 * yd + 0.2 *yd(-1) $ 
FRMl <>  i = I0 $ 
FRMl <>  ii = i *2  $
FRMl <>  x = 2 $  
frml <> dog = y $'''
    mtest = mc.model(ftest)
    draw_adjacency_matrix(mtest.totgraph_nolag)
    plt.show()
    draw_adjacency_matrix(mtest.endograph, mtest.strongorder,
                          mtest.strongblock, mtest.strongtype)
    plt.show()
    #%%
    mtest.draw('Y')
    mtest.drawmodel()
    #%%

    drawendoexo(mtest)
        plt.tight_layout()
        fig.subplots_adjust(top=top)

    plt.show()
    return fig

if __name__ == '__main__' and 1:
    basis = pd.DataFrame([[100, 100.], [-10.0, -12], [12, -10], [-10, 10]],
                         index=['nii', 'cost', 'credit', 'fee'],
                         columns=['ex', 'ex2'])
    basis.loc['total'] = basis.sum()
    waterplot(basis)

    pass
    if (not 'ffrbus' in locals()):
        locfrbus = r'f:\mf modeller\frbus\python'
        base = pd.read_pickle(locfrbus + r'\data\baseline.pc')
        alt = pd.read_pickle(locfrbus + r'/data/alt.pc')

        # get the model
        with open(locfrbus + r"\model\ffrbusvar.txt", "r") as text_file:
            ffrbus = text_file.read()
    mfrbus = mc.model(ffrbus)
    mfrbus.basedf = base
    mfrbus.lastdf = alt
    mfrbus.smpl('2020q1', '2030q4', base)
    _ = mfrbus['rff*'].dif.plot(colrow=1)
    _ = mfrbus['rff'].dif.plot(colrow=1)
    _ = mfrbus[' '.join(['rff'] * 2)].dif.plot(colrow=1, top=0.92)
    _ = mfrbus[' '.join(['rff'] * 10)].dif.plot(colrow=1, top=0.965)
        return res

    def __call__(self, *args, **kwargs):
        return self.targetseek(*args, **kwargs)


#%%
if __name__ == '__main__':
    try:
        # If not already there, get the model and make it into an modelinstance
        if 'mmonas' not in locals():
            with open(r"J:\Udvikling - feedback\Systemfiler\super.fru",
                      "r") as text_file:
                fmonas = text_file.read()
            mmonas = mc.model(fmonas)
        # get the baseline
        grund = pd.read_pickle(
            r'..\Udvikling - feedback\Systemfiler\supergrund.pc')

        #%% Make a copy of the data
        mul = grund.copy(deep=True)

        #%% solve the model. Setbase make sure this is the reference run  when comparing
        yy = mmonas(mul,
                    start='2015q1',
                    s**t='2017q4',
                    first_test=20,
                    conv='FY',
                    silent=True,
                    antal=100,
Beispiel #12
0
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 26 10:12:34 2019

@author: hanseni
"""
import pandas as pd

from modelclass import model 

#%%
if not 'madam' in locals(): 
    bank  = pd.read_pickle('adam/lang100_2017.pc') 
    fadam = open('adam/jul17x.txt','rt').read()
    madam = model(fadam)    
    madam.use_preorder = True
    #%%   

grund = madam(bank,2018,2030,conv='YR UL',max_iterations=100,alfa=0.4,stats=0,ljit=0,chunk=30,
              sim2= 0,debug=1)
mulstart =grund.copy()
mulstart.TG = grund.TG+0.02
mulstart.TFN_O = grund.TFN_O - 10
mul = madam(mulstart,2018,2030,conv='YR UL',max_iterations=100,alfa=0.5,stats=0,ljit=0,chunk=30, relconv = 0.000000001)
                 
# print(madam[['ENL','ul','fy','tg']].dif.df)
# _ = madam.totexplain(pat='fy',top=0.86)
assert 1==1
#%%
solverlist = ['base_sim', 'sim', 'sim1d','newton']   #,'newtonstack']
modeldic  = {solver: {'model':model(fadam)} for solver in solverlist}
Beispiel #13
0
import pandas as pd
import sys
import matplotlib.pyplot as plt
from matplotlib import colors
import seaborn as sns
import numpy as np
from collections import namedtuple
import os

import modelmanipulation as mp
import modelclass as mc
import model_Excel as me
#import databank          as pb
from itertools import groupby, chain

path = 'imf-lcr\\'
if __name__ == '__main__':
    # read from the P: drive and save to local else read from local drive (just to work if not on the network)
    tlcr = open(path + 'lcr.txt').read()  # read the template model
    t2lcr = doable(tlcr)
    #%%

    print(t2lcr)
    flcr = mp.explode(mp.tofrml(t2lcr))
    mlcr = mc.model(flcr)
    mlcr.drawall()

    exp = 'frml ddd ddd %0_ib %1__ha'
    l = ['{abekat}', '{hest}']
    ud = dosubst(l, exp)
Beispiel #14
0
    def __init__(self, files, save=True, savepath='', modelname='testmodel'):
        ''' initialize a model'''

        if type(files) == list:
            modfilenames = [f for f in files if f.endswith('.mod')]
            self.filenames = [
                f for f in modfilenames if not f.endswith('param.mod')
            ]
            self.parafilenames = [
                f for f in modfilenames if f.endswith('param.mod')
            ]

            self.alllines = ''
            self.alllines = ' '.join(
                [open(f, 'rt').read() for f in self.filenames]).upper()
            self.paralines = ' '.join(
                [open(f, 'rt').read() for f in self.parafilenames]).upper()
            self.path, self.modelname = self.filenames[0][:-4].rsplit('\\', 1)
            self.savepath = savepath if savepath else self.path
        else:
            self.alllines = files  # we got a string
            self.savepath = savepath
            self.modelname = modelname

    #%% get rid of comments
        rest = [
            l.split(r'//')[0].strip() for l in self.alllines.split('\n')
            if 0 != len(l.split(r'//')[0].strip())
        ]

        #%% get the model and make it
        restmod = ' '.join(rest)
        modelpat = r'model(.*?)end'.upper()
        modellist = re.findall(modelpat, restmod)  # extract all model segments
        assert len(modellist) == len([l for l in rest if 'model' in l.lower()
                                      ]), 'Some model segments missing'

        lthismodel = [
            f'frml <> {f} $' for f in ''.join(modellist).split(';')
            if 0 != len(f.strip())
        ]
        self.fthismodel = '\n'.join(lthismodel).replace('^', '**')

        assert pt.check_syntax_model(self.fthismodel), 'Syntax error in file'

        self.mthismodel = mc.model(self.fthismodel, modelname=self.modelname)

        #%% get the resmodel
        self.fresmodel = mp.find_res_dynare_new(self.fthismodel)
        self.mresmodel = mc.model(self.fresmodel,
                                  modelname=self.modelname + '_res')

        modelresvar = [v for v in self.mthismodel.allvar if v.endswith('_RES')]
        resmodelresvar = [v for v in self.mresmodel.endogene]
        assert set(modelresvar) == set(
            resmodelresvar
        ), 'Residual model does not match residuals in the model'

        #%% get a model for injecting parameters
        parampat = r'parameters(.*?)(?:(?:model)|(?:var)|(?:exovar)|(?:$))'.upper(
        )
        paramlist = re.findall(parampat, restmod) + [self.paralines]
        assert len(paramlist) - 1 == len([
            l for l in rest if 'parameters'.upper() in l
        ]), 'Some parameter segments missing'

        #new get all the parameter settings and make a model for injecting theese
        self.paravalues = sorted(
            set([
                re.sub(r'\s+', '', f) for f in ''.join(paramlist).split(';')
                if 0 != len(f.strip()) and '=' in f
            ]))
        self.fparamodel = '\n'.join(
            [f'frml <> {f} $' for f in self.paravalues])
        self.mparamodel = mc.model(self.fparamodel,
                                   modelname=self.modelname + '_para')

        # now get all the parameter names:
        llparavars = ' '.join(
            [f.split('=')[0] for f in ''.join(paramlist).split(';')])
        self.paravars = sorted({
            re.sub(r'\s+', '', p)
            for p in llparavars.split(' ') if 0 != len(p.strip())
        })

        ##% Variables
        varpat = r'var[^e](.*?);'.upper()
        varlist = re.findall(varpat, restmod)
        vars = {v for v in ' '.join(varlist).split(' ') if 0 != len(v)}
        assert len(vars) == len(self.mthismodel.endogene
                                ), 'Not all endogenous variables are declared'

        exovarpat = r'varexo(.*?);'.upper()
        exovarlist = re.findall(exovarpat, restmod)
        exovars = {v for v in ' '.join(exovarlist).split(' ') if 0 != len(v)}

        #%% Output model

        self.modout = self.mthismodel.todynare(self.paravars, self.paravalues)

        #%%
        newline = '\n'
        self.inf = (
            f'Model file(s)                             : {self.filenames} \n'
            +
            f'Contemporaneous feedback (simultaneous)   : {not self.mthismodel.istopo} \n'
            +
            f'Var equal to left hand variables          : {self.mthismodel.endogene ==  set(vars)} \n'
            +
            f'Number of endogeneous                     : {len(self.mthismodel.endogene)} \n'
            +
            f'Number of exogenous                       : {len(self.mthismodel.exogene)} \n'
            +
            f'Parameters                                : {len(self.paravars)} \n'
            +
            f'Parameters set to values                  : {len(self.paravalues)} \n'
            +
            f'Exovar not in model                       : { set(exovars)- self.mthismodel.exogene} \n'
            +
            f'Model exogenous not in exovar or parameter: {self.mthismodel.exogene- (set(exovars)|set(self.paravars))} \n'
            +
            f'Parameters also in exovar                 : {set(exovars) & set(self.paravars)} \n'
            +
            f'Parameters not in model                   : \n{newline.join(sorted(set(self.paravars)- self.mthismodel.exogene))} \n'
        )
        #print(inf)
        if save:
            with open(f'{self.savepath}\{self.modelname}.inf',
                      'wt') as inffile:
                inffile.write(self.inf)
            with open(f'{self.savepath}\{self.modelname}.frm',
                      'wt') as frmfile:
                frmfile.write(self.fthismodel)
            with open(f'{self.savepath}\{self.modelname}_para.frm',
                      'wt') as frmfile:
                frmfile.write(self.fparamodel)
            with open(f'{self.savepath}\{self.modelname}_res.frm',
                      'wt') as frmfile:
                frmfile.write(self.fresmodel)
            with open(f'{self.savepath}\{self.modelname}_cons.mod',
                      'wt') as consfile:
                consfile.write(self.modout)