ModelFlowIb 1.46__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modelhelp.py ADDED
@@ -0,0 +1,253 @@
1
+ """
2
+ Created on Tue Mar 7 10:38:28 2017
3
+
4
+ @author: hanseni
5
+
6
+ utilities for Stampe models
7
+
8
+ """
9
+
10
+
11
+ import networkx as nx
12
+ import pandas as pd
13
+ import numpy as np
14
+ import time
15
+ from contextlib import contextmanager
16
+ import sys
17
+ import itertools
18
+ import operator as op
19
+
20
+
21
+
22
+
23
+ def update_var(databank,xvar,operator='=',inputval=0,start='',slut='',create=1, lprint=False,scale=1.0):
24
+
25
+ """Updates a variable in the databank. Possible update choices are:
26
+ \n \= : val = inputval
27
+ \n \+ : val = val + inputval
28
+ \n \- : val = val - inputval
29
+ \n \* : val = val * inputval
30
+ \n \=growth : val = val(t-1)+inputval +
31
+ \n \% : val = val(1+inputval/100)
32
+ \n
33
+ \n scale scales the input variables default =1.0
34
+
35
+ """
36
+ var = xvar.upper()
37
+ if var not in databank:
38
+ if not create:
39
+ errmsg = f'Variable to update not found:{var}, timespan = [{start} {slut}] \nSet create=True if you want the variable created: '
40
+ raise Exception(errmsg)
41
+ else:
42
+ if 0:
43
+ print('Variable not in databank, created ',var)
44
+ databank[var]=0.0
45
+
46
+ current_per = databank.index[databank.index.get_slice_bound(start,'left')
47
+ :databank.index.get_slice_bound(slut,'right')]
48
+ if operator.upper() == '+GROWTH':
49
+ if databank.index.get_loc(start) == 0:
50
+ raise Exception(f"+growth update can't start at first row, var:{var}")
51
+ orgdata=pd.Series(databank.pct_change().loc[current_per,var]).copy(deep=True)
52
+ ...
53
+ else:
54
+ orgdata=pd.Series(databank.loc[current_per,var]).copy(deep=True)
55
+
56
+ antalper=len(current_per)
57
+ # breakpoint()
58
+ if isinstance(inputval,float) or isinstance(inputval,int) :
59
+ inputliste=[float(inputval)]
60
+ elif isinstance(inputval,str):
61
+ inputliste=[float(i) for i in inputval.split()]
62
+ elif isinstance(inputval,list):
63
+ inputliste= [float(i) for i in inputval]
64
+
65
+
66
+ elif isinstance(inputval, pd.Series):
67
+ # inputliste= inputval.base
68
+ inputliste= list(inputval) #Ib for at håndtere mulitindex serier
69
+ else:
70
+ print('Fejl i inputdata',type(inputval))
71
+ inputdata=inputliste*antalper if len(inputliste) == 1 else inputliste
72
+
73
+ if len(inputdata) != antalper :
74
+ print('** Error, There should be',antalper,'values. There is:',len(inputdata))
75
+ print('** Update =',var,'Data=',inputdata,start,slut)
76
+ raise Exception('wrong number of datapoints')
77
+ else:
78
+ inputserie=pd.Series(inputdata,current_per)*scale
79
+ # print(' Variabel------>',var)
80
+ # print( databank[var])
81
+ if operator=='=': #changes value to input value
82
+ outputserie=inputserie
83
+ elif operator == '+':
84
+ outputserie=orgdata+inputserie
85
+ elif operator == '*':
86
+ outputserie=orgdata*inputserie
87
+ elif operator == '%':
88
+ outputserie=orgdata*(1.0+inputserie/100.0)
89
+ elif operator == '=DIFF': # data=data(-1)+inputdata
90
+ if databank.index.get_loc(start) == 0:
91
+ raise Exception(f"=diff update can't start at first row, var:{var}")
92
+ ilocrow =databank.index.get_loc(start)-1
93
+ iloccol = databank.columns.get_loc(var)
94
+ temp=databank.iloc[ilocrow,iloccol]
95
+ addon = list(itertools.accumulate([i for i in inputserie],op.add))
96
+ # print(f'{addon=}')
97
+ opdater=[temp+add for add in addon]
98
+ outputserie=pd.Series(opdater,current_per)
99
+ elif operator.upper() == '=GROWTH': # data=data(-1)+inputdata
100
+ if databank.index.get_loc(start) == 0:
101
+ raise Exception(f"=growth update can't start at first row, var:{var}")
102
+ ilocrow =databank.index.get_loc(start)-1
103
+ iloccol = databank.columns.get_loc(var)
104
+ temp=databank.iloc[ilocrow,iloccol]
105
+ factor = list(itertools.accumulate([(1+i/100) for i in inputserie],op.mul))
106
+ opdater=[temp * it for it in factor]
107
+ outputserie=pd.Series(opdater,current_per)
108
+ elif operator.upper() == '+GROWTH': # data=data(-1)+inputdata
109
+ if databank.index.get_loc(start) == 0:
110
+ raise Exception(f"+growth update can't start at first row, var:{var}")
111
+ ilocrow =databank.index.get_loc(start)-1
112
+ iloccol = databank.columns.get_loc(var)
113
+ temp=databank.iloc[ilocrow,iloccol]
114
+ factor = list(itertools.accumulate([(1+i/100+o) for i,o in zip(inputserie,orgdata)],op.mul))
115
+ opdater=[temp * it for it in factor]
116
+ outputserie=pd.Series(opdater,current_per)
117
+ else:
118
+ raise Exception(f'Illegal operator in update:{operator} Variable: {var}')
119
+ outputserie=pd.Series(np.NaN,current_per)
120
+ outputserie.name=var
121
+ databank.loc[current_per,var]=outputserie
122
+ if lprint:
123
+ print('Update',operator,inputdata,start,slut)
124
+ forspalte=str(max(6,len(var)))
125
+ print(('{:<'+forspalte+'} {:>20} {:>20} {:>20}').format(var,'Before', 'After', 'Diff'))
126
+ newdata=databank.loc[current_per,var]
127
+ diff=newdata-orgdata
128
+ for i in current_per:
129
+ print(('{:<'+forspalte+'} {:>20.4f} {:>20.4f} {:>20.4f}').format(str(i),orgdata[i],newdata[i],diff[i]))
130
+
131
+
132
+ def tovarlag(var,lag):
133
+ ''' creates a stringof var(lag) if lag else just lag '''
134
+ if type(lag)==int:
135
+ return f'{var}({lag:+})' if lag else var
136
+ else:
137
+ return f'{var}({lag})' if lag else var
138
+
139
+ def cutout(input,threshold=0.0):
140
+ '''get rid of rows below treshold and returns the dataframe or serie '''
141
+ if type(input)==pd.DataFrame:
142
+ org_sum = input.sum(axis=0)
143
+ new = input.iloc[(abs(input) >= threshold).any(axis=1).values,:]
144
+ if len(new) < len(input):
145
+ new_sum = new.sum(axis=0)
146
+ small = org_sum - new_sum
147
+ small.name = 'Small'
148
+ # breakpoint()
149
+ output = pd.concat([new,pd.DataFrame(small).T],axis=0)
150
+ else:
151
+ output = input
152
+ return output
153
+ if type(input)==pd.Series:
154
+ org_sum = input.sum()
155
+ new = input.iloc[(abs(input) >= threshold).values]
156
+ if len(new) < len(input):
157
+ new_sum = new.sum()
158
+ small = pd.Series(org_sum - new_sum)
159
+ small.index = ['Small']
160
+ output = pd.concat([new,small])
161
+ else:
162
+ output=input
163
+ return output
164
+
165
+ @contextmanager
166
+ def ttimer(input='test',show=True,short=False):
167
+ '''
168
+ A timer context manager, implemented using a
169
+ generator function. This one will report time even if an exception occurs"""
170
+
171
+ Parameters
172
+ ----------
173
+ input : string, optional
174
+ a name. The default is 'test'.
175
+ show : bool, optional
176
+ show the results. The default is True.
177
+ short : bool, optional
178
+ . The default is False.
179
+
180
+ Returns
181
+ -------
182
+ None.
183
+
184
+ '''
185
+
186
+ start = time.time()
187
+ if show and not short: print(f'{input} started at : {time.strftime("%H:%M:%S"):>{15}} ')
188
+ try:
189
+ yield
190
+ finally:
191
+ if show:
192
+ end = time.time()
193
+ seconds = (end - start)
194
+ minutes = seconds/60.
195
+ if minutes < 2.:
196
+ afterdec='1' if seconds >= 10 else ('3' if seconds >= 1 else '10')
197
+ print(f'{input} took : {seconds:>{15},.{afterdec}f} Seconds')
198
+ else:
199
+ afterdec='1' if minutes >= 10 else '4'
200
+ print(f'{input} took : {minutes:>{15},.{afterdec}f} Minutes')
201
+
202
+ def finddec(df):
203
+ ''' find a suitable number of decimal places from the magnitudes of a dataframe '''
204
+ try:
205
+ thismax = df.abs().max().max()
206
+ except:
207
+ thismax = df.abs().max()
208
+
209
+ if thismax > 1000:
210
+ outdec = 0
211
+ elif thismax > 10:
212
+ outdec = 3
213
+ else:
214
+ outdec = 6
215
+
216
+ return outdec
217
+
218
+
219
+ def insertModelVar(dataframe, model=None):
220
+ """Inserts all variables from model, not already in the dataframe.
221
+ Model can be a list of models """
222
+ if isinstance(model,list):
223
+ imodel=model
224
+ else:
225
+ imodel = [model]
226
+
227
+ myList=[]
228
+ for item in imodel:
229
+ myList.extend(item.allvar.keys())
230
+ manglervars = list(set(myList)-set(dataframe.columns))
231
+ if len(manglervars):
232
+ extradf = pd.DataFrame(0.0,index=dataframe.index,columns=manglervars).astype('float64')
233
+ data = pd.concat([dataframe,extradf],axis=1)
234
+ return data
235
+ else:
236
+ return dataframe
237
+
238
+ def df_extend(df,add=5):
239
+ '''Extends a Dataframe, assumes that the indes is of period_range type'''
240
+ newindex = pd.period_range(df.index[0], periods=len(df)+add, freq=df.index.freq)
241
+ return df.reindex(newindex,method='ffill')
242
+
243
+ if __name__ == '__main__':
244
+ #%% Test
245
+ if not 'baseline' in locals() or 1 :
246
+ from modelclass import model
247
+ madam,baseline = model.modelload('../Examples/ADAM/baseline.pcim',run=1,silent=0,ljit=1,stringjit=0 )
248
+ # make a simpel experimet VAT
249
+ scenarie = baseline.copy()
250
+ scenarie.TG = scenarie.TG + 0.05
251
+ _ = madam(scenarie)
252
+
253
+ update_var(baseline,'tg','=GROWTH',1,2021,2024,lprint=1)
modelinvert.py ADDED
@@ -0,0 +1,211 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Sep 21 12:41:10 2017
4
+
5
+ @author: IBH
6
+
7
+ Class to handle general target/instrument problems.
8
+
9
+ Number of targets should be equal to number of instruments
10
+
11
+ An instrument can comprice of severeral variables
12
+ instruments are inputtet as a list of instruments
13
+
14
+ """
15
+
16
+ import pandas as pd
17
+ import numpy as np
18
+ from tqdm.auto import tqdm
19
+
20
+
21
+ from modelhelp import update_var
22
+
23
+
24
+ class targets_instruments():
25
+ ''' Class to handle general target/instrument problems.
26
+ Where the response is delayed specify this with delay.
27
+
28
+ Number of targets should be equal to number of instruments
29
+
30
+ An instrument can comprice of severeral variables
31
+
32
+ **Instruments** are inputtet as a list of instruments
33
+
34
+ To calculate the jacobian each instrument variable has a impuls,
35
+ which is used as delta when evaluating the jacobi matrix::
36
+
37
+ [ 'QO_J','TG'] Simple list each variable are shocked by the default impulse
38
+ [ ('QO_J',0.5), 'TG'] Here QO_J is getting its own impuls (0.5)
39
+ [ [('QO_J',0.5),('ORLOV',1.)] , ('TG',0.01)] here an impuls is given for each variable, and the first instrument consiste of two variables
40
+
41
+ **Targets** are list of variables
42
+
43
+ Convergence is achieved when all targets are within convergens distance from the target value
44
+
45
+ Convergencedistance can be set individual for a target variable by setting a value in <modelinstance>.targetconv
46
+
47
+ Targets and target values are provided by a dataframe.
48
+
49
+ '''
50
+
51
+ def __init__(self,databank,targets,instruments,model,DefaultImpuls=0.01,defaultconv=0.01,
52
+ nonlin=False,silent = True, maxiter=30,solveopt={},varimpulse=False):
53
+
54
+ self.model = model
55
+ self.df = model.lastdf
56
+ self.targetvars = targets if isinstance(targets,list) else targets.columns
57
+ self.targetconv = {t: defaultconv for t in self.targetvars} # make sure there is a convergence criteria ]
58
+ self.targets = targets
59
+ self.instruments = {}
60
+ self.solveopt = {**solveopt, **{'keep':''}}
61
+ self.silent = silent
62
+ self.debug=False
63
+ self.maxiter = maxiter
64
+ self.nonlin=nonlin
65
+ self.databank = databank.copy()
66
+ self.varimpulse = varimpulse
67
+ self.savesolvearg = model.oldkwargs if hasattr(model,'oldkwargs') else {}
68
+ for inumber,i in enumerate(instruments):
69
+ vars = i if isinstance(i,list) else [i] # make it a list even if one variable
70
+ xx = [v if isinstance(v,tuple) else (v,DefaultImpuls) for v in vars] # make sure there is a impuls
71
+ name = ','.join(n for n,i in xx)
72
+ name = f'Instrument_{inumber}' if len(name)>500 else name
73
+ self.instruments[inumber] = {'name':name , 'vars': xx, }
74
+ self.DefaultImpuls = DefaultImpuls
75
+ # breakpoint()
76
+
77
+
78
+ def jacobi(self,per,delay=None):
79
+ ''' Calculates a jecobi matrix of derivatives based on the instruments and targets
80
+
81
+ returns a dataframe '''
82
+ # breakpoint()
83
+
84
+ iper = self.df.index.get_loc(per)
85
+ iper_delayed = iper-delay
86
+ per_delayed = self.df.index[iper_delayed]
87
+
88
+
89
+ self.jac = jac=pd.DataFrame(0,index=self.targetvars, columns=[v['name'] for v in self.instruments.values()])
90
+ with self.model.set_smpl(per_delayed,per):
91
+ mul = self.model(self.df,setlast=False, **self.solveopt) # start point for this quarter
92
+ basis = mul.copy(deep=True) # make a reference point for the calculation the derivatives.
93
+ if not self.silent: print(f'Update jacobi: {per} effects from {per_delayed}')
94
+ oldsave = self.model.save
95
+ for instrument in self.instruments.values():
96
+ # print('instrument: ',instrument['name'])
97
+ # set the instrument
98
+ for var,impuls in instrument['vars']:
99
+ if self.varimpulse:
100
+ mul.loc[per_delayed,var] = mul.loc[per_delayed,var] + impuls # increase loan growth
101
+ else:
102
+ mul.loc[per_delayed:,var] = mul.loc[per_delayed:,var] + impuls # increase loan growth
103
+ # calculate the effect
104
+ with self.model.set_smpl(per_delayed,per):
105
+
106
+ res = self.model(mul,save=False,**self.solveopt) #antal=600,first_test=20,ljit=1) # solve model
107
+
108
+ # breakpoint()
109
+ jac.loc[self.targetvars,instrument['name']] = res.loc[per,self.targetvars]-basis.loc[per,self.targetvars] # store difference in original bank
110
+ # reset the instrument
111
+ for var,impuls in instrument['vars']:
112
+ mul.loc[per_delayed:,var]=basis.loc[per_delayed:,var]
113
+ self.model.oldkwargs = self.savesolvearg
114
+ self.model.save = oldsave
115
+
116
+ return jac
117
+
118
+ def invjacobi(self,per,diag=False,delay=0):
119
+ ''' Calculates the inverted jacobi matrix
120
+
121
+ returns a dataframe '''
122
+
123
+ x = self.jacobi(per,delay=delay)
124
+ # print(x)
125
+ if diag:
126
+ out = pd.DataFrame(np.diag(1.0/x.values.diagonal()),x.columns,x.index)
127
+ else:
128
+ out= pd.DataFrame(np.linalg.inv(x),x.columns,x.index)
129
+ return out
130
+
131
+ def targetseek(self,databank=None,shortfall=False,ti_damp=1.0,delay=0,progressbar = True,**kwargs):
132
+ ''' Calculates the instruments as a function of targets '''
133
+ silent = kwargs.get('silent',self.silent)
134
+ self.maxiter = kwargs.get('maxiter',self.maxiter)
135
+ self.nonlin = kwargs.get('nonlin',self.nonlin)
136
+
137
+ tindex = self.model.current_per.copy()
138
+ res = self.databank
139
+ # self.inv = inv = self.invjacobi(self.targets.index[0])
140
+ self.inv = inv = self.invjacobi(self.targets.index[0],diag=shortfall,delay=delay)
141
+
142
+ self.conv = pd.Series([self.targetconv[v] for v in self.targetvars],self.targetvars)
143
+ # print(inv)
144
+ oldsave = self.model.save
145
+ bars = '{desc}: {percentage:3.0f}%|{bar}| {n_fmt}/{total_fmt}'
146
+
147
+ with tqdm(total=len(self.targets.index),disable = not progressbar,desc=f'Finding instruments ',bar_format=bars) as pbar:
148
+
149
+ for per in self.targets.index:
150
+
151
+ iper = self.df.index.get_loc(per)
152
+ iper_delayed = iper-delay
153
+ per_delayed = self.df.index[iper_delayed]
154
+
155
+
156
+ if not silent or self.debug:
157
+ print('Period:',per)
158
+ res = self.model(res,per_delayed ,per ,save=False, **self.solveopt)
159
+ orgdistance = self.targets.loc[per,self.targetvars] - res.loc[per,self.targetvars]
160
+ shortfallvar = (shortfall * orgdistance) >= 0
161
+ for iterations in range(self.maxiter):
162
+ startdistance = self.targets.loc[per,self.targetvars] - res.loc[per,self.targetvars]
163
+ self.distance = distance = startdistance*shortfallvar if shortfall else startdistance
164
+ if not silent: print('Period:',per,' Target instrument iteration:',iterations,
165
+ f' Max distance: {distance.abs().max():.3f}')
166
+ if self.debug:
167
+ print(f'\nTarget instrument {per}, iteration {iterations}')
168
+ print(f'Distance to target :\n{startdistance}\n')
169
+ if shortfall:
170
+ print(f'Distance to shortfall target :\n{distance}\n')
171
+
172
+ if (distance.abs()>=self.conv).any():
173
+ if self.nonlin:
174
+ if type(self.nonlin) == int:
175
+ if iterations >= self.nonlin:
176
+ if not iterations%self.nonlin:
177
+ self.inv = inv = self.invjacobi(per,diag=shortfall,delay=delay)
178
+ else:
179
+ self.inv = inv = self.invjacobi(per,diag=shortfall,delay=delay)
180
+
181
+ update = inv.dot(distance) * ti_damp
182
+ if self.debug :
183
+ print('Update instruments:')
184
+ print(update)
185
+ for instrument in self.instruments.values():
186
+ for var,impuls in instrument['vars']:
187
+ if self.varimpulse:
188
+ res.loc[per_delayed,var] = res.loc[per_delayed,var] + update[instrument['name']] * impuls # increase loan growth
189
+ else:
190
+ res.loc[per_delayed:,var] = res.loc[per_delayed:,var] + update[instrument['name']] * impuls # increase loan growth
191
+
192
+ res = self.model(res,per_delayed ,per ,setlast=False,**self.solveopt)
193
+ else:
194
+ break
195
+ pbar.update()
196
+ self.model.lastdf = res
197
+ self.model.oldkwargs = self.savesolvearg
198
+ self.model.save = oldsave
199
+ self.model.current_per = tindex
200
+
201
+ return res
202
+
203
+
204
+ def __call__(self, *args, **kwargs ):
205
+ '''Uses :any:`targetseek` '''
206
+ return self.targetseek( *args, **kwargs)
207
+
208
+
209
+ #%% running
210
+ if __name__ == '__main__':
211
+ pass