Esempio n. 1
0
class newmodel(model):
    def sim2d(self,
              databank,
              start='',
              s**t='',
              silent=0,
              samedata=0,
              alfa=1.0,
              stats=False,
              first_test=1,
              antal=1,
              conv=[],
              absconv=0.01,
              relconv=0.00001,
              dumpvar=[],
              ldumpvar=False,
              dumpwith=15,
              dumpdecimal=5,
              chunk=1_000_000,
              ljit=False,
              fairopt={'fairantal': 1},
              **kwargs):
        '''Evaluates this model on a databank from start to s**t (means end in Danish). 
        
        First it finds the values in the Dataframe, then creates the evaluater function through the *outeval* function 
        (:func:`modelclass.model.fouteval`) 
        then it evaluates the function and returns the values to a the Dataframe in the databank.
        
        The text for the evaluater function is placed in the model property **make_los_text** 
        where it can be inspected 
        in case of problems.         
        
        '''
        starttimesetup = time.time()
        fairantal = {**fairopt, **kwargs}.get('fairantal', 1)
        sol_periode = self.smpl(start, s**t, databank)
        if self.maxlag and not (self.current_per[0] +
                                self.maxlag) in databank.index:
            print(
                '***** Warning: You are solving the model before all lags are avaiable'
            )
            print('Maxlag:', self.maxlag, 'First solveperiod:',
                  self.current_per[0], 'First dataframe index',
                  databank.index[0])
            sys.exit()

        if not silent: print('Will start calculating: ' + self.name)
        if not samedata or not hasattr(self, 'solve2d'):
            if (not hasattr(self, 'solve2d')) or (not self.eqcolumns(
                    self.genrcolumns, databank.columns)):
                databank = insertModelVar(
                    databank, self)  # fill all Missing value with 0.0
                for i in [
                        j for j in self.allvar.keys()
                        if self.allvar[j]['matrix']
                ]:
                    databank.loc[:, i] = databank.loc[:, i].astype(
                        'O'
                    )  #  Make sure columns with matrixes are of this type

                self.make_los_text2d = self.outsolve2dcunk(databank,
                                                           chunk=chunk,
                                                           ljit=ljit,
                                                           debug=kwargs.get(
                                                               'debug', 1))
                exec(self.make_los_text2d,
                     globals())  # creates the los function
                self.pro2d, self.solve2d, self.epi2d = make_los(
                    self.funks, self.errfunk)

        values = databank.values.copy()  #
        self.genrcolumns = databank.columns.copy()
        self.genrindex = databank.index.copy()

        convvar = [conv.upper()] if isinstance(
            conv,
            str) else [c.upper()
                       for c in conv] if conv != [] else list(self.endogene)
        convplace = [databank.columns.get_loc(c)
                     for c in convvar]  # this is how convergence is measured
        convergence = True

        if ldumpvar:
            self.dumplist = []
            self.dump = convvar if dumpvar == [] else self.vlist(dumpvar)
            dumpplac = [databank.columns.get_loc(v) for v in self.dump]

        ittotal = 0
        endtimesetup = time.time()

        starttime = time.time()
        for fairiteration in range(fairantal):
            if fairantal >= 2:
                print(f'Fair-Taylor iteration: {fairiteration}')
            for self.periode in sol_periode:
                row = databank.index.get_loc(self.periode)

                if ldumpvar:
                    self.dumplist.append([fairiteration, self.periode,
                                          int(0)] +
                                         [values[row, p] for p in dumpplac])

                itbefore = [values[row, c] for c in convplace]
                self.pro2d(values, values, row, alfa)
                for iteration in range(antal):
                    self.solve2d(values, values, row, alfa)
                    ittotal += 1

                    if ldumpvar:
                        self.dumplist.append(
                            [fairiteration, self.periode,
                             int(iteration + 1)] +
                            [values[row, p] for p in dumpplac])
                    if iteration > first_test:
                        itafter = [values[row, c] for c in convplace]
                        convergence = True
                        for after, before in zip(itafter, itbefore):
                            #                        print(before,after)
                            if before > absconv and abs(after - before) / abs(
                                    before) > relconv:
                                convergence = False
                                break
                        if convergence:
                            break
                        else:
                            itbefore = itafter
                self.epi2d(values, values, row, alfa)

                if not silent:
                    if not convergence:
                        print(
                            f'{self.periode} not converged in {iteration} iterations'
                        )
                    else:
                        print(
                            f'{self.periode} Solved in {iteration} iterations')

        if ldumpvar:
            self.dumpdf = pd.DataFrame(self.dumplist)
            del self.dumplist
            self.dumpdf.columns = ['fair', 'per', 'iteration'] + self.dump
            if fairantal <= 2: self.dumpdf.drop('fair', axis=1, inplace=True)

        outdf = pd.DataFrame(values,
                             index=databank.index,
                             columns=databank.columns)

        if stats:
            numberfloats = self.calculate_freq[-1][1] * ittotal
            endtime = time.time()
            self.simtime = endtime - starttime
            self.setuptime = endtimesetup - starttimesetup
            print(
                f'Setup time (seconds)                 :{self.setuptime:>15,.2f}'
            )
            print(
                f'Foating point operations             :{self.calculate_freq[-1][1]:>15,}'
            )
            print(f'Total iterations                     :{ittotal:>15,}')
            print(f'Total floating point operations      :{numberfloats:>15,}')
            print(
                f'Simulation time (seconds)            :{self.simtime:>15,.2f}'
            )
            if self.simtime > 0.0:
                print(
                    f'Floating point operations per second : {numberfloats/self.simtime:>15,.1f}'
                )

        if not silent: print(self.name + ' solved  ')
        return outdf
