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.
model_latex_classpy.py ADDED
@@ -0,0 +1,401 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on October 2 2022
4
+ Based on model_latex module
5
+ using dataclases
6
+
7
+ @author: hanseni
8
+
9
+ Mostly to eat latex models and translate to business logic
10
+
11
+ The routines are specific to a style of latex and should be inspected before use
12
+
13
+ """
14
+ from IPython.display import display, Math, Latex, Markdown , Image
15
+ import re
16
+ from IPython.lib.latextools import latex_to_png
17
+ from pathlib import Path
18
+ import os
19
+ import re
20
+ from dataclasses import dataclass,field
21
+
22
+
23
+ import modelmanipulation as mp
24
+ import modelclass as mc
25
+ import modelpattern as pt
26
+ def rebank(model):
27
+ ''' All variable names are decorated by a {bank}
28
+ The {bank} is injected as the first dimension '''
29
+
30
+ ypat = re.compile(pt.namepat+pt.lagpat)
31
+ funk = set(pt.funkname) | {'SUM'}
32
+ nobank = set('N S T __{bank} NORM PPF CDF'.split()) # specific variable names not to be decorated
33
+ notouch = funk | nobank # names not to be decorated
34
+ def trans(matchobj):
35
+ ''' The function recieves a matchobj entity. The matching groups can be accesed by matchobj.group()
36
+ it returns a string with a bankname added at the end or at the first __ which marks dimensions '''
37
+ var = matchobj.group(1)
38
+ lag = '(' + matchobj.group(2) + ')'if matchobj.group(2) else ''
39
+ if var.upper() in notouch:
40
+ return var+lag
41
+ else:
42
+ if '__' in var:
43
+ pre,post = var.split('__',1)
44
+ post = ''+post
45
+ else:
46
+ pre = var
47
+ post = ''
48
+
49
+ return (pre+'__{bank}'+post + lag)
50
+
51
+ banked = ypat.sub(trans, model)
52
+ return banked
53
+
54
+
55
+ def defrack(streng):
56
+ '''
57
+ \frac{xxx}{yyy} = ((xxx)/(yyy))
58
+
59
+ '''
60
+ tstreng = streng[:]
61
+ tfunks = [r'\frac{',r'\dfrac{']
62
+ for tfunk in tfunks :
63
+ while tfunk in tstreng:
64
+ start = tstreng.find(tfunk)
65
+ # first find the first matching {}
66
+ match = tstreng[start + len(tfunk):] # the rest of the string in which we have to match }
67
+ open = 1 # we already found the first {
68
+ for index1 in range(len(match)):
69
+ if match[index1] in '{}':
70
+ open = (open + 1) if match[index1] == '{' else (open - 1)
71
+ if not open:
72
+ break
73
+ # now find the second matching {}
74
+ match2 = match[index1 + 1+1:] # the string from the location of second { to the end of string
75
+ open=1
76
+ for index2 in range(len(match2)):
77
+ if match2[index2] in '{}':
78
+ open = (open + 1) if match2[index2] == '{' else (open - 1)
79
+ if not open:
80
+ break
81
+ tstreng = tstreng[:start]+ '(('+ match[:index1] +')/('+ match2[:index2]+'))'+match2[index2+1:]
82
+ return tstreng
83
+
84
+ def depower(streng):
85
+ '''
86
+ ^{(xxx)} = **(xxx)
87
+
88
+ '''
89
+ tstreng = streng[:]
90
+ tfunk = r'^{'
91
+ while tfunk in tstreng:
92
+ start = tstreng.find(tfunk)
93
+ match = tstreng[start + len(tfunk):]
94
+ open=1
95
+ for index in range(len(match)):
96
+ if match[index] in '{}':
97
+ open = (open + 1) if match[index] == '{' else (open - 1)
98
+ if not open:
99
+ break
100
+ tstreng = tstreng[:start]+ '**('+ match[:index] +')' + match[index+1:]
101
+ return tstreng
102
+
103
+
104
+ def debrace(streng):
105
+ '''
106
+ Eliminates underbrace{xxx}_{yyy} in a string
107
+ underbrace{xxx}_{yyy} => (xxx)
108
+ As there can be nested {} we need to match the braces
109
+
110
+ '''
111
+ tstreng = streng[:]
112
+ tfunk = r'\underbrace{'
113
+ while tfunk in tstreng:
114
+ start = tstreng.find(tfunk)
115
+ match = tstreng[start + len(tfunk):]
116
+ open = 1
117
+ for index1 in range(len(match)):
118
+ if match[index1] in '{}':
119
+ open = (open + 1) if match[index1] == '{' else (open - 1)
120
+ if not open:
121
+ break
122
+ goodstuf = tstreng[:start]+match[index1]
123
+ match2 = match[index1 + 1+2:]
124
+ open=1
125
+ for index2 in range(len(match2)):
126
+ if match2[index2] in '{}':
127
+ open = (open + 1) if match2[index2] == '{' else (open - 1)
128
+ if not open:
129
+ break
130
+ tstreng = tstreng[:start]+ ''+ match[:index1] +''+ match2[index2+1:]
131
+ return tstreng
132
+
133
+ def defunk(funk, subs , streng,startp='{',slutp='}'):
134
+ '''
135
+ \funk{xxx} => subs(xxx)
136
+
137
+ in a string
138
+ '''
139
+ tfunk, tstreng = funk[:] , streng[:]
140
+ tfunk = tfunk + startp
141
+ while tfunk in tstreng:
142
+ start = tstreng.find(tfunk)
143
+ match = tstreng[start + len(tfunk):]
144
+ open = 1
145
+ for index in range(len(match)):
146
+ if match[index] in startp+slutp:
147
+ open = (open + 1) if match[index] == startp else (open - 1)
148
+ if not open:
149
+ break
150
+ tstreng = tstreng[:start]+subs+'(' + match[:index] +')' + match[index + 1:]
151
+ return tstreng
152
+ #print(defunk(r'\sqrt',r'sqrt',r'a=\sqrt{b+ \sqrt{f+y}}'))
153
+ #print(defunk(r'\log',r'log',r'a=\log(b(-1)+ \log({f+y}))',startp='(',slutp=')'))
154
+
155
+ def findindex(ind):
156
+ ''' find the index variables on the left hand side. meaning variables braced by {} '''
157
+ lhs=ind.split('=')[0]
158
+ return re.findall('\{([A-Za-z][\w]*)\}',lhs ) # all the index variables
159
+
160
+ def doableold(ind,show=False):
161
+ ''' find all dimensions in the left hand side of = and and decorate with the nessecary do .. enddo '''
162
+
163
+ indexvar = findindex(ind) # all the index variables
164
+ if indexvar :
165
+ pre = ' $ '.join(['Do '+i for level,i in enumerate(indexvar)])+' $ \n '
166
+ post = '\n' + 'enddo $ '*len(indexvar)
167
+ out = pre+ind + post
168
+ if show:
169
+ print('Before doable',ind,sep='\n')
170
+ print('After doable',out,sep='\n')
171
+ print()
172
+ else:
173
+ out=ind
174
+ return out
175
+
176
+
177
+ def findlists(input):
178
+ '''extracte list with sublist from latex'''
179
+ relevant = re.findall(r'LIST \s*\$[^$]*\$',input.upper())
180
+ # print(f'{relevant=}')
181
+ temp1 = [l.replace('$','').replace('\\','').replace(',',' ')
182
+ .replace('{','').replace('}','').replace('\n','/ \n')
183
+ for l in relevant]
184
+ # print(f'{temp1=}')
185
+ temp2 = [l.split('=')[0]+' = '
186
+ + l.split('=')[0][4:]
187
+ +' : '+ l.split('=')[1]+'$' for l in temp1]
188
+ return temp2
189
+
190
+ if 1:
191
+ listtest='''
192
+ List $ stage=\{s1, s2,s3\} \\
193
+ stagened:\{ 0, 0, 1,\} $
194
+ '''
195
+ print(findlists(listtest))
196
+
197
+ def findallindex(ind0):
198
+ '''
199
+ - an equation looks like this
200
+ - <frmlname> [do_condition,...] lhs = rhs
201
+ - indicies are identified as __{} on the left hand side.
202
+
203
+ this function find frmlname and index variables on the left hand side. meaning variables braced by {} '''
204
+ if ind0.startswith('<'):
205
+ frmlname = re.findall('\<.*?\>',ind0)[0]
206
+ ind = ind0[ind0.index('>')+1:].strip()
207
+ else:
208
+ frmlname='<>'
209
+ ind=ind0.strip()
210
+ print(f'{ind=}')
211
+
212
+ if ind.startswith('['):
213
+ do_conditions = ind[1:ind.index(']')]
214
+ rest = ind[ind.index(']')+1:].strip()
215
+ all_do_condition = {condition.split('.')[0] : condition.split('.')[1] for condition in do_conditions.split(',')}
216
+ print(f'{do_conditions=}')
217
+ else:
218
+ all_do_condition = dict()
219
+ rest = ind.strip()
220
+
221
+ lhs=ind.split('=')[0]
222
+ do_indicies = re.findall('__\{([A-Za-z][\w]*)\}',lhs ) # all the index variables
223
+
224
+ do_indicies_dict = {ind : all_do_condition.get(ind) for ind in do_indicies}
225
+
226
+ return frmlname,do_indicies_dict,rest
227
+
228
+ def do_list(do_index,do_condition):
229
+ ''' do do_index_list do_condition = 1 $ '''
230
+
231
+
232
+ if do_condition:
233
+ out = f'do {do_index} {do_condition} = 1 $'
234
+ else:
235
+ out = f'do {do_index}_list $'
236
+ return out
237
+
238
+ def doable(ind,show=False):
239
+ ''' find all dimensions in the left hand side of = and and decorate with the nessecary do .. enddo '''
240
+
241
+ frmlname,do_indicies_dict,rest = findallindex(ind) # all the index variables
242
+ # breakpoint()
243
+
244
+ print(f'{do_indicies_dict=}*******************')
245
+ if do_indicies_dict :
246
+ pre = ' '.join([do_list(i,c) for i,c in do_indicies_dict.items() ])
247
+ # print(f'{pre=}*******************')
248
+
249
+ post = 'enddo $ '*len(do_indicies_dict)
250
+ out = f'{pre}\n {frmlname} {rest} $\n{post}'
251
+ else:
252
+ out=f'frml {rest.strip()} $'
253
+
254
+ if show:
255
+ print('\nBefore doable',ind,sep='\n')
256
+ print('After doable',out,sep='\n')
257
+ return out
258
+ #%% class
259
+
260
+ @dataclass
261
+ class a_latex_model:
262
+ '''a model in latex '''
263
+ modeltext : str()
264
+ modelequations : str = field(init=False)
265
+ modellists : str = field(init=False)
266
+
267
+ def __post_init__(self):
268
+ self.modelequations = [equation for name,equation in re.findall(r'\\label\{eq:(.*?)\}\n(.*?)\\end\{',self.modeltext,re.DOTALL)
269
+ if name != 'Exclude'] # select the relevant equations
270
+ self.modellists = findlists(self.modeltext )
271
+
272
+
273
+
274
+
275
+ @dataclass
276
+ class a_latex_equation():
277
+ original_equation : str = '' # an equation in latex
278
+ original_interior_equation : str = field(init=False)
279
+ original_terminal_equation : str = field(init=False)
280
+
281
+ def __post_init__(self):
282
+ self.original_interior_equation = self.original_terminal_equation = ''
283
+ if self.original_equation.startswith(r'\begin{split}'):
284
+ self.original_interior_equation,self.original_terminal_equation = self.original_equation.split(r'\\')
285
+ else:
286
+ self.original_interior_equation,self.original_terminal_equation = self.original_equation, None
287
+
288
+ self.transformed_interior = self.straighten_eq(self.original_interior_equation)
289
+ self.transformed_terminal = self.straighten_eq(self.original_terminal_equation )
290
+
291
+ # self.int_frmlname,self.int_do_conditions,self.int_do_indices,self.expression = (
292
+ # findallindex(self.transformed_interior))
293
+
294
+ @property
295
+ def pprint(self):
296
+ print(self.original_interior_equation )
297
+ print(self.transformed_interior)
298
+ print(self.transformed_terminal)
299
+ print(doable(self.transformed_interior))
300
+
301
+ def straighten_eq(self,temp):
302
+ trans={r'\left':'',
303
+ r'\right':'',
304
+ r'\min':'min',
305
+ r'\max':'max',
306
+ r'\rho':'rho',
307
+ r'&':'',
308
+ r'\\':'',
309
+ r'\nonumber' : '',
310
+ r'\_' : '_',
311
+ r'_{t}' : '',
312
+ r'_t' :'',
313
+ 'logit^{-1}' : 'logit_inverse',
314
+ r'\{' : '{',
315
+ r'\}' : '}',
316
+ r'\begin{split}' : '',
317
+ '\n' :'',
318
+ r'\forall' :'',
319
+ r'\;' :'',
320
+
321
+
322
+ }
323
+ ftrans = {
324
+ r'\sqrt':'sqrt',
325
+ r'\Delta':'diff',
326
+ r'\sum_':'sum',
327
+ r'\Phi':'NORM.CDF',
328
+ r'\Phi^{-1}':'NORM.PDF'
329
+ }
330
+ regtrans = {
331
+ r'\\Delta ([A-Za-z_][\w{},\^]*)':r'diff(\1)', # \Delta xy => diff(xy)
332
+ r'_{t-([1-9]+)}' : r'(-\1)', # _{t-x} => (-x)
333
+ r'_{t\+([1-9]+)}' : r'(+\1)', # _{t+x} => (+x)
334
+
335
+ # r'\^([\w])' : r'_\1', # ^x => _x
336
+ # r'\^\{([\w]+)\}(\w)' : r'_\1_\2', # ^{xx}y => _xx_y
337
+ r'\^{([\w+-]+)}' : r'__{\1}', # ^{xx} => _xx
338
+ r'\^{([\w+-]+),([\w+-]+)\}' : r'__{\1}__{\2}', # ^{xx,yy} => _xx_yy
339
+ r'\s*\\times\s*':'*' ,
340
+ r'\\text{\[([\w+-,.]+)\]}' : r'[\1]'
341
+ }
342
+ # breakpoint()
343
+ for before,to in ftrans.items():
344
+ temp = defunk(before,to,temp)
345
+ for before,to in trans.items():
346
+ temp = temp.replace(before,to)
347
+ for before,to in regtrans.items():
348
+ temp = re.sub(before,to,temp)
349
+ temp = debrace(temp)
350
+ temp = defrack(temp)
351
+ temp = depower(temp)
352
+ return temp
353
+
354
+
355
+
356
+ if __name__ == '__main__' :
357
+ if 0:
358
+ test = r'''\
359
+ Loans can be in 3 stages, 1,2 3.
360
+ New loans will be generated and loans will mature.
361
+
362
+
363
+ \begin{equation}
364
+ \label{eq:Norm}
365
+ TR^{stage\_from,stage} = \frac{TR\_U^{stage\_from,stage}}{1+0*\sum_{stage\_from2}(TR\_U^{stage\_from2,stage})}
366
+ \times(1-M^{stage}-WRO^{stage})
367
+ \end{equation}
368
+
369
+ List $stage=\{s1, s2,s3\}$
370
+
371
+ List $stage\_from=\{s1, s2,s3\}$
372
+
373
+ List $stage\_from2=\{s1, s2,s3\}$
374
+
375
+ List $stage\_to=\{s1, s2,s3\}$
376
+
377
+ '''
378
+
379
+ test1 =r'''
380
+ list $agegroup=\{16, 17, 18, 19, 20, 99, 100 \} \\
381
+ end : \{0 , 0 , 0 , 0 , 0 , 0 , 1\}$
382
+
383
+
384
+ \begin{equation}
385
+ \label{eq:mod_another}
386
+ \begin{split}
387
+ \forall [agegroup.NONEND,hest.abe]\; \left(QC^{agegroup,hest}_t\right)^{(-ECX_t)} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
388
+ \\
389
+ & & &=
390
+ \dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
391
+ * (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
392
+ \end{split}
393
+ \end{equation}
394
+ '''
395
+ #%% test
396
+
397
+ this = a_latex_model(test1)
398
+ thiseq = a_latex_equation(this.modelequations[0])
399
+ thiseq.pprint
400
+
401
+
model_run_numba.py ADDED
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ This script runs a model with numba
4
+
5
+
6
+ @author: hanseni
7
+ """
8
+
9
+ import sys
10
+ from modelclass import model
11
+
12
+ try:
13
+ mmodel, basedf = model.modelload(sys.argv[1],run=1,ljit=1,stringjit=False)
14
+ except:
15
+ ...