ModelFlowIb 2.61__tar.gz → 2.63__tar.gz
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.
- {modelflowib-2.61 → modelflowib-2.63}/ModelFlowIb.egg-info/PKG-INFO +2 -1
- {modelflowib-2.61 → modelflowib-2.63}/ModelFlowIb.egg-info/SOURCES.txt +2 -0
- {modelflowib-2.61 → modelflowib-2.63}/ModelFlowIb.egg-info/requires.txt +1 -0
- {modelflowib-2.61 → modelflowib-2.63}/ModelFlowIb.egg-info/top_level.txt +2 -0
- {modelflowib-2.61 → modelflowib-2.63}/PKG-INFO +2 -1
- modelflowib-2.63/modelBLfunk.py +180 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_cvx.py +13 -6
- {modelflowib-2.61 → modelflowib-2.63}/model_dynare.py +5 -5
- {modelflowib-2.61 → modelflowib-2.63}/model_latex.py +0 -29
- {modelflowib-2.61 → modelflowib-2.63}/model_latex_class.py +137 -30
- {modelflowib-2.61 → modelflowib-2.63}/modelclass.py +624 -48
- modelflowib-2.63/modelconstruct.py +1491 -0
- modelflowib-2.63/modelestimation.py +1776 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelhelp.py +130 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeljupytermagic.py +417 -3
- {modelflowib-2.61 → modelflowib-2.63}/modelmanipulation.py +73 -37
- {modelflowib-2.61 → modelflowib-2.63}/modelmf.py +45 -1
- {modelflowib-2.61 → modelflowib-2.63}/modelnewton.py +304 -18
- {modelflowib-2.61 → modelflowib-2.63}/modelnormalize.py +87 -50
- {modelflowib-2.61 → modelflowib-2.63}/modelpattern.py +50 -3
- {modelflowib-2.61 → modelflowib-2.63}/modelreport.py +8 -3
- {modelflowib-2.61 → modelflowib-2.63}/modelvis.py +6 -2
- {modelflowib-2.61 → modelflowib-2.63}/modelwidget_input.py +271 -81
- {modelflowib-2.61 → modelflowib-2.63}/pyproject.toml +4 -3
- modelflowib-2.61/modelBLfunk.py +0 -97
- {modelflowib-2.61 → modelflowib-2.63}/ModelFlowIb.egg-info/dependency_links.txt +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_Excel.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_doable.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_financial_stability.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_ifrs9.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/model_run_numba.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelclass2.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldash.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldashboot.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldashsidebar.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldekom.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldiff.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeldisplay.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelfeedback.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelgrab.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelgrabwf2.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelinvert.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeljupyter.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelmacrograb.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelnet.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelpatternrev.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelsandbox.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelsandbox_Mixin.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeltodo.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeluserfunk.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modeluserfunk_old.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelwidget.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/modelwidget_shinypy.py +0 -0
- {modelflowib-2.61 → modelflowib-2.63}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ModelFlowIb
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.63
|
|
4
4
|
Summary: A tool to solve and manage dynamic economic models
|
|
5
5
|
Author-email: Ib Hansen <your@email.com>
|
|
6
6
|
License: MIT
|
|
@@ -24,6 +24,7 @@ Requires-Dist: numba
|
|
|
24
24
|
Requires-Dist: dash
|
|
25
25
|
Requires-Dist: dash-bootstrap-components
|
|
26
26
|
Requires-Dist: dash-interactive-graphviz
|
|
27
|
+
Requires-Dist: lmfit
|
|
27
28
|
Provides-Extra: jupyter
|
|
28
29
|
Requires-Dist: jupyter; extra == "jupyter"
|
|
29
30
|
Requires-Dist: ipywidgets; extra == "jupyter"
|
|
@@ -10,12 +10,14 @@ model_latex_class.py
|
|
|
10
10
|
model_run_numba.py
|
|
11
11
|
modelclass.py
|
|
12
12
|
modelclass2.py
|
|
13
|
+
modelconstruct.py
|
|
13
14
|
modeldash.py
|
|
14
15
|
modeldashboot.py
|
|
15
16
|
modeldashsidebar.py
|
|
16
17
|
modeldekom.py
|
|
17
18
|
modeldiff.py
|
|
18
19
|
modeldisplay.py
|
|
20
|
+
modelestimation.py
|
|
19
21
|
modelfeedback.py
|
|
20
22
|
modelgrab.py
|
|
21
23
|
modelgrabwf2.py
|
|
@@ -11,12 +11,14 @@ model_latex_class
|
|
|
11
11
|
model_run_numba
|
|
12
12
|
modelclass
|
|
13
13
|
modelclass2
|
|
14
|
+
modelconstruct
|
|
14
15
|
modeldash
|
|
15
16
|
modeldashboot
|
|
16
17
|
modeldashsidebar
|
|
17
18
|
modeldekom
|
|
18
19
|
modeldiff
|
|
19
20
|
modeldisplay
|
|
21
|
+
modelestimation
|
|
20
22
|
modelfeedback
|
|
21
23
|
modelgrab
|
|
22
24
|
modelgrabwf2
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: ModelFlowIb
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.63
|
|
4
4
|
Summary: A tool to solve and manage dynamic economic models
|
|
5
5
|
Author-email: Ib Hansen <your@email.com>
|
|
6
6
|
License: MIT
|
|
@@ -24,6 +24,7 @@ Requires-Dist: numba
|
|
|
24
24
|
Requires-Dist: dash
|
|
25
25
|
Requires-Dist: dash-bootstrap-components
|
|
26
26
|
Requires-Dist: dash-interactive-graphviz
|
|
27
|
+
Requires-Dist: lmfit
|
|
27
28
|
Provides-Extra: jupyter
|
|
28
29
|
Requires-Dist: jupyter; extra == "jupyter"
|
|
29
30
|
Requires-Dist: ipywidgets; extra == "jupyter"
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Fri Mar 2 17:01:49 2018
|
|
4
|
+
|
|
5
|
+
@author: hanseni
|
|
6
|
+
|
|
7
|
+
Functions placed here are included in the Pyfs business language
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
from math import exp, log, sqrt, tanh, erf
|
|
11
|
+
from numpy import transpose , array
|
|
12
|
+
from scipy.stats import norm,lognorm
|
|
13
|
+
from scipy.stats import gamma
|
|
14
|
+
import inspect
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
# raise ImportError("Simulating ImportError for numba.")
|
|
18
|
+
|
|
19
|
+
from numba import jit
|
|
20
|
+
except ImportError:
|
|
21
|
+
print("Numba is not available. No worry")
|
|
22
|
+
|
|
23
|
+
def jit(*args, **kwargs):
|
|
24
|
+
def wrapper(func):
|
|
25
|
+
# print('jit called')
|
|
26
|
+
return func
|
|
27
|
+
return wrapper
|
|
28
|
+
|
|
29
|
+
@jit("f8(f8)",nopython=True)
|
|
30
|
+
def logit_inverse(number):
|
|
31
|
+
''' A function which returns the logit of a number
|
|
32
|
+
|
|
33
|
+
takes care of extreme values
|
|
34
|
+
'''
|
|
35
|
+
if number > 100:
|
|
36
|
+
return 1.0
|
|
37
|
+
elif number < -100:
|
|
38
|
+
return 0.0
|
|
39
|
+
else:
|
|
40
|
+
return 1/(1+exp(-number))
|
|
41
|
+
|
|
42
|
+
classfunk = []
|
|
43
|
+
try:
|
|
44
|
+
from cvxopt import matrix
|
|
45
|
+
from model_cvx import mv_opt, mv_opt_prop
|
|
46
|
+
classfunk = ['TRANS'] # names a classfunk which can be called
|
|
47
|
+
except:
|
|
48
|
+
print('ModelFlow info: CVXopt not installed. Only matters if you are incorporating optimization')
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
from numpy import array
|
|
52
|
+
|
|
53
|
+
from model_financial_stability import lifetime_credit_loss
|
|
54
|
+
|
|
55
|
+
def sum_excel(*arg):
|
|
56
|
+
''' a functions which sums the arguments used in models franslated from excel
|
|
57
|
+
'''
|
|
58
|
+
return sum(arg)
|
|
59
|
+
|
|
60
|
+
def logit(number):
|
|
61
|
+
''' A function which returns the logit of a number
|
|
62
|
+
'''
|
|
63
|
+
return(-log(1.0/number-1.0))
|
|
64
|
+
|
|
65
|
+
# @jit("f8(f8)")
|
|
66
|
+
# def logit_inverse(number):
|
|
67
|
+
# ''' A function which returns the logit of a number
|
|
68
|
+
|
|
69
|
+
# takes care of extreme values
|
|
70
|
+
# '''
|
|
71
|
+
# if number > 100:
|
|
72
|
+
# return 1.0
|
|
73
|
+
# elif number < -100:
|
|
74
|
+
# return 0.0
|
|
75
|
+
# else:
|
|
76
|
+
# return 1/(1+exp(-number))
|
|
77
|
+
|
|
78
|
+
def normcdf(input,mu=0.0,sigma=1.0):
|
|
79
|
+
return norm.cdf(input,mu,sigma)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def qgamma(q,a,loc):
|
|
83
|
+
res = gamma.ppf(q,a,loc,scale=1)
|
|
84
|
+
return res
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def cdf_lognorm_econ(x, mu, sigma, eps=1e-12):
|
|
88
|
+
"""
|
|
89
|
+
Econ / GAMS-style log-normal cumulative distribution function (Newton-safe).
|
|
90
|
+
|
|
91
|
+
Computes the CDF of a log-normal distribution using the mean-parameterized
|
|
92
|
+
(econ / GAMS) formulation, where `mu` is the mean of the level variable X,
|
|
93
|
+
not the mean of log(X).
|
|
94
|
+
|
|
95
|
+
Mathematically:
|
|
96
|
+
F(x) = Φ((ln(x / mu) + 0.5 * sigma**2) / sigma)
|
|
97
|
+
|
|
98
|
+
where Φ(·) is the standard normal CDF.
|
|
99
|
+
|
|
100
|
+
This implementation is numerically safe for use in Newton solvers and
|
|
101
|
+
numerical differentiation by applying a soft lower bound to x and using
|
|
102
|
+
a stable normal CDF.
|
|
103
|
+
|
|
104
|
+
Parameters
|
|
105
|
+
----------
|
|
106
|
+
x : float
|
|
107
|
+
Evaluation point. May be zero or negative during numerical
|
|
108
|
+
differentiation.
|
|
109
|
+
mu : float
|
|
110
|
+
Mean of the level variable X (E[X] = mu).
|
|
111
|
+
sigma : float
|
|
112
|
+
Standard deviation of ln(X).
|
|
113
|
+
eps : float, optional
|
|
114
|
+
Small positive floor used to ensure log(x) is well-defined.
|
|
115
|
+
Default is 1e-12.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
float
|
|
120
|
+
Value of the log-normal CDF in the interval [0, 1].
|
|
121
|
+
|
|
122
|
+
Notes
|
|
123
|
+
-----
|
|
124
|
+
- The median of the distribution is mu * exp(-0.5 * sigma**2),
|
|
125
|
+
not mu.
|
|
126
|
+
- As x → 0, the function smoothly approaches 0 with a vanishing
|
|
127
|
+
derivative, which is critical for Newton stability.
|
|
128
|
+
- Equivalent to the standard log-mean parameterization with:
|
|
129
|
+
mu_log = ln(mu) - 0.5 * sigma**2
|
|
130
|
+
"""
|
|
131
|
+
from scipy.special import ndtr
|
|
132
|
+
|
|
133
|
+
x_safe = x if x > eps else eps
|
|
134
|
+
z = (log(x_safe / mu) + 0.5 * sigma**2) / sigma
|
|
135
|
+
return float(ndtr(z))
|
|
136
|
+
|
|
137
|
+
def part_exp_lognorm(k, mu, std, eps=1e-12):
|
|
138
|
+
"""
|
|
139
|
+
Safe scalar equivalent of the GAMS PartExpLogNorm function.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
k : float
|
|
144
|
+
Marginal input
|
|
145
|
+
mu : float
|
|
146
|
+
Average input (scale)
|
|
147
|
+
std : float
|
|
148
|
+
Dispersion / smoothing parameter
|
|
149
|
+
eps : float, optional
|
|
150
|
+
Small positive number to avoid log/division errors
|
|
151
|
+
|
|
152
|
+
Returns
|
|
153
|
+
-------
|
|
154
|
+
float
|
|
155
|
+
Smoothed, bounded transformation
|
|
156
|
+
"""
|
|
157
|
+
# protect against division by zero
|
|
158
|
+
mu_safe = mu if abs(mu) > eps else eps
|
|
159
|
+
|
|
160
|
+
# protect against log(0)
|
|
161
|
+
ratio = abs(k / mu_safe)
|
|
162
|
+
ratio_safe = ratio if ratio > eps else eps
|
|
163
|
+
|
|
164
|
+
# protect against std = 0
|
|
165
|
+
std_safe = std if abs(std) > eps else eps
|
|
166
|
+
|
|
167
|
+
return mu_safe * erf((log(ratio_safe) - 0.5 * std_safe**2) / std_safe)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def clognorm(input,mu=0.0,sigma=1.0):
|
|
172
|
+
res = lognorm.cdf(input,mu,sigma)
|
|
173
|
+
return res
|
|
174
|
+
|
|
175
|
+
if __name__ == '__main__' and 1:
|
|
176
|
+
xx = logit_inverse(-3*10
|
|
177
|
+
)
|
|
178
|
+
print(xx)
|
|
179
|
+
|
|
180
|
+
|
|
@@ -53,23 +53,30 @@ def MV_test(lprint=True):
|
|
|
53
53
|
results=pd.DataFrame(res,columns=columns) # create an empty pandas.Dataframe
|
|
54
54
|
return results
|
|
55
55
|
|
|
56
|
-
def mv_opt(PP,qq,riskaversion,bsum,weights,weigthtedsum,boundsmin,boundsmax,lprint=False,solget=None):
|
|
56
|
+
def mv_opt(PP,qq,riskaversion,bsum,weights,weigthtedsum,boundsmin,boundsmax,maximize=True,lprint=False,solget=None):
|
|
57
57
|
''' Performs mean variance optimization by calling a
|
|
58
58
|
quadratic optimization function from the cvxopt
|
|
59
59
|
library
|
|
60
60
|
|
|
61
61
|
'''
|
|
62
|
+
yield_multiplier = -1 if maximize else 1
|
|
63
|
+
|
|
62
64
|
q_size = len(qq)
|
|
63
65
|
P = matrix(2.0*(1.0-riskaversion)*PP) # to
|
|
64
|
-
q = matrix(
|
|
66
|
+
q = matrix(yield_multiplier*riskaversion*qq)
|
|
65
67
|
Gmin = -matrix(np.eye(q_size))
|
|
66
68
|
hmin = -matrix(boundsmin)
|
|
67
69
|
Gmax = matrix(np.eye(q_size))
|
|
68
70
|
hmax = matrix(boundsmax)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if weights:
|
|
72
|
+
Gweights = matrix(weights)
|
|
73
|
+
hweights = matrix(weigthtedsum)
|
|
74
|
+
G = matrix([Gmin,Gmax,Gweights.T]) # creates the combined inequalities
|
|
75
|
+
h = matrix([hmin,hmax,hweights])
|
|
76
|
+
else:
|
|
77
|
+
G = matrix([Gmin,Gmax]) # creates the combined inequalities
|
|
78
|
+
h = matrix([hmin,hmax])
|
|
79
|
+
|
|
73
80
|
A = matrix(1.,(1,q_size)) if bsum else None # sum of shares equal to bsum
|
|
74
81
|
b = matrix([bsum]) if bsum else None
|
|
75
82
|
options['show_progress'] = False
|
|
@@ -149,15 +149,15 @@ creates 5 files:
|
|
|
149
149
|
)
|
|
150
150
|
print(self.inf)
|
|
151
151
|
if save:
|
|
152
|
-
with open(f'{self.savepath}
|
|
152
|
+
with open(f'{self.savepath}/{self.modelname}.inf','wt') as inffile:
|
|
153
153
|
inffile.write(self.inf)
|
|
154
|
-
with open(f'{self.savepath}
|
|
154
|
+
with open(f'{self.savepath}/{self.modelname}.frm','wt') as frmfile:
|
|
155
155
|
frmfile.write(self.fthismodel)
|
|
156
|
-
with open(f'{self.savepath}
|
|
156
|
+
with open(f'{self.savepath}/{self.modelname}_para.frm','wt') as frmfile:
|
|
157
157
|
frmfile.write(self.fparamodel)
|
|
158
|
-
with open(f'{self.savepath}
|
|
158
|
+
with open(f'{self.savepath}/{self.modelname}_res.frm','wt') as frmfile:
|
|
159
159
|
frmfile.write(self.fresmodel)
|
|
160
|
-
with open(f'{self.savepath}
|
|
160
|
+
with open(f'{self.savepath}/{self.modelname}_cons.mod','wt') as consfile:
|
|
161
161
|
consfile.write(self.modout)
|
|
162
162
|
|
|
163
163
|
if __name__ == '__main__' :
|
|
@@ -183,17 +183,6 @@ def findlists(input):
|
|
|
183
183
|
+' : '+ l.split('=')[1]+'$' for l in temp1]
|
|
184
184
|
return temp2
|
|
185
185
|
|
|
186
|
-
if 0:
|
|
187
|
-
listtest='''
|
|
188
|
-
List $stage=\{stage1, stage2,stage3\}$
|
|
189
|
-
|
|
190
|
-
List $stage\_from=\{stage1, stage2,stage3\}$
|
|
191
|
-
|
|
192
|
-
List $stage\_to=\{stage1, stage2,stage3\}$
|
|
193
|
-
|
|
194
|
-
List $stage\_to2=\{stage1, stage2,stage3\}$
|
|
195
|
-
'''
|
|
196
|
-
print(findlistsold(listtest))
|
|
197
186
|
|
|
198
187
|
def findlistsnew(input):
|
|
199
188
|
'''extracte list with sublist from latex'''
|
|
@@ -505,22 +494,4 @@ if __name__ == '__main__' :
|
|
|
505
494
|
'''
|
|
506
495
|
res = latextotxt(test)
|
|
507
496
|
|
|
508
|
-
test1 ='''
|
|
509
|
-
list $agegroup=\{16, 17, 18, 19, 20, 99, 100 \} \\
|
|
510
|
-
end : \{0 , 0 , 0 , 0 , 0 , 0 , 1\}$
|
|
511
497
|
|
|
512
|
-
|
|
513
|
-
\begin{equation}
|
|
514
|
-
\label{eq:mod_another}
|
|
515
|
-
\begin{split}
|
|
516
|
-
\text{[NONEND]}\; & \left(QC^{agegroup}_t\right)^{-ECX_t} & &= \left(\dfrac{PCTOT_t}{PCTOT_{t+1}}\right) * QC^{agegroup+1}_{t+1}
|
|
517
|
-
\\
|
|
518
|
-
\text{}\;& & &=
|
|
519
|
-
\dfrac{VB^{agegroup-1}_{t-1} * \dfrac{NPOP^{agegroup-1}_{t-1}}{NPOP^{agegroup}_t}
|
|
520
|
-
* (1+R) + VY^{agegroup}_t - VB^{agegroup}_{t+1} )}{ PCTOT_{t}}
|
|
521
|
-
\end{split}
|
|
522
|
-
\end{equation}
|
|
523
|
-
'''
|
|
524
|
-
print(findlists(test1))
|
|
525
|
-
|
|
526
|
-
|
|
@@ -24,6 +24,7 @@ import modelmanipulation as mp
|
|
|
24
24
|
from modelclass import model
|
|
25
25
|
import modelpattern as pt
|
|
26
26
|
from modelnormalize import normal
|
|
27
|
+
from modelhelp import debug_var
|
|
27
28
|
|
|
28
29
|
def rebank(model):
|
|
29
30
|
''' All variable names are decorated by a {bank}
|
|
@@ -138,26 +139,27 @@ def debrace(streng):
|
|
|
138
139
|
|
|
139
140
|
'''
|
|
140
141
|
tstreng = streng[:]
|
|
141
|
-
tfunk
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
142
|
+
for tfunk in [ r'\underbrace{', r'\overbrace{']:
|
|
143
|
+
# tfunk = r'\underbrace{'
|
|
144
|
+
while tfunk in tstreng:
|
|
145
|
+
start = tstreng.find(tfunk)
|
|
146
|
+
match = tstreng[start + len(tfunk):]
|
|
147
|
+
open = 1
|
|
148
|
+
for index1 in range(len(match)):
|
|
149
|
+
if match[index1] in '{}':
|
|
150
|
+
open = (open + 1) if match[index1] == '{' else (open - 1)
|
|
151
|
+
if not open:
|
|
152
|
+
break
|
|
153
|
+
goodstuf = tstreng[:start]+match[index1]
|
|
154
|
+
match2 = match[index1 + 1+2:]
|
|
155
|
+
open=1
|
|
156
|
+
for index2 in range(len(match2)):
|
|
157
|
+
if match2[index2] in '{}':
|
|
158
|
+
open = (open + 1) if match2[index2] == '{' else (open - 1)
|
|
159
|
+
if not open:
|
|
160
|
+
break
|
|
161
|
+
tstreng = tstreng[:start]+ ''+ match[:index1] +''+ match2[index2+1:]
|
|
162
|
+
# debug_var(tstreng)
|
|
161
163
|
return tstreng
|
|
162
164
|
|
|
163
165
|
def defunk(funk, subs , streng,startp='{',slutp='}'):
|
|
@@ -228,6 +230,42 @@ def findallindex(ind0):
|
|
|
228
230
|
- <frmlname> [do_condition,...] lhs = rhs
|
|
229
231
|
- indicies are identified as __{} on the left hand side.
|
|
230
232
|
|
|
233
|
+
this function find frmlname and index variables on the left hand side. meaning variables braced by {} '''
|
|
234
|
+
if ind0.startswith('<'):
|
|
235
|
+
frmlname = re.findall(r'\<.*?\>',ind0)[0]
|
|
236
|
+
ind = ind0[ind0.index('>')+1:].strip()
|
|
237
|
+
else:
|
|
238
|
+
frmlname='<>'
|
|
239
|
+
ind=ind0.strip()
|
|
240
|
+
# print(f'{ind=}')
|
|
241
|
+
# breakpoint()
|
|
242
|
+
if ind.startswith('['):
|
|
243
|
+
do_conditions = ind[1:ind.index(']')]
|
|
244
|
+
rest = ind[ind.index(']')+1:].strip()
|
|
245
|
+
|
|
246
|
+
all_do_condition = {condition.strip().split(' ',1)[0].strip() :
|
|
247
|
+
condition.strip().split(' ',1)[1].strip().replace('=',' = ')
|
|
248
|
+
for condition in do_conditions.split(',')}
|
|
249
|
+
# print(f'{do_conditions=}')
|
|
250
|
+
# print(f'{all_do_condition=}')
|
|
251
|
+
else:
|
|
252
|
+
all_do_condition = dict()
|
|
253
|
+
rest = ind.strip()
|
|
254
|
+
|
|
255
|
+
lhs=rest.split('=')[0]
|
|
256
|
+
do_indicies = re.findall(r'__\{([A-Za-z][\w]*)\}',lhs ) # all the index variables
|
|
257
|
+
|
|
258
|
+
do_indicies_dict = {ind : all_do_condition.get(ind) for ind in do_indicies}
|
|
259
|
+
|
|
260
|
+
return frmlname,do_indicies_dict,rest
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def findallindex(ind0): #old
|
|
264
|
+
'''
|
|
265
|
+
- an equation looks like this
|
|
266
|
+
- <frmlname> [do_condition,...] lhs = rhs
|
|
267
|
+
- indicies are identified as __{} on the left hand side.
|
|
268
|
+
|
|
231
269
|
this function find frmlname and index variables on the left hand side. meaning variables braced by {} '''
|
|
232
270
|
if ind0.startswith('<'):
|
|
233
271
|
frmlname = re.findall(r'\<.*?\>',ind0)[0]
|
|
@@ -240,7 +278,7 @@ def findallindex(ind0):
|
|
|
240
278
|
if ind.startswith('['):
|
|
241
279
|
do_conditions = ind[1:ind.index(']')]
|
|
242
280
|
rest = ind[ind.index(']')+1:].strip()
|
|
243
|
-
all_do_condition = {condition.split('=')[0] : condition.split('=')[1] for condition in do_conditions.split(',')}
|
|
281
|
+
all_do_condition = {condition.split('=',1)[0] : condition.split('=',1)[1].replace('=',' = ') for condition in do_conditions.split(',')}
|
|
244
282
|
# print(f'{do_conditions=}')
|
|
245
283
|
else:
|
|
246
284
|
all_do_condition = dict()
|
|
@@ -253,37 +291,102 @@ def findallindex(ind0):
|
|
|
253
291
|
|
|
254
292
|
return frmlname,do_indicies_dict,rest
|
|
255
293
|
|
|
294
|
+
|
|
256
295
|
def do_list(do_index,do_condition):
|
|
257
296
|
''' do do_index_list do_condition = 1 $ '''
|
|
258
297
|
|
|
259
298
|
|
|
260
299
|
if do_condition:
|
|
261
|
-
|
|
300
|
+
if '=' in do_condition:
|
|
301
|
+
out = f'do {do_index} {do_condition} $'
|
|
302
|
+
else:
|
|
303
|
+
out = f'do {do_index} {do_condition} = 1 $'
|
|
262
304
|
else:
|
|
263
305
|
out = f'do {do_index} $'
|
|
264
306
|
return out
|
|
265
307
|
|
|
266
|
-
def
|
|
308
|
+
def sum_lists(do_index,do_condition):
|
|
309
|
+
''' do do_index_list do_condition = 1 $ '''
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
if do_condition:
|
|
313
|
+
if '=' in do_condition:
|
|
314
|
+
out = f' {do_condition}'
|
|
315
|
+
else:
|
|
316
|
+
out = f' {do_condition} = 1'
|
|
317
|
+
else:
|
|
318
|
+
out = f''
|
|
319
|
+
return out
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def doable(ind,funks=[],show=False):
|
|
267
323
|
''' find all dimensions in the left hand side of = and and decorate with the nessecary do .. enddo '''
|
|
324
|
+
|
|
325
|
+
def endovar(f,funks=[]): # Finds the first variable in a expression
|
|
326
|
+
# print(f)
|
|
327
|
+
for t in pt.udtryk_parse(f,funks=funks):
|
|
328
|
+
if t.var:
|
|
329
|
+
ud=t.var
|
|
330
|
+
break
|
|
331
|
+
return ud
|
|
332
|
+
|
|
268
333
|
|
|
269
334
|
if show:
|
|
270
335
|
print('\nBefore doable',ind,sep='\n')
|
|
271
336
|
|
|
272
|
-
frmlname,do_indicies_dict,rest = findallindex(ind) # all the index variables
|
|
337
|
+
frmlname,do_indicies_dict,rest = findallindex(ind.upper() ) # all the index variables
|
|
338
|
+
# debug_var(frmlname,do_indicies_dict,rest)
|
|
273
339
|
# breakpoint()
|
|
274
|
-
|
|
340
|
+
if show:
|
|
341
|
+
print(f'{do_indicies_dict=}*******************')
|
|
275
342
|
if do_indicies_dict :
|
|
276
343
|
pre = ' '.join([do_list(i,c) for i,c in do_indicies_dict.items() ])
|
|
277
344
|
# print(f'{pre=}*******************')
|
|
278
345
|
|
|
279
346
|
post = 'enddo $ '*len(do_indicies_dict)
|
|
280
|
-
out = f'{pre}\n frml {frmlname} {rest} $\n{post}'
|
|
347
|
+
out = [f'{pre}\n frml {frmlname} {rest} $\n{post}']
|
|
281
348
|
else:
|
|
282
|
-
out=f'frml {frmlname} {rest.strip()} $'
|
|
283
|
-
|
|
349
|
+
out=[f'frml {frmlname} {rest.strip()} $']
|
|
350
|
+
# print()
|
|
351
|
+
if ind.startswith('<'):
|
|
352
|
+
sumname = mp.kw_frml_name(frmlname, 'sum')
|
|
353
|
+
if sumname:
|
|
354
|
+
sep = ']' if ']' in ind else '>'
|
|
355
|
+
lhs= ind.split(sep,1)[1].split('=',1)[0].strip()
|
|
356
|
+
# debug_var(lhs,)
|
|
357
|
+
lhsvar = endovar(lhs,funks=funks)
|
|
358
|
+
lhsvar_stub = lhsvar.split('{',1)[0]
|
|
359
|
+
sums = ''.join([f'sum({i}{sum_lists(i,c)},' for i,c in do_indicies_dict.items() ])+lhsvar+')'*len(do_indicies_dict)
|
|
360
|
+
out.append(f'frml {frmlname} {lhsvar_stub}{sumname} = {sums} $ \n\n')
|
|
361
|
+
out_str = '\n'.join(out).upper()
|
|
284
362
|
if show:
|
|
285
|
-
print('\nAfter doable',
|
|
286
|
-
return
|
|
363
|
+
print('\nAfter doable',out_str,sep='\n')
|
|
364
|
+
return out_str
|
|
365
|
+
# xx = doable('<sum=_sum,HEST> LOSS__{BANKS}__{SECTORs} =HOLDING__{BANKS}__{SECTORs} * PD__{BANKS}__{SECTORs}'.upper()
|
|
366
|
+
# ,show=True)
|
|
367
|
+
# xx = doable('<sum=abe,HEST> [banks=country = denmark ] LOSS__{BANKS}__{SECTORs} =HOLDING__{BANKS}__{SECTORs} * PD__{BANKS}__{SECTORs}'.upper() ,show=True)
|
|
368
|
+
def normalize_lists(text: str) -> str:
|
|
369
|
+
"""
|
|
370
|
+
Finds all LIST ... $ blocks (possibly spanning multiple lines)
|
|
371
|
+
and rewrites them so that each block is on a single line
|
|
372
|
+
with clean spacing.
|
|
373
|
+
|
|
374
|
+
Will NOT match across another line starting with LIST.
|
|
375
|
+
"""
|
|
376
|
+
pattern = re.compile(
|
|
377
|
+
r'LIST' # start marker
|
|
378
|
+
r'((?:(?!^\s*LIST).)*?)' # content: anything, but stop if line starts with LIST
|
|
379
|
+
r'\$', # end marker
|
|
380
|
+
flags=re.DOTALL | re.MULTILINE
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
def replacer(match):
|
|
384
|
+
content = match.group(1)
|
|
385
|
+
flattened = " ".join(content.split())
|
|
386
|
+
return f"LIST {flattened} "
|
|
387
|
+
|
|
388
|
+
return pattern.sub(replacer, text)
|
|
389
|
+
|
|
287
390
|
|
|
288
391
|
@dataclass
|
|
289
392
|
class a_latex_model:
|
|
@@ -359,11 +462,14 @@ Methods:
|
|
|
359
462
|
|
|
360
463
|
equation_name : str = '' # The name
|
|
361
464
|
original_equation : str = '' # an equation in latex
|
|
465
|
+
partial : bool = False
|
|
362
466
|
modellists : list = ''
|
|
363
467
|
|
|
364
468
|
def __post_init__(self):
|
|
365
469
|
|
|
366
470
|
self.transformed_equation = self.straighten_eq(self.original_equation)
|
|
471
|
+
if self.partial:
|
|
472
|
+
return
|
|
367
473
|
# breakpoint()
|
|
368
474
|
self.doable_equation = doable(f'<{self.equation_name}> {self.transformed_equation}',show=0)
|
|
369
475
|
|
|
@@ -424,6 +530,7 @@ Raises:
|
|
|
424
530
|
'\n' :'',
|
|
425
531
|
r'\forall' :'',
|
|
426
532
|
r'\;' :'',
|
|
533
|
+
r'\:' :'',
|
|
427
534
|
r' ' :' ',
|
|
428
535
|
|
|
429
536
|
|