Esempio n. 2
0
if __name__ == '__main__' :
    #%%
    # running withe the mtotal model 
    df2 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,20.,30.,40.] ,'YD':[10.,20.,30.,40.]},index=[2017,2018,2019,2020])
    df3 = pd.DataFrame({'Z':[1., 22., 33,43] , 'TY':[10.,40.,60.,10.] ,'YD':[10.,49.,36.,40.]},index=[2017,2018,2019,2020])
    ftest = ''' 
    FRMl <>  ii = TY(-1)+c(-1)+Z*c(-1) $
    frml <>  c=0.8*yd+log(1) $
    frml <>  d = c +2*ii(-1) $
   frml <>  c2=0.8*yd+log(1) $
    frml <>  d2 = c + 42*ii $
   frml <>  c3=0.8*yd+log(1) $
    frml <>  d3 = c +ii $
 '''
    m2=newmodel(ftest,straight=True,modelname='m2 testmodel')
    df2=mc.insertModelVar(df2,m2)
    df3=mc.insertModelVar(df3,m2)
    z1 = m2(df2)
    z2 = m2(df3)
    ccc = m2.totexplain(pat='D2',per=2019,vtype='all',top=0.8)
    ccc = m2.totexplain('D2',vtype='last',top=0.8)
    ccc = m2.totexplain('D2',vtype='per',top=0.8)
#%%        
    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:
