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.
modelmanipulation.py ADDED
@@ -0,0 +1,1273 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon Sep 02 19:41:11 2013
4
+
5
+ This module is a textprocessing module which is used to transforms a *template model* for a generic bank into into a unrolled and
6
+ expande model which covers all banks - under control of a list feature.
7
+
8
+ The resulting model can be solved after beeing proccesed in the *modelclass* module.
9
+
10
+ In addition to creating a model for forecasting, the module can also create a model which calculates residulas and and variables
11
+ in historic periods. This model can also be solved by the *modelclass* module.
12
+
13
+
14
+
15
+ @author: Ib
16
+ """
17
+ import re
18
+ from collections import defaultdict
19
+ from collections import namedtuple
20
+ from itertools import groupby,chain
21
+ from sympy import solve, sympify
22
+ import ast
23
+
24
+
25
+ from modelpattern import find_statements,split_frml,find_frml,list_extract,udtryk_parse,kw_frml_name,commentchar
26
+
27
+
28
+ class oldsafesub(dict):
29
+ '''A subclass of dict.
30
+ if a *safesub* is indexed by a nonexisting keyword it just return the keyword
31
+ this alows missing keywords when substitution text inspired by Python cookbook '''
32
+
33
+ def __missing__(self, key):
34
+ return '{' + key +'}'
35
+
36
+ class safesub(dict):
37
+ '''A subclass of dict.
38
+ if a *safesub* is indexed by a nonexisting keyword it just return the keyword
39
+
40
+ - key-<number> where key is in integer returns the numeric value of key-number
41
+ - key+<number> where key is in integer returns the numeric value of key+number
42
+ -
43
+
44
+ this alows missing keywords when substitution text inspired by Python cookbook '''
45
+
46
+ def __missing__(self, key):
47
+ # breakpoint()
48
+ if '-' in key:
49
+ try:
50
+ testkey,ofset = key.split('-')
51
+ res = f'{str(int(self.get(testkey.strip()))-int(ofset))}'
52
+ return res
53
+
54
+ except:
55
+ print(f'Wrong key or value in list ofset:<{key}>')
56
+ raise
57
+
58
+ if '+' in key:
59
+ try:
60
+ testkey,ofset = key.split('+')
61
+ res = f'{str(int(self.get(testkey.strip()))+int(ofset))}'
62
+ return res
63
+
64
+ except:
65
+ print(f'Wrong key or value in list ofset:<{key}>')
66
+ raise
67
+ return '{' + key +'}'
68
+
69
+
70
+ def sub(text, katalog):
71
+ '''Substitutes keywords from dictionary by returning
72
+ text.format_map(safesub(katalog))
73
+ Allows missing keywords by using safesub subclass'''
74
+ return text.format_map(safesub(katalog))
75
+
76
+ def oldsub_frml(ibdic, text, plus='', var='', lig='', sep='\n'):
77
+ ''' to repeat substitution from list
78
+
79
+ * *plus* is a seperator, used for creating sums \n
80
+
81
+ * *var* and *lig* Determins for which items the substitution should take place
82
+ by var=abe, lig='ko' substitution is only performed for entries where var=='ko' '''
83
+
84
+
85
+ katalog = dict()
86
+ outlist = []
87
+ try:
88
+ keys = [k for k in ibdic.keys()] # find keywords
89
+ # print('var, lig',var,lig)'
90
+ if var == '':
91
+ values = select = ibdic[keys[0]] # no selection criteri
92
+ elif lig != '':
93
+ # We have a selecton criteria and select the situations where it apply
94
+ values = ibdic[var] # the values for thes sublist
95
+ select = [t for t in values if t == lig]
96
+ for taller, k in enumerate(values): # loop over number of values
97
+ if k in select:
98
+ for j in ibdic.keys(): # for each keywords
99
+ ud = ibdic.get(j)
100
+ # print('*****************test',j,taller)
101
+ katalog[j] = ud[taller]
102
+ # make a list with the new text
103
+ outlist.append(sub(text, katalog) + sep)
104
+ except:
105
+ print('***** problem',ibdic, '<'+text+ '>')
106
+ print(' ')
107
+ return plus.join(outlist)
108
+
109
+ def sub_frml(ibdic, text, plus='', xvar='', lig='', sep='\n'):
110
+ ''' to repeat substitution from list
111
+
112
+ * *plus* is a seperator, used for creating sums \n
113
+
114
+ * *xvar* and *lig* Determins for which items the substitution should take place
115
+ by var=abe, lig='ko' substitution is only performed for entries where var=='ko'
116
+
117
+ * *xvar* is the variable to chek against selected in list
118
+ * *select list* is a list of elements in the xvar list to be included
119
+ * *matc* is the entry in *select list* from which to select from xvar
120
+
121
+
122
+ '''
123
+ # print(f'{text=}')
124
+ # print(f'{ibdic=}')
125
+ katalog = dict()
126
+ outlist = []
127
+ try:
128
+ keys = [k for k in ibdic.keys()] # find keywords
129
+ # print('var, lig',xvar,lig)
130
+ if xvar == '':
131
+ values = select = ibdic[keys[0]] # no selection criteri
132
+ else:
133
+ if xvar not in list(ibdic.keys()):
134
+ raise Exception(f'**** Trying to condition on a sublist which is not there\nCondition:{xvar}\nAllowed sublists:{list(ibdic.keys())}')
135
+ values = ibdic[xvar]
136
+ if lig != '':
137
+ # We have a selecton criteria and select the situations where it apply
138
+ select = [t for t in values if t == lig]
139
+ # print('values:',[t for t in values])
140
+ for taller, k in enumerate(values): # loop over number of values in the list
141
+ katalog={}
142
+ if k in select:
143
+ katalog = {j: ibdic.get(j)[taller] for j in ibdic.keys()}
144
+ outlist.append(sub(text, katalog) + sep)
145
+ # print('>',katalog)
146
+ # print(err1)
147
+ except:
148
+ err1 = f'**** when substitution in formulas:\nDictionary from which values are taken:\n{ibdic}\n{text=} \nSelection:{xvar} == {lig}\n{katalog=}'
149
+ raise Exception(err1)
150
+ return plus.join(outlist)
151
+
152
+ a={'bankdic': {'bank':['Danske','Nordea'],'danske':['yes','no'],'nordisk':['yes','no']}}
153
+
154
+
155
+ def find_res(f):
156
+ ''' Finds the expression which calculates the residual in a formel. FRML <res=a,endo=b> x=a*b+c $ '''
157
+ from sympy import solve, sympify
158
+ a, fr, n, udtryk = split_frml(f.upper())
159
+ udres = ''
160
+ tres = kw_frml_name(n, 'RES')
161
+ if tres:
162
+ lhs, rhs = udtryk.split('=', 1)
163
+ res = lhs.strip() + '_J' if tres == 'J' else lhs.strip() + \
164
+ '_JR' if tres == 'JR' else tres
165
+ # we take the the $ out
166
+ kat = sympify('Eq(' + lhs + ',' + rhs[0:-1] + ')')
167
+ res_frml = sympify('res_frml')
168
+ res_frml = solve(kat, res)
169
+ udres = 'FRML ' + 'RES' + ' ' + \
170
+ res.ljust(25) + ' = ' + str(res_frml)[1:-1] + ' $'
171
+ return udres
172
+
173
+ def find_res_dynare(equations):
174
+ ''' equations to calculat _res formulas
175
+ FRML <> x=a*b+c +x_RES $ -> FRML <> x_res =x-a*b+c $'''
176
+ from sympy import solve, sympify
177
+ out=[]
178
+ for f in find_frml(equations):
179
+ a, fr, n, udtryk = split_frml(f.upper())
180
+ lhs, rhs = udtryk.split('=', 1)
181
+ res= lhs.strip()+'_RES'
182
+ if res in rhs:
183
+ # we take the the $ out
184
+ kat = sympify('Eq(' + lhs + ',' + rhs[0:-1] + ')')
185
+ res_frml = sympify('res_frml')
186
+ res_frml = solve(kat, res)
187
+ udres = 'FRML ' + 'RES' + ' ' + res + ' = ' + str(res_frml)[1:-1] + ' $'
188
+ out.append(udres)
189
+ return '\n'.join(out)
190
+
191
+ def find_res_dynare_new(equations):
192
+ ''' equations to calculat _res formulas
193
+ FRML <> x=a*b+c +x_RES $ -> FRML <> x_res =x-a*b+c $
194
+ not finished to speed time up '''
195
+ out=[]
196
+ for f in find_frml(equations):
197
+ a, fr, n, udtryk = split_frml(f.upper())
198
+ lhs,rhs=udtryk.split('=',1)
199
+ lhs = lhs.strip()
200
+ rhs=rhs.strip()[:-1]
201
+ res= lhs+'_RES'
202
+ if res in rhs:
203
+ rep = rhs.replace(res,'').strip()[:-1]
204
+ new = f'frml <> {res} = {lhs} - ({rep}) $'
205
+ out.append(new)
206
+
207
+ return '\n'.join(out)
208
+
209
+ def find_hist_model(equations):
210
+ ''' takes a unrolled model and create a model which can be run for historic periode \n
211
+ and the identities are also calculeted'''
212
+ hist = []
213
+ for f in find_frml(equations):
214
+ a, fr, n, udtryk = split_frml(f.upper())
215
+ if kw_frml_name(n, 'IDENT') or kw_frml_name(n, 'I'):
216
+ # identites are just replicated in order to calculate backdata
217
+ hist.append(f.strip())
218
+ return('\n'.join(hist))
219
+
220
+
221
+ def exounroll(in_equations):
222
+ ''' takes a model and makes a new model by enhancing frml's with <exo=,j=,jr=>
223
+ in their frml name.
224
+
225
+ :exo: the value can be fixed in to a value valuename_x by setting valuename_d=1
226
+
227
+ :jled: a additiv adjustment element is added to the frml
228
+
229
+ :jrled: a multiplicativ adjustment element is added to the frml
230
+ '''
231
+
232
+ nymodel = []
233
+ equations = in_equations[:] # we want do change the e
234
+ for comment, command, value in find_statements(equations.upper()):
235
+ # print('>>',comment,'<',command,'>',value)
236
+ if comment:
237
+ nymodel.append(comment)
238
+ elif command == 'FRML':
239
+ a, fr, n, udtryk = split_frml((command + ' ' + value).upper())
240
+ #print(fr,n, kw_frml_name(n.upper(),'EXO'))
241
+ # we want a relatov adjustment
242
+ if kw_frml_name(n, 'JRLED') or kw_frml_name(n, 'JR'):
243
+ lhs, rhs = udtryk.split('=', 1)
244
+ udtryk = lhs + \
245
+ '= (' + rhs[:-1] + ')*(1+' + lhs.strip() + '_JR )' + '$'
246
+ if kw_frml_name(n, 'JLED') or kw_frml_name(n, 'J'):
247
+ lhs, rhs = udtryk.split('=', 1)
248
+ # we want a absolute adjustment
249
+ udtryk = lhs + '=' + rhs[:-1] + '+' + lhs.strip() + '_J' + '$'
250
+ if kw_frml_name(n, 'EXO'):
251
+ lhs, rhs = udtryk.split('=', 1)
252
+ endogen = lhs.strip()
253
+ dummy = endogen + '_D'
254
+ exogen = endogen + '_X'
255
+ udtryk = lhs + \
256
+ '=(' + rhs[:-1] + ')*(1-' + dummy + ')+' + exogen + '*' + dummy + '$'
257
+
258
+ nymodel.append(command + ' ' + n + ' ' + udtryk)
259
+ else:
260
+ nymodel.append(command + ' ' + value)
261
+
262
+ equations = '\n'.join(nymodel)
263
+ return equations
264
+
265
+
266
+
267
+ def tofrml(expressions,sep='\n'):
268
+ ''' a function, wich adds FRML to all expressions seperated by <sep>
269
+ if no start is specified the max lag will be used '''
270
+
271
+ notrans = {'DO','ENDDO','LIST','FRML'}
272
+
273
+ def trans(eq):
274
+ test= eq.strip().upper()+' '
275
+ if len(test.strip()) == 0:
276
+ return ''
277
+ if test.split()[0] in notrans:
278
+ return eq + '$'
279
+ elif test[0] == commentchar:
280
+ return eq
281
+ else:
282
+ return ('FRML ' + (eq if test.startswith('<') else ' <> ' +eq) +' $' )
283
+ if '$' in expressions:
284
+ return expressions
285
+ else:
286
+ # exp = re.sub(commentchar+'!(.*)\n',r'!\1\n',expressions)
287
+ eqlist = [trans(e) for e in expressions.split(sep)]
288
+
289
+ return '\n'.join(eqlist)
290
+
291
+
292
+ def dounloop(in_equations,listin=False):
293
+ ''' Expands (unrolls do loops in a model template
294
+ goes trough a model template until there is no more nested do loops '''
295
+ equations = in_equations[:].upper() # we want do change the equations
296
+ # print(f'equations \n{equations} \n ')
297
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
298
+ rest = 1
299
+ while rest: # We want to eat all doo loops
300
+ nymodel = []
301
+ domodel = []
302
+ dolevel = 0
303
+ rest = 0
304
+ liste_name, liste_key, liste_select = '', '', ''
305
+ for comment, command, value in find_statements(equations):
306
+ # print('>>',comment,'<',command,'>',value)
307
+ if command.upper() == 'DO':
308
+ dolevel = dolevel + 1
309
+ if dolevel >= 2:
310
+ rest = 1 # do we have more doolops
311
+ domodel.append(command + ' ' + value)
312
+ #=========================================================================
313
+ # else: # find listname key=select
314
+ #=========================================================================
315
+ elif dolevel == 1:
316
+ liste_name = [
317
+ t.strip().upper() for t in re.split(
318
+ r'[\s,]\s*',
319
+ value[:-1]) if t != '']
320
+ current_dict = liste_dict[liste_name[0]]
321
+ # print('listenavn ',liste_name, current_dict)
322
+ #ibdic, text, plus='', xvar='', lig='', sep='\n',selectlist=None,match=''
323
+ if len(liste_name) == 1:
324
+ liste_key, liste_select = '', ''
325
+ elif len(liste_name) == 4:
326
+ liste_key, liste_select = liste_name[1], liste_name[3]
327
+ # current_dict=liste_dict[value[0:-2].strip()] # find the name of the list
328
+ else:
329
+ raise Exception(f' *** error in DO statement either 1 or 4 arguments:\n{comment=}\n{command=}\nArguments= {value}')
330
+ domodel = []
331
+ elif(command.upper() == 'ENDDO'):
332
+ dolevel = dolevel - 1
333
+ if dolevel == 0:
334
+ if len(liste_name) == 1 or len(liste_name) == 4 or len(liste_name) == 5 :
335
+ ibsud = sub_frml(current_dict, '\n'.join(domodel),plus='',sep='\n',
336
+ xvar=liste_key, lig=liste_select)
337
+ else:
338
+ print('Fejl i liste', liste_name)
339
+ print('Ibsud>',ibsud)
340
+ nymodel.append('\n' + ibsud + '\n')
341
+ elif dolevel >= 1:
342
+ domodel.append(command + ' ' + value + '\n')
343
+ elif dolevel >= 1: # a command to store for do looping
344
+ if comment:
345
+ domodel.append(comment + '')
346
+ else:
347
+ domodel.append(command + ' ' + value)
348
+ else: # a command outside a dooloop
349
+ if comment:
350
+ nymodel.append(comment)
351
+ else:
352
+ nymodel.append(command + ' ' + value)
353
+ equations = '\n'.join(nymodel)
354
+ return equations
355
+
356
+ #%
357
+ def find_arg(funk, streng):
358
+ ''' chops a string in 3 parts \n
359
+ 1. before 'funk('
360
+
361
+ 2. in the matching parantesis
362
+
363
+ 3. after the last matching parenthesis '''
364
+ tfunk, tstreng = funk.upper(), streng.upper()
365
+ tfunk = tfunk + '('
366
+ if tfunk in tstreng:
367
+ start = tstreng.find(tfunk)
368
+ match = tstreng[start + len(tfunk):]
369
+ open = 1
370
+ for index in range(len(match)):
371
+ if match[index] in '()':
372
+ open = (open + 1) if match[index] == '(' else (open - 1)
373
+ if not open:
374
+ return tstreng[:start], match[:index], match[index + 1:]
375
+
376
+
377
+ def sumunroll_old(in_equations,listin=False):
378
+ ''' expands all sum(list,'expression') in a model
379
+ returns a new model'''
380
+ nymodel = []
381
+ equations = in_equations[:].upper() # we want do change the e
382
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
383
+ for comment, command, value in find_statements(equations):
384
+ # print('>>',comment,'<',command,'>',value)
385
+ if comment:
386
+ nymodel.append(comment)
387
+ else:
388
+ while 'SUM(' in value.upper():
389
+ forsum, sumudtryk, eftersum = find_arg('sum', value.upper())
390
+ sumover, sumled = sumudtryk.split(',', 1)
391
+ current_dict = liste_dict[sumover]
392
+ ibsud = sub_frml(current_dict, sumled, '+', '', sep='')
393
+ value = forsum + '(' + ibsud + ')' + eftersum
394
+ nymodel.append(command + ' ' + value)
395
+ equations = '\n'.join(nymodel)
396
+ return equations
397
+
398
+ def lagarray_unroll(in_equations,funks=[]):
399
+ ''' expands all sum(list,'expression') in a model
400
+ returns a new model'''
401
+ nymodel = []
402
+ equations = in_equations[:].upper() # we want do change the e
403
+ for comment, command, value in find_statements(equations):
404
+ # print('>>',comment,'<',command,'>',value)
405
+ if comment:
406
+ nymodel.append(comment)
407
+ else:
408
+ while 'LAG_ARRAY(' in value:
409
+ forlag, lagudtryk, efterlag = find_arg('LAG_ARRAY', value.upper())
410
+ lagnumber, lagled = lagudtryk.split(',', 1)
411
+ ibsud = lag_n_tup(lagled,int(lagnumber),funks=funks)
412
+ value = forlag + 'ARRAY([' + ibsud + '])' + efterlag
413
+ nymodel.append(command + ' ' + value)
414
+ equations = '\n'.join(nymodel)
415
+ return equations
416
+
417
+ def sumunroll(in_equations,listin=False):
418
+ ''' expands all sum(list,'expression') in a model
419
+ if sum(list xvar=lig,'expression') only list elements where the condition is
420
+ satisfied wil be summed
421
+
422
+ returns a new model'''
423
+ nymodel = []
424
+ equations = in_equations[:].upper() # we want do change the e
425
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
426
+ for comment, command, value in find_statements(equations):
427
+ # print('>>',comment,'<',command,'>',value)
428
+ if comment:
429
+ nymodel.append(comment)
430
+ else:
431
+ while 'SUM(' in value.upper():
432
+ forsum, sumudtryk, eftersum = find_arg('sum', value.upper())
433
+ suminit, sumled = sumudtryk.split(',', 1)
434
+
435
+ if '=' in suminit:
436
+ sumover,remain = suminit.split(' ',1)
437
+ xvar,lig =remain.replace(' ','').split('=')
438
+ else:
439
+ sumover = suminit
440
+ xvar=''
441
+ lig=''
442
+
443
+ current_dict = liste_dict[sumover]
444
+ ibsud = sub_frml(current_dict, sumled, '+', xvar=xvar,lig=lig,sep='')
445
+ value = forsum + '(' + ibsud + ')' + eftersum
446
+ nymodel.append(command + ' ' + value)
447
+ equations = '\n'.join(nymodel)
448
+ return equations
449
+
450
+ def argunroll(in_equations,listin=False):
451
+ ''' expands all ARGEXPAND(list,'expression') in a model
452
+ returns a new model'''
453
+ nymodel = []
454
+ equations = in_equations[:].upper() # we want do change the e
455
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
456
+ for comment, command, value in find_statements(equations):
457
+ # print('>>',comment,'<',command,'>',value)
458
+ if comment:
459
+ nymodel.append(comment)
460
+ else:
461
+ while 'ARGEXPAND(' in value.upper():
462
+ forsum, sumudtryk, eftersum = find_arg('ARGEXPAND', value.upper())
463
+ sumover, sumled = sumudtryk.split(',', 1)
464
+ current_dict = liste_dict[sumover]
465
+ ibsud = sub_frml(current_dict, sumled, ',', '', sep='')
466
+ value = forsum + '' + ibsud + '' + eftersum
467
+ nymodel.append(command + ' ' + value)
468
+ equations = '\n'.join(nymodel)
469
+ return equations
470
+
471
+ def creatematrix(in_equations,listin=False):
472
+ ''' expands all ARGEXPAND(list,'expression') in a model
473
+ returns a new model'''
474
+ nymodel = []
475
+ equations = in_equations[:].upper() # we want do change the e
476
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
477
+ for comment, command, value in find_statements(equations):
478
+ # print('>>',comment,'<',command,'>',value)
479
+ if comment:
480
+ nymodel.append(comment)
481
+ else:
482
+ while 'TO_MATRIX(' in value.upper():
483
+ forcreate, createudtryk, eftercreate = find_arg('TO_MATRIX', value.upper())
484
+ temp = createudtryk.split(',', 2)
485
+ mat_name = temp[-1]
486
+ create_row = temp[0]
487
+ row_dict = liste_dict[create_row]
488
+ if len(temp)==2:
489
+ ibsud = '['+sub_frml(row_dict, mat_name, ',', '', sep='')+']'
490
+ else:
491
+ create_column= temp[1]
492
+ column_dict = liste_dict[create_column]
493
+ ibsud0 = '['+sub_frml(column_dict, mat_name, ',', '', sep='')+']'
494
+ ibsud = '['+sub_frml(row_dict, ibsud0 , ',\n', '', sep='')+']'
495
+
496
+ value = forcreate + 'matrix(\n' + ibsud + ')' + eftercreate
497
+ nymodel.append(command + ' ' + value)
498
+ equations = '\n'.join(nymodel)
499
+ return equations
500
+ def createarray(in_equations,listin=False):
501
+ ''' expands all to_array(list) in a model
502
+ returns a new model'''
503
+ nymodel = []
504
+ equations = in_equations[:].upper() # we want do change the e
505
+ liste_dict = listin if listin else list_extract(equations) # Search the whold model for lists
506
+ for comment, command, value in find_statements(equations):
507
+ # print('>>',comment,'<',command,'>',value)
508
+ if comment:
509
+ nymodel.append(comment)
510
+ else:
511
+ while 'TO_ARRAY(' in value.upper():
512
+ forcreate, createudtryk, eftercreate = find_arg('TO_ARRAY', value.upper())
513
+ temp = createudtryk.split(',', 2)
514
+ mat_name = temp[-1]
515
+ create_row = temp[0]
516
+ row_dict = liste_dict[create_row]
517
+ if len(temp)==2:
518
+ ibsud = '['+sub_frml(row_dict, mat_name, ',', '', sep='')+']'
519
+ else:
520
+ create_column= temp[1]
521
+ column_dict = liste_dict[create_column]
522
+ ibsud0 = '['+sub_frml(column_dict, mat_name, ',', '', sep='')+']'
523
+ ibsud = '['+sub_frml(row_dict, ibsud0 , ',\n', '', sep='')+']'
524
+
525
+ value = forcreate + 'array(\n' + ibsud + ')' + eftercreate
526
+ nymodel.append(command + ' ' + value)
527
+ equations = '\n'.join(nymodel)
528
+ return equations
529
+
530
+ def kaedeunroll(in_equations,funks=[]):
531
+ ''' unrolls a chain (kaede) expression - used in the SMEC moedel '''
532
+ nymodel=[]
533
+ equations=in_equations[:] # we want do change the e
534
+ # modelprint(equations)
535
+ for comment,command,value in find_statements(equations.upper()):
536
+ # print('>>',comment,'<',command,'>',value)
537
+ if comment:
538
+ nymodel.append(comment)
539
+ elif command=='FRML':
540
+ a,fr,n,udtryk=split_frml((command+' '+value).upper())
541
+ while 'KAEDE(' in udtryk.upper():
542
+ forkaede,kaedeudtryk,efterkaede=find_arg('KAEDE',udtryk)
543
+ arg=kaedeudtryk.split(',')
544
+ taller,navner='(','('
545
+ for i in range(len(arg)/2):
546
+ j=i*2
547
+ taller=taller+arg[j]+'*('+lagone(arg[j+1]+'/'+arg[j],funks=funks)+')+'
548
+ navner=navner+lagone(arg[j+1],funks=funks)+'+'
549
+ navner=navner[:-1]+')'
550
+ taller=taller[:-1]+')'
551
+ #print(taller,'/',navner)
552
+ udtryk=forkaede+'('+taller+'/'+navner+')'+efterkaede
553
+ while 'KAEDEP(' in udtryk.upper():
554
+ forkaede,kaedeudtryk,efterkaede=find_arg('KAEDEP',udtryk)
555
+ arg=kaedeudtryk.split(',')
556
+ #print(arg,len(arg)/2)
557
+ taller,navner='(','('
558
+ for i in range(len(arg)/2):
559
+ j=i*2
560
+ taller=taller+arg[j]+'*'+lagone(arg[j+1],funks=funks)+'+'
561
+ navner=navner+lagone(arg[j],funks=funks)+'+'+lagone(arg[j+1],funks=funks)+'+'
562
+ navner=navner[:-1]+')'
563
+ taller=taller[:-1]+')'
564
+ #print(taller,'/',navner)
565
+ udtryk=forkaede+'('+taller+'/'+navner+')'+efterkaede
566
+ while 'MOVAVG(' in udtryk.upper():
567
+ forkaede,kaedeudtryk,efterkaede=find_arg('MOVAVG',udtryk)
568
+ arg=kaedeudtryk.split(',',1)
569
+ avg='(('
570
+ term=arg[0]
571
+ #print(arg,len(arg)/2)
572
+ # breakpoint()
573
+ antal=int(arg[1])
574
+ for i in range(antal):
575
+ avg=avg[:]+term[:]+'+'
576
+ term=lagone(term,funks=funks)
577
+ avg=avg[:-1]+')/'+str(antal)+'.0)'
578
+ #print(taller,'/',navner)
579
+ udtryk=forkaede+avg+efterkaede
580
+ nymodel.append(command+' '+n+' '+udtryk)
581
+
582
+ else:
583
+ nymodel.append(command+' '+value)
584
+ equations='\n'.join(nymodel)
585
+ return equations
586
+
587
+ def check_syntax_frml(frml):
588
+ ''' check syntax of frml '''
589
+ try:
590
+ a, fr, n, udtryk = split_frml(frml)
591
+ ast.parse(re.sub(r'\n','',re.sub(' ','',udtryk[:-1])))
592
+ return True
593
+ except:
594
+ return False
595
+
596
+ def normalize_a_frml(frml,show=False):
597
+ ''' Normalize and show a frml'''
598
+ new = normalize(frml,sym=True)
599
+ if show:
600
+ print(f'Try normalizing :{frml} ')
601
+ print(f'After normalization :{new} ')
602
+ if not check_syntax_frml(new):
603
+ print(f'** ERROR in :{new} ')
604
+ print(f'**Normalization did not solve the problem in this formula')
605
+
606
+ return new
607
+
608
+ def nomalize_a_model(equations):
609
+ ''' a symbolic normalization is performed if there is a syntaxerror '''
610
+
611
+ out= '\n'.join([frml if check_syntax_frml(frml) else normalize_a_frml(frml,True) for frml in find_frml(equations)])
612
+ return out
613
+
614
+
615
+ def normalize(in_equations,sym=False,funks=[]):
616
+ ''' Normalize an equation with log or several variables at the left hand side, the first variable is considerd the endogeneeus'''
617
+ def findendo(ind):
618
+ ''' finds the equation for the first variable of a string'''
619
+ def endovar(f): # Finds the first variable in a expression
620
+ for t in udtryk_parse(f,funks=funks):
621
+ if t.var:
622
+ ud=t.var
623
+ break
624
+ return ud
625
+ ind=re.sub(r'LOG\(','log(',ind) # sypy uses lover case for log and exp
626
+ ind=re.sub(r'EXP\(','exp(',ind)
627
+ lhs,rhs=ind.split('=',1)
628
+ if len(udtryk_parse(lhs,funks=funks)) >=2: # we have an expression on the left hand side
629
+ # print('Before >>',ind)
630
+ endo=sympify(endovar(lhs))
631
+ kat=sympify('Eq('+lhs+','+rhs[0:-1]+')') # we take the the $ out
632
+ endo_frml=solve(kat,endo,simplify=False,rational=False)
633
+ # print('After >>', str(endo)+'=' + str(endo_frml[0])+'$')
634
+ return str(endo)+'=' + str(endo_frml[0])+'$'
635
+ else: # no need to solve this equation
636
+ return ind
637
+ def normalizesym(in_equations):
638
+ ''' Normalizes equations by using sympy '''
639
+ nymodel=[]
640
+ equations=in_equations[:] # we want do change the e
641
+ # modelprint(equations)
642
+ for comment,command,value in find_statements(equations.upper()):
643
+ # print('>>',comment,'<',command,'>',value)
644
+ if comment:
645
+ nymodel.append(comment)
646
+ elif command=='FRML':
647
+ a,fr,n,udtryk=split_frml((command+' '+value).upper())
648
+ while 'DLOG(' in udtryk.upper():
649
+ fordlog,dlogudtryk,efterdlog=find_arg('dlog',udtryk)
650
+ udtryk=fordlog+'diff(log('+dlogudtryk+'))'+efterdlog
651
+ while 'LOGIT(' in udtryk.upper():
652
+ forlogit,logitudtryk,efterlogit=find_arg('LOGIT',udtryk)
653
+ udtryk=forlogit+'(log('+logitudtryk+'/(1.0 -'+logitudtryk+')))'+efterlogit
654
+ while 'DIFF(' in udtryk.upper():
655
+ fordif,difudtryk,efterdif=find_arg('diff',udtryk)
656
+ udtryk=fordif+'(('+difudtryk+')-('+lagone(difudtryk+'',funks=funks)+'))'+efterdif
657
+ nymodel.append(command+' '+n+' '+findendo(udtryk))
658
+ else:
659
+ nymodel.append(command+' '+value)
660
+ equations='\n'.join(nymodel)
661
+ # modelprint(equations)
662
+ return equations
663
+ def normalizehift(in_equations):
664
+ ''' Normalizes equations by shuffeling terms around \n
665
+ can handel LOG, DLOG and DIF in the left hans side of = '''
666
+ nymodel=[]
667
+ equations=in_equations[:] # we want do change the e
668
+ # modelprint(equations)
669
+ for comment,command,value in find_statements(equations.upper()):
670
+ # print('>>',comment,'<',command,'>',value)
671
+ if comment:
672
+ nymodel.append(comment)
673
+ elif command=='FRML':
674
+ a,fr,n,udtryk=split_frml((command+' '+value).upper())
675
+ lhs,rhs=udtryk.split('=',1)
676
+ lhsterms=udtryk_parse(lhs,funks=funks)
677
+ if len(lhsterms) == 1: # only one term on the left hand side, no need to shuffel around
678
+ pass
679
+ elif len(lhsterms) == 4: # We need to normalixe expect funk(x) on the levt hand side funk=LOG,DLOG or DIFF
680
+ rhs=rhs[:-1].strip() # discharge the $ strip blanks for nice look
681
+ lhs=lhsterms[2].var # name of the dependent variablem, no syntax check here
682
+ if lhsterms[0].op == 'LOG':
683
+ rhs='EXP('+rhs+')'
684
+ udtryk=lhs+'='+rhs+'$' # now the equation is normalized
685
+ elif lhsterms[0].op == 'DIFF':
686
+ rhs=lagone(lhs,funks=funks)+'+('+rhs+')'
687
+ udtryk=lhs+'='+rhs+'$' # now the equation is normalized
688
+ elif lhsterms[0].op == 'DLOG':
689
+ rhs='EXP(LOG('+lagone(lhs,funks=funks)+')+'+rhs+')'
690
+ udtryk=lhs+'='+rhs+'$' # now the equation is normalized
691
+ elif lhsterms[0].op == 'LOGIT':
692
+ rhs='(exp('+rhs+')/(1+EXP('+rhs+')))'
693
+ udtryk=lhs+'='+rhs+'$' # now the equation is normalized
694
+ # else:
695
+ # print('*** ERROR operand not allowed left of =',lhs)
696
+ # print(lhsterms)
697
+ else:
698
+ pass
699
+ # print('*** Try to normalize relation for:',lhs)
700
+ # now the right hand side is expanded for DLOG and DIFF
701
+ while 'DLOG(' in udtryk.upper():
702
+ fordlog,dlogudtryk,efterdlog=find_arg('dlog',udtryk)
703
+ udtryk=fordlog+'diff(log('+dlogudtryk+'))'+efterdlog
704
+ while 'DIFF(' in udtryk.upper():
705
+ fordif,difudtryk,efterdif=find_arg('diff',udtryk)
706
+ udtryk=fordif+'(('+difudtryk+')-('+lagone(difudtryk+'',funks=funks)+'))'+efterdif
707
+ nymodel.append(command+' '+n+' '+udtryk)
708
+ else:
709
+ nymodel.append(command+' '+value)
710
+ equations='\n'.join(nymodel)
711
+ # modelprint(in_equations,'Before normalization and expansion')
712
+ # modelprint(equations, 'After normalization and expansion')
713
+ return equations
714
+ if sym :
715
+ equations1=normalizesym(in_equations)
716
+ else:
717
+ equations1=normalizehift(in_equations)
718
+ #equations2=normalizesym(equations1) # to speed up
719
+ return equations1
720
+
721
+
722
+ def udrul_model(model,norm=True):
723
+
724
+ return explode(model, norm)
725
+
726
+ def explode(model,norm=True,sym=False,funks=[],sep='\n'):
727
+ '''prepares a model from a model template.
728
+
729
+ Returns a expanded model which is ready to solve
730
+
731
+ Eksempel: model = udrul_model(MinModel.txt)'''
732
+ # breakpoint()
733
+ udrullet=tofrml(model,sep=sep)
734
+ modellist = list_extract(udrullet)
735
+ if norm : udrullet = normalize(udrullet,sym,funks=funks ) # to save time if we know that normalization is not needed
736
+ udrullet = lagarray_unroll(udrullet,funks=funks )
737
+ udrullet = exounroll(udrullet) # finaly the exogeneous and adjustment terms - if present - are handled
738
+ udrullet = dounloop(udrullet,listin=modellist) # we unroll the do loops
739
+ # breakpoint()
740
+ udrullet = sumunroll(udrullet,listin=modellist) # then we unroll the sum
741
+ udrullet = creatematrix(udrullet,listin=modellist)
742
+ udrullet = createarray(udrullet,listin=modellist)
743
+ udrullet = argunroll(udrullet,listin=modellist)
744
+
745
+ return udrullet
746
+
747
+
748
+ def modelprint(ind, title=' A model', udfil='', short=0):
749
+ ''' prettyprinter for a a model.
750
+ :udfil: if present is output file
751
+ :short: if present condences the model
752
+ Can handle both model templates and models '''
753
+ import sys
754
+ f = sys.stdout
755
+ if udfil:
756
+ f = open(udfil, 'w+')
757
+ maxlenfnavn, maxlenlhs, maxlenrhs = 0, 0, 0
758
+ for comment, command, value in find_statements(ind):
759
+ if command.upper() == 'FRML':
760
+ a, fr, n, udtryk = split_frml(command.upper() + ' ' + value)
761
+ lhs, rhs = udtryk.split('=', 1)
762
+ maxlenfnavn = max(maxlenfnavn, len(n)) # Finds the max length of frml name
763
+ maxlenlhs = max(maxlenlhs, len(lhs)) # finds the max length of left hand variable
764
+ print('! '+ title, file=f)
765
+ dolevel = 0
766
+ for comment, command, value in find_statements(ind):
767
+ print(' ' * dolevel, end='', file=f)
768
+ if comment:
769
+ print(comment, file=f)
770
+ else:
771
+ if command.upper() == 'FRML':
772
+ a, fr, n, udtryk = split_frml(
773
+ command.upper() + ' ' + value)
774
+ lhs , rhs = udtryk.split('=', 1)
775
+ if short:
776
+ print(fr.ljust(5),'X',lhs.strip().ljust(maxlenlhs),
777
+ '=', rhs , file=f)
778
+ else:
779
+ rhs=re.sub(' +',' ',rhs)
780
+ print(
781
+ fr.ljust(5),
782
+ n.strip().ljust(maxlenfnavn),
783
+ lhs.strip().ljust(maxlenlhs),
784
+ '=',
785
+ rhs.replace('\n','\n'+(' ' * dolevel)+(' '*(10+maxlenfnavn+maxlenlhs))),
786
+ file=f)
787
+ elif command.upper() == 'DO':
788
+ print(command, ' ', value, file=f)
789
+ dolevel = dolevel + 1
790
+ elif command.upper() == 'ENDDO':
791
+ print(command, ' ', value, file=f)
792
+ dolevel = dolevel - 1
793
+ else:
794
+ print(command, ' ', value, file=f)
795
+
796
+
797
+
798
+ def lagone(ind,funks=[],laglead=-1):
799
+ ''' All variables in a string i
800
+ s lagged one more time '''
801
+ nt=udtryk_parse(ind,funks=funks)
802
+ fib=[]
803
+ for t in nt:
804
+ if t.op:
805
+ ud=t.op
806
+ elif t.number:
807
+ ud=t.number
808
+ elif t.var:
809
+ lag=t.lag if t.lag else '0'
810
+ org_lag = int(lag)
811
+ new_lag = org_lag+laglead
812
+ if new_lag == 0:
813
+ ud = t.var
814
+ else:
815
+ ud= t.var+ f'({new_lag:+})'
816
+ fib.append(ud)
817
+ return ''.join(fib)
818
+
819
+ def lag_n(udtryk,n=1,funks=[],laglead=-1):
820
+ new=udtryk
821
+ for i in range(n):
822
+ new=lagone(new,funks=funks,laglead=laglead)
823
+ return f'({new})'
824
+ lag_n('a+b',n=3,laglead=1)
825
+
826
+ def lag_n_tup(udtryk,n=-1,funks=[]):
827
+ ''' return a tuppel og lagged expressions from lag = 0 to lag = n)'''
828
+ new=udtryk.upper()
829
+ res=''
830
+ for i in range(abs(n)):
831
+ new=lagone(new,funks=funks,laglead=-1 if n < 0 else 1 )
832
+ res = res + new +','
833
+ return f'{res[:-1]}'
834
+ lag_n_tup('a',n=-3)
835
+
836
+
837
+
838
+ def pastestring(ind,post,funks=[],onlylags = False):
839
+ ''' All variable names in a in a string **ind** is pasted with the string **post**
840
+
841
+ This function can be used to awoid variable name conflict with the internal variable names in sympy.
842
+
843
+ an advanced function
844
+ '''
845
+ nt=udtryk_parse(ind,funks=funks)
846
+ fib=[]
847
+ for t in nt:
848
+ if t.op:
849
+ ud=t.op
850
+ elif t.number:
851
+ ud=t.number
852
+ elif t.var:
853
+ if onlylags:
854
+ if t.lag:
855
+ ud= t.var+ post.upper()+ ('('+(str(int(t.lag)))+')' if t.lag else '')
856
+ else:
857
+ ud= t.var
858
+ else:
859
+ ud= t.var+ post+ ('('+(str(int(t.lag)))+')' if t.lag else '')
860
+
861
+ fib.append(ud)
862
+ return ''.join(fib)
863
+
864
+ def stripstring(ind,post,funks=[]):
865
+ ''' All variable names in a in a string is **ind** is stripped of the string **post**.
866
+
867
+ This function reverses the pastestring process
868
+ '''
869
+ nt=udtryk_parse(ind,funks=funks)
870
+ fib=[]
871
+ lenpost=len(post)
872
+ for t in nt:
873
+ if t.op:
874
+ ud=t.op
875
+ elif t.number:
876
+ ud=t.number
877
+ elif t.var:
878
+ if t.var.endswith(post.upper()):
879
+ ud= t.var[:-lenpost]+ ('('+(str(int(t.lag)))+')' if t.lag else '')
880
+ else:
881
+ ud= t.var+ ('('+(str(int(t.lag)))+')' if t.lag else '')
882
+
883
+ # print('Tries to strip '+post +' from '+ind)
884
+ fib.append(ud)
885
+ return ''.join(fib)
886
+
887
+
888
+ def findindex(ind00):
889
+ ''' find the index variables meaning variables on the left hand side of = braced by {} '''
890
+ ind0 = ind00.strip()
891
+ if ind0.startswith('<'):
892
+ ind = ind0[ind0.index('>')+1:].strip()
893
+ else:
894
+ ind = ind0
895
+ lhs=ind.split('=')[0]
896
+ return (re.findall('\{([A-Za-z][\w]*)\}',lhs )) # all the index variables
897
+
898
+ def doablelist(expressions,sep='\n'):
899
+ ''' create a list of tupels from expressions seperated by sep,
900
+ each element in the list is a tupel (index, number og expression, the expression)
901
+
902
+ we want make group the expressions acording to index
903
+ index is elements on the left of = braced by {}
904
+ '''
905
+
906
+ lt = [(findindex(l.strip()),i,l.strip()) for i,l in enumerate(expressions.split(sep))]
907
+ out = sorted(lt,key = lambda x: (x[0],1./(x[1]+1.)),reverse=True)
908
+ return out
909
+
910
+ def dosubst(index,formular):
911
+ out=formular[:]
912
+ for nr,i in enumerate(index):
913
+ assert nr <= 9 , 'To many indeces in'+formular
914
+ out = re.sub(r'\%'+str(nr),'{'+i+'}',out)
915
+ out = re.sub(r'\%i','__'.join(['{'+i+'}' for i in index]),out)
916
+ return out
917
+
918
+
919
+ def doablekeep(formulars):
920
+ ''' takes index in the lhs and creates a do loop around the lines with same indexes
921
+ on the right side you can use %0_, %1_ an so on to indicate the index, just to awoid typing to much
922
+
923
+ Also %i_ will be changed to all the indexes'''
924
+
925
+
926
+ sep = '$' if '$' in formulars else '\n'
927
+ xx = doablelist(formulars,sep)
928
+ out = []
929
+ listout = []
930
+ for xxx,ee in groupby(xx,key=lambda x : x[0]):
931
+ if xxx:
932
+ pre = ''.join([' '*i+ 'Do '+x+'_list $\n' for i,x in enumerate(xxx)])
933
+ post = ''.join([' '*(len(xxx)-1-i)+'Enddo $ \n' for i,x in enumerate(xxx)]) +'\n'
934
+ frml = ''.join([' '*(len(xxx))+dosubst(xxx,e[2])+ ' $ \n' for e in ee ])
935
+ out.append([pre + frml + post])
936
+ else:
937
+ for l,number,exp in ee:
938
+ if exp.upper().startswith('LIST'):
939
+ listout.append([exp + ' $ \n'])
940
+ elif len(exp) :
941
+ # print(exp)
942
+ out.append([exp+ ' $ \n'])
943
+ return ''.join(chain(*listout,*out))
944
+ ##%%
945
+ def doable(formulars,funks=[]):
946
+ ''' takes index in the lhs and creates a do loop around the line
947
+ on the right side you can use %0_, %1_ an so on to indicate the index, just to awoid typing to much
948
+
949
+ Also %i_ will be changed to all the indexes'''
950
+
951
+ def endovar(f,funks=[]): # Finds the first variable in a expression
952
+ for t in udtryk_parse(f,funks=funks):
953
+ if t.var:
954
+ ud=t.var
955
+ break
956
+ return ud
957
+
958
+
959
+ sep = '$' if '$' in formulars else '\n'
960
+ lt = [(findindex(l.strip()),i,l.strip()) for i,l in enumerate(formulars.split(sep))]
961
+
962
+ out = []
963
+ for xxx,number,exp in lt:
964
+ if xxx:
965
+ pre = ''.join([' '*i+ 'Do '+x+'_list $\n' for i,x in enumerate(xxx)])
966
+ post = ''.join([' '* (len(xxx)-1-i)+'Enddo $ \n' for i,x in enumerate(xxx)]) +'\n'
967
+ frml = ' '*(len(xxx)) + dosubst(xxx,exp)+ ' $ \n'
968
+ out.append(pre + frml + post)
969
+ ind = exp.strip()
970
+
971
+ if ind.startswith('<'):
972
+ frml_name = re.findall('\<.*?\>',ind)[0]
973
+ sumname = kw_frml_name(frml_name, 'sum')
974
+ if sumname:
975
+ lhs= ind.split('>',1)[1].split('=',1)[0]
976
+ lhsvar = endovar(lhs,funks=funks)
977
+ lhsvar_stub = lhsvar.split('{',1)[0]
978
+ sumlist = ''.join([f'sum({x}_list,' for x in xxx])+lhsvar+')'*len(xxx)
979
+ out.append(f'{lhsvar_stub}{sumname} = {sumlist} $ \n\n')
980
+ elif len(exp) :
981
+ out.append(exp+ ' $ \n')
982
+ return ''.join(out).replace('$','') # the replace is a quick fix
983
+
984
+ #% test doable
985
+ frml = '''
986
+ list sectors_list = sectors : a b
987
+ list banks_list = banks : hest ko
988
+
989
+ a__{banks}__{sectors} = b
990
+ <sum= all> b__{sectors}__{banks} = b
991
+ <sum= all> diff(xx__{sectors}__{banks}) = 42
992
+ '''.upper()
993
+ testfrml = (doable(frml))
994
+ # print(explode(testfrml))
995
+
996
+ ##%%
997
+ def findindex_gams(ind00):
998
+ '''
999
+ - an equation looks like this
1000
+ - <frmlname> [index] lhs = rhs
1001
+
1002
+ this function find frmlname and index variables on the left hand side. meaning variables braced by {} '''
1003
+ ind0 = ind00.strip()
1004
+ if ind0.startswith('<'):
1005
+ frmlname = re.findall('\<.*?\>',ind0)[0]
1006
+ ind = ind0[ind0.index('>')+1:].strip()
1007
+ else:
1008
+ frmlname='<>'
1009
+ ind=ind0.strip()
1010
+
1011
+ if ind.startswith('['):
1012
+ allindex = re.findall('\[.*?\]',ind0)[0]
1013
+ index = allindex[1:-1].split(',')
1014
+ rest = ind[ind.index(']')+1:]
1015
+ else:
1016
+ index = []
1017
+ rest = ind
1018
+ return frmlname,index,rest
1019
+
1020
+ def un_normalize_expression(frml) :
1021
+ '''This function makes sure that all formulas are unnormalized.
1022
+ if the formula is already decorated with <endo=name> this is kept
1023
+ else the lhs_varriable is used in <endo=>
1024
+ '''
1025
+ frml_name,frml_index,frml_rest = findindex_gams(frml.upper())
1026
+ this_endo = kw_frml_name(frml_name.upper(), 'ENDO')
1027
+ # breakpoint()
1028
+ lhs,rhs = frml_rest.split('=')
1029
+ if this_endo:
1030
+ lhs_var = this_endo.strip()
1031
+ frml_name_out = frml_name
1032
+ else:
1033
+ lhs_var = lhs.strip()
1034
+ # frml_name_out = f'<endo={lhs_var}>' if frml_name == '<>' else f'{frml_name[:-1]},endo={lhs_var}>'
1035
+ frml_name_out = frml_name[:]
1036
+ # print(this_endo)
1037
+ new_rest = f'{lhs_var}___res = ( {rhs.strip()} ) - ( {lhs.strip()} )'
1038
+ return f'{frml_name_out} {frml_index if len(frml_index) else ""} {new_rest}'
1039
+
1040
+
1041
+ def un_normalize_model(in_equations,funks=[]):
1042
+ ''' un normalize a model '''
1043
+ nymodel=[]
1044
+ equations=in_equations.upper() # we want do change the e
1045
+ # modelprint(equations)
1046
+ for comment,command,value in find_statements(equations):
1047
+ # print('>>',comment,'<',command,'>',value)
1048
+ # breakpoint()
1049
+ if comment:
1050
+ nymodel.append(comment)
1051
+ elif command=='FRML':
1052
+ un_frml = un_normalize_expression(value[:-1])
1053
+ nymodel.append(f'FRML {un_frml} $')
1054
+ else:
1055
+ nymodel.append(command+' '+value)
1056
+ equations='\n'.join(nymodel)
1057
+ return equations
1058
+
1059
+ def un_normalize_simpel(in_equations,funks=[]):
1060
+ ''' un-normalize expressions delimeted by linebreaks'''
1061
+ edm = '\n'.join(un_normalize_expression(f) for f in in_equations.split('\n') if len(f.strip()))
1062
+ fdm = explode(edm)
1063
+ return fdm
1064
+
1065
+
1066
+ def eksempel(ind):
1067
+ ''' takes a template model as input, creates a model and a histmodel and prints the models'''
1068
+ abe = udrul_model(ind)
1069
+ ko = find_hist_model(abe)
1070
+ modelprint(ind, 'Model template')
1071
+ modelprint(abe, 'Unrolled model')
1072
+ modelprint(ko, 'Historic model')
1073
+ # print('Hej fra modelmanipulation')
1074
+ if __name__ == '__main__' and 1 :
1075
+ #%%
1076
+ print(sub_frml(a['bankdic'],'Dette er {bank}'))
1077
+ print(sub_frml(a['bankdic'],'Dette er {bank}',sep=' and '))
1078
+ print(sub_frml(a['bankdic'],'Dette er {bank}',plus='+',sep=''))
1079
+ print(sub_frml(a['bankdic'],'Dette er {bank}',xvar='danske',lig='no'))
1080
+ print(sub_frml(a['bankdic'],'Dette er {bank}',xvar='danske',lig='yes'))
1081
+ print(sub_frml(a['bankdic'],'Dette er {bank}'))
1082
+
1083
+ #%%
1084
+ fmodel= '''
1085
+ list BANKDIC = bank : Danske , Nordea $
1086
+ list countryDIC = country : Uk , DK, SE , NO , IR, GE US AS AT CA$
1087
+ list countrydanske = country : uk , DK, IR $
1088
+ list countrynordea = country: SE , DK, AT $
1089
+ do bankdic$
1090
+ frml x {bank}_income = {bank}_a +{bank}_b $
1091
+ do country{bank} $
1092
+ frml x {bank}_{country} = 4242 $
1093
+ enddo $
1094
+ do countrydic $
1095
+ frml x {bank}_{country}_all = 42 $
1096
+ enddo $
1097
+ enddo $ '''
1098
+
1099
+ print(dounloop(fmodel))
1100
+ #%%
1101
+ print(stripstring(pastestring('a+b+log(x)','_xx'),'_xx'))
1102
+ split_frml ('FRML x ib =1+gris $')
1103
+ split_frml ('FRML <res> ib =1+gris $')
1104
+ find_statements(' FRML x ib =1+gris $ frml <exo> hane= 27*ged$')
1105
+ find_statements('! FRML x ib =1+gris $ \n frml <exo> hane=27*ged $')
1106
+ find_statements('FRML x ib =1+gris*(ko+1) $ ! Comment \n frml <exo> hane= ged $')
1107
+ sub('O {who} of {from}',{'who':'Knights','from':'Ni'})
1108
+ sub('O {who} of {from}, , we have brought you your {weed}',{'who':'Knights','from':'Ni'})
1109
+ sub_frml({'weed':['scrubbery','herring']},'we have brought you your {weed}')
1110
+ sub_frml({'weed':['scrubbery','herring'],'where':['land','sea']},'we have brought you your {weed} from {where} ')
1111
+ sub_frml({'weed':['scrubbery','herring'],'where':['land','sea']},'we have brought you your {weed} from {where} ')
1112
+ a={'bankdic': {'bank':['Danske','Nordea'],'danske':['yes','no']}}
1113
+ sub_frml(a['bankdic'],'Dette er {bank}')
1114
+ sub_frml(a['bankdic'],'Dette er {bank}',sep=' and ')
1115
+ sub_frml(a['bankdic'],'Dette er {bank}',plus='+',sep='')
1116
+ sub_frml(a['bankdic'],'Dette er {bank}',xvar='danske',lig='yes')
1117
+ sub_frml(a['bankdic'],'Dette er {bank}',xvar='danske',lig='no')
1118
+ sub_frml(a['bankdic'],'Dette er {bank}')
1119
+ list_extract('list bankdic = bank : Danske , Nordea / danske : yes , no $')
1120
+ kw_frml_name('<res=abe>','res')
1121
+ kw_frml_name('<res=abe,animal>','animal')
1122
+ find_res('FRML <res=x> ib =x+y+v +ib(-1)$')
1123
+ find_res('FRML <res=J> ib =x+y+v + ib_j $')
1124
+ find_res('FRML <res=JR> ib =(x+y+v + ib_j)*(1+ ib_JR) $)')
1125
+ find_hist_model('FRML <res=x> ib =x+y+v $ frml <ident> y=c+i+x-m $')
1126
+ (exounroll('frml <j> ib=x+y $ frml <jr> ib=x+y $ frml <j,jr'))
1127
+ find_arg('log','x=log(a+b)+77')
1128
+ find_arg('log','x=log(a+log(b))+77')
1129
+
1130
+ kaedeunroll('FRML <res=x> ib =x+y+lAG_ARRAY(v,3) $')
1131
+ #%%
1132
+ x= (dounloop(
1133
+ '''list bankdic = bank : Danske , Nordea /
1134
+ ko : yes , no $
1135
+ do bankdic ko = no $
1136
+ frml x {bank}_income = {bank}_a +{bank}_b $
1137
+ enddo $
1138
+ frml x ialt=sum(bankdic ko= yes,{bank}_income ) $'''))
1139
+ # breakpoint()
1140
+ print(sumunroll(x))
1141
+ #%%
1142
+ print(sumunroll(dounloop(
1143
+ '''list BANKDIC = bank : Danske , Nordea $
1144
+ do BANKDIC $
1145
+ frml x {bank}_income = {bank}_a +{bank}_b $
1146
+ enddo $
1147
+ frml x ialt=sum(bankdic,{bank}_income) $''')))
1148
+ #%%
1149
+ # Trying some of the features:
1150
+ fmodel='''
1151
+ list BANKDIC = currentbank : Danske , Nordea , Jyske $
1152
+ list BANKDIC2 = loantobank : Danske , Nordea , Jyske $
1153
+ list BANKDIC3 = loanfrombank : Danske , Nordea , Jyske $
1154
+ frml <matrix> msigma = to_matrix(bankdic,bankdic2,cov_{currentbank}_{loantobank} ) $
1155
+ frml <matrix> vreturn = to_matrix(bankdic2,return_{loantobank}) $
1156
+ frml <matrix> vcapitalweights = to_matrix(bankdic2,capitalweights_{loantobank}) $
1157
+
1158
+ do BANKDIC $
1159
+ do bankdic2 $
1160
+ frml setmax loanmax_{currentbank}_to_{loantobank} = {currentbank}_capital*0.25 $
1161
+ frml setmin loanmin_{currentbank}_to_{loantobank} = 0.0 $
1162
+ enddo$
1163
+ frml setmax loanmax_{currentbank}_to_{currentbank} = 0.0 $
1164
+ frml <matrix> {currentbank}_loan_max =to_matrix(bankdic2,loan_max_{currentbank}_to_{loantobank}) $
1165
+ frml <matrix> {currentbank}_loan_min =to_matrix(bankdic2,loan_min_{currentbank}_to_{loantobank}) $
1166
+ enddo $
1167
+
1168
+ do Bankdic $
1169
+ frml <matrix> loan_{currentbank}_to
1170
+ = mv_opt(msigma,vreturn,riskaversion, {currentbank}_totalloan, vcapitalweights, {currentbank}_capital,
1171
+ {currentbank}_loan_max,{currentbank}_loan_min) $
1172
+ enddo $
1173
+ do Bankdic $
1174
+ frml x {currentbank}_loancheck=sum(bankdic2,{currentbank}_to_{loantobank}_loan)$
1175
+ enddo $ '''
1176
+ modelprint(fmodel)
1177
+ modelprint(sumunroll(argunroll(creatematrix(dounloop(fmodel)))))
1178
+ #%%
1179
+ def f1(x):
1180
+ return x
1181
+
1182
+ eq='''FRML <> logit(pd_country_sector) =
1183
+ 0.008 + 0.44 * logit(pd_country_sector(-1)) - 0.18 * gdp_country -0.05*gdp_country(-1) + 0.12 * URX_country + 0.02 * sltn_country + 0.11 * nsltn_country(-1) $ '''
1184
+ # eq='''FRML <> logit(cow) = logit(1+1) + diff(a) $ '''
1185
+ # eq='''FRML <> x = diff(f1(a)) $ '''
1186
+ print(eq)
1187
+ print('After normalization' )
1188
+ neq=normalize(eq,funks=[f1]).lower()
1189
+ print(neq)
1190
+ toteq=eq.lower()+neq
1191
+
1192
+ #%%
1193
+
1194
+ org ='''
1195
+ ! credit risk
1196
+ ! calculate pd for each portefolio sector based on bank specific starting point
1197
+ ! remember to do a .replace('%seg%','{bank}__{CrCountry}__{PortSeg}')
1198
+ do banklist $
1199
+ do {bank}CrCoList $
1200
+ do PortSegList $
1201
+ ! non defaultet stock
1202
+ non_def_exp_end_y__%seg% = non_def_exp_end_y__%seg%(-1)*(1-pd_pit__Q__%seg%) $
1203
+
1204
+ ! for this prototype we just use last period LGD
1205
+ Lgd_pit_new_scen__%seg% = Lgd_pit_new_scen__%seg%(-1) $
1206
+
1207
+ ! gross impairemens (losses) newly defaulted stock
1208
+ gross_imp_loss_new_def__%seg% = non_def_exp_end_y__%seg%(-1)*pd_pit__Q__%seg%*Lgd_pit_new_scen__%seg% $
1209
+
1210
+ ! share of provision stock which can ofset the impairements
1211
+ alfa__%seg% = pd_pit__Q__%seg% $
1212
+
1213
+ part of provision stock which can ofset losses
1214
+ prov_release_new_def__%seg% = alfa__%seg% * prov_stock_end_y__%seg%(-1) $
1215
+
1216
+ ! net impairemens on newly defaulted stock, can only contribute to loss
1217
+ imp_loss_new_def__%seg% = max(0.0, gross_imp_loss_new_def__%seg% - prov_release_new_def__%seg%) $
1218
+
1219
+ ! update the stock of provisions relateed to newly defaulted amount after release to cover impairement
1220
+ prov_stock_end_y__%seg% = prov_stock_end_y__%seg%(-1) + diff(imp_loss_new_def__%seg%) $
1221
+
1222
+ ! now we look at previous defaulted loans
1223
+
1224
+ ! defaulted exposure
1225
+ def_stock_end_y__%seg% = def_stock_end_y__%seg%(-1) + non_def_exp_end_y__%seg%(-1)*pd_pit__Q__%seg% $
1226
+
1227
+ ! impairement old defaulted stock, can not contribute to profit only loss
1228
+ imp_loss_old_def__%seg% = max(0,diff(Lgd_pit_old_scen__%seg%)* def_stock_end_y__%seg%(-1)) $
1229
+
1230
+ ! amount of provisions stock relateed to old defaulted stock
1231
+ prov_stock_end_y_def__%seg% = prov_stock_end_y_def__%seg%(-1)+imp_loss_old_def__%seg% $
1232
+
1233
+ ! total impairment
1234
+ imp_loss_total_def__%seg% = imp_loss_old_def__%seg% +imp_loss_new_def__%seg% $
1235
+
1236
+ enddo $
1237
+ enddo $
1238
+ frml <> imp_loss_total_def__{bank} = sum({bank}CrCoList,sum(PortSegList,imp_loss_total_def__%seg%)) $
1239
+
1240
+ enddo $
1241
+
1242
+
1243
+ '''
1244
+
1245
+ print((doable(org)))
1246
+ print(tofrml(doable(org),sep='$'))
1247
+
1248
+ print(tofrml('a=b \n c=55 ',sep='\n'))
1249
+ #%%
1250
+ model = '''\
1251
+ !jjjfj
1252
+ list alist = al : a b c
1253
+ a = c(-1) + b(-1)
1254
+ x = 0.5 * c
1255
+ d = x + 3 * a(-1)
1256
+ '''
1257
+ print(tofrml(model))
1258
+
1259
+
1260
+ #%% testtofrml
1261
+ mtest = '''
1262
+ list banklist = bank : ib soren marie /
1263
+ country : denmark sweden denmark $
1264
+
1265
+ do banklist $
1266
+ ! kkdddk
1267
+ frml <> profit_{bank} = revenue_{bank} - lag_array(-3,expenses_{bank}) $
1268
+ frml <> expenses_{bank} = factor_{country} * revenue_{bank} $
1269
+ enddo $
1270
+ '''
1271
+
1272
+ print(tofrml(mtest))
1273
+ print(explode(mtest))