Esempio n. 3
0
        '''
        starttimesetup = time.time()
        fairantal = {**fairopt, **kwargs}.get('fairantal', 1)
        sol_periode = self.smpl(start, s**t, databank)
        if self.maxlag and not (self.current_per[0] +
                                self.maxlag) in databank.index:
            print(
                '***** Warning: You are solving the model before all lags are avaiable'
            )
            print('Maxlag:', self.maxlag, 'First solveperiod:',
                  self.current_per[0], 'First dataframe index',
                  databank.index[0])
            sys.exit()

        self.findpos()
        databank = insertModelVar(databank,
                                  self)  # fill all Missing value with 0.0

        with ttimer('create stuffer and gauss lines ', timeon) as t:
            if (not hasattr(self, 'stuff3')) or (not self.eqcolumns(
                    self.simcolumns, databank.columns)):
                self.stuff3, self.saveeval3 = self.createstuff3(databank)
                self.simcolumns = databank.columns.copy()

        with ttimer('Create solver function', timeon) as t:
            if ljit:
                if not hasattr(self, 'solve1d_jit'):
                    self.make_los_text1d = self.outsolve1dcunk(
                        chunk=chunk, ljit=ljit, debug=kwargs.get('debug', 1))
                    exec(self.make_los_text1d,
                         globals())  # creates the los function
                    self.pro1d_jit, self.solve1d_jit, self.epi1d_jit = make_los(
Esempio n. 4
0
    def run(b):
        nonlocal firstrun
        nonlocal altname
        # mulstart       = model.basedf.copy()
        mulstart = insertModelVar(basedf, model)
        model.smpl(df=mulstart)

        # First update from the sliders
        if lslidedef:
            for i, (des, cont) in enumerate(slidedef.items()):
                op = cont.get('op', '=')
                var = cont['var']
                for var in cont['var'].split():
                    if op == '+':
                        mulstart.loc[model.current_per,
                                     var] = mulstart.loc[model.current_per,
                                                         var] + wset[i].value
                    elif op == '+impulse':
                        mulstart.loc[model.current_per[0],
                                     var] = mulstart.loc[model.current_per[0],
                                                         var] + wset[i].value
                    elif op == '=start-':
                        startindex = mulstart.index.get_loc(
                            model.current_per[0])
                        varloc = mulstart.columns.get_loc(var)
                        mulstart.iloc[:startindex, varloc] = wset[i].value
                    elif op == '=':
                        mulstart.loc[model.current_per, var] = wset[i].value
                    elif op == '=impulse':
                        mulstart.loc[model.current_per[0], var] = wset[i].value
                    else:
                        print(f'Wrong operator in {cont}.\nNot updated')
                        assert 1 == 3, 'wRONG OPERATOR'

        # now  update from the radio buttons
        if lradiodef:
            for wradio, (des, cont) in zip(wradiolist, radiodef.items()):
                # print(des,wradio.value,wradio.index,cont[wradio.index])
                for v in cont:
                    mulstart.loc[model.current_per, v[1]] = 0.0
                mulstart.loc[model.current_per, cont[wradio.index][1]] = 1.0

        if lcheckdef:
            for box, (des, var, _) in zip(wchecklist, checkdef):
                mulstart.loc[model.current_per, var] = 1.0 * box.value

        #with out:
        clear_output()
        mul = model(mulstart, **modelopt)
        # model.mulstart=mulstart

        clear_output()
        display(w)
        #_ = mfrbus['XGDPN RFF RFFMIN GFSRPN'].dif.rename(trans).plot(colrow=1,sharey=0)

        if firstrun:
            model.experiment_results = {}
            model.experiment_results[basename] = {
                'results': model.basedf.copy()
            }
            firstrun = False
            wname.value = f'{altname}'
        else:
            altname = wname.value
            walt.value = f'{altname}'
            model.experiment_results[altname] = {
                'results': model.lastdf.copy()
            }

        if showout:
            varpat_this = wpat.value
            resdic = get_alt_dic(model, varpat_this, model.experiment_results)
            a = jupviz(resdic, model)()
        else:
            a = vis_alt3(get_alt_dic(model, wpat.value,
                                     model.experiment_results),
                         model,
                         trans=trans)
    rdm = '''\
                 demand = (demand_ofset+ demand_slope*price+ demand_second * price**2)
                 supply = supply_ofset+ supply_slope*price+ supply_second * price**2
    <endo=price> supply = demand 
    
    '''.upper()
    rdm2 = '''\
    <endo=price> supply_ofset+ supply_slope*price+ supply_second * price**2 = (demand_ofset+ demand_slope*price+ demand_second * price**2) 
    '''
    os.environ['PYTHONBREAKPOINT'] = '0'

    edm = '\n'.join(un_normalize(f) for f in rdm.split('\n') if len(f.strip()))
    fdm = mp.explode(edm)
    mdm = ms.newmodel(fdm)
    #%%
    grunddf = mc.insertModelVar(grunddf, mdm).astype('float')
    sim1 = mdm.newton1per(grunddf,
                          antal=30,
                          silent=0,
                          nonlin=0,
                          newtonalfa=0.4,
                          newtondamp=4)
    ssim1 = mdm.newtonstack(grunddf,
                            antal=30,
                            silent=0,
                            nonlin=0,
                            newtonalfa=1,
                            newtondamp=4)
    #%%
    # print(newton.diff_model.equations)
    if 0:
Esempio n. 6
0
                     'def los(double[:] a):\n    ' +
                     '    '.join([('los' + i + '(a) \n') for i in chunklist]) +
                     '    return asarray(a)')
        #    print(masterout)
        return chunkedout, masterout, chunklist

if __name__ == '__main__':
    #%%
    numberlines = 10
    chunksize = 10
    df = pd.DataFrame({'A0': [1., 2., 3, 4], 'B': [10., 20., 30., 40.]})
    mtest = simmodel(''.join([
        'FRMl <>  a' + str(i) + '=b(-1) +' + str(i) + '*2 +c $ '
        for i in range(numberlines)
    ]))
    df = mc.insertModelVar(df, mtest)
    mtest.findpos()
    tt = mtest.outsolve3(chunk=3, ljit=1)
    with open('solve.py', 'wt') as out:
        out.write(tt)
    print(tt)
    exec(tt, globals())
    solvefunk = make()
    #%%
    #    xx = mtest(df,'1','2',setalt=True,setbase=True)
    if 1:
        testout = '''
from  slos import los  
import pandas as pd 
#df = pd.DataFrame([[0.0,0.0,0.0,0.0,0.0,0.0,0.0] for i in range(''' + str(
            len(df.columns)) + ''')]).T
       FRMl <>  ii = TY(-1)+c(-1)+Z*c(+1) $
       frml <>  c=0.8*yd+log(1) $
       frml <>  d = c +2*ii(-1) $
      frml <>  c2=0.8*yd+log(1) $
       frml <>  d2 = c + 42*ii $
      frml <>  c3=0.8*yd+log(1) $
      frml <>  c4=0.8*yd+log(1) $
      frml <>  c5=0.8*yd+log(1) $
 '''
   fnew = un_normalize_model(ftest)
   m2 = newmodel(un_normalize_model(ftest),
                 funks=[f],
                 straight=True,
                 modelname='m2 testmodel')
   m2.normalized = False
   df2 = insertModelVar(df2, m2)
   df3 = insertModelVar(df3, m2)
   z1 = m2(df2)
   z2 = m2(df3)
   #        ccc = m2.totexplain('D2',per=2019,vtype='per',top=0.8)
   #        ccc = m2.totexplain('D2',vtype='last',top=0.8)
   #        ccc = m2.totexplain('D2',vtype='sum',top=0.8)
   #%%
   # ddd = totdif(m2)
   # eee = totdif(m2)
   # ddd.totexplain('D2',vtype='all',top=0.8)
   # eee.totexplain('D2',vtype='all',top=0.8)
   #%%
   nn = newton_diff(m2, df=df2, timeit=0, onlyendocur=1)
   df_dif = nn.get_diff_df_tot(df2)
   # md1 = mat_dif.toarray()