modelflowib 2.73__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.73/PKG-INFO +156 -0
- modelflowib-2.73/README.md +113 -0
- modelflowib-2.73/license.md +10 -0
- modelflowib-2.73/modelBLfunk.py +180 -0
- modelflowib-2.73/model_Excel.py +332 -0
- modelflowib-2.73/model_cvx.py +139 -0
- modelflowib-2.73/model_dynare.py +173 -0
- modelflowib-2.73/model_financial_stability.py +88 -0
- modelflowib-2.73/model_latex.py +497 -0
- modelflowib-2.73/model_latex_class.py +808 -0
- modelflowib-2.73/model_parquet_mixin.py +424 -0
- modelflowib-2.73/modelclass.py +9828 -0
- modelflowib-2.73/modelconstruct.py +1496 -0
- modelflowib-2.73/modelconstruct_estimation.py +2872 -0
- modelflowib-2.73/modeldash.py +265 -0
- modelflowib-2.73/modeldashboot.py +202 -0
- modelflowib-2.73/modeldashsidebar.py +456 -0
- modelflowib-2.73/modeldekom.py +651 -0
- modelflowib-2.73/modeldiff.py +561 -0
- modelflowib-2.73/modeldisplay.py +550 -0
- modelflowib-2.73/modelestimation.py +1776 -0
- modelflowib-2.73/modelestimator_new.py +2613 -0
- modelflowib-2.73/modelflowib.egg-info/PKG-INFO +156 -0
- modelflowib-2.73/modelflowib.egg-info/SOURCES.txt +47 -0
- modelflowib-2.73/modelflowib.egg-info/dependency_links.txt +1 -0
- modelflowib-2.73/modelflowib.egg-info/requires.txt +28 -0
- modelflowib-2.73/modelflowib.egg-info/top_level.txt +39 -0
- modelflowib-2.73/modelgrab.py +318 -0
- modelflowib-2.73/modelgrabgdx.py +584 -0
- modelflowib-2.73/modelgrabwf2.py +1107 -0
- modelflowib-2.73/modelhelp.py +543 -0
- modelflowib-2.73/modelhtml.py +606 -0
- modelflowib-2.73/modelinvert.py +250 -0
- modelflowib-2.73/modeljupyter.py +824 -0
- modelflowib-2.73/modeljupytermagic.py +813 -0
- modelflowib-2.73/modelmacrograb.py +98 -0
- modelflowib-2.73/modelmanipulation.py +1461 -0
- modelflowib-2.73/modelmf.py +349 -0
- modelflowib-2.73/modelnet.py +114 -0
- modelflowib-2.73/modelnewton.py +2178 -0
- modelflowib-2.73/modelnormalize.py +430 -0
- modelflowib-2.73/modelpattern.py +428 -0
- modelflowib-2.73/modelreport.py +2187 -0
- modelflowib-2.73/modeluserfunk.py +97 -0
- modelflowib-2.73/modelvis.py +1038 -0
- modelflowib-2.73/modelwidget.py +718 -0
- modelflowib-2.73/modelwidget_input.py +1933 -0
- modelflowib-2.73/pyproject.toml +81 -0
- modelflowib-2.73/setup.cfg +4 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modelflowib
|
|
3
|
+
Version: 2.73
|
|
4
|
+
Summary: A tool to solve and manage dynamic economic and financial models
|
|
5
|
+
Author-email: Ib Hansen <Ib.Hansen.Iv@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/IbHansen/ModelFlow2
|
|
8
|
+
Project-URL: Repository, https://github.com/IbHansen/ModelFlow2
|
|
9
|
+
Keywords: economics,dynamic models,BLL,numba,modelling
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: license.md
|
|
15
|
+
Requires-Dist: pandas
|
|
16
|
+
Requires-Dist: matplotlib
|
|
17
|
+
Requires-Dist: seaborn
|
|
18
|
+
Requires-Dist: sympy
|
|
19
|
+
Requires-Dist: numpy
|
|
20
|
+
Requires-Dist: networkx
|
|
21
|
+
Requires-Dist: tqdm
|
|
22
|
+
Requires-Dist: scipy
|
|
23
|
+
Requires-Dist: numexpr
|
|
24
|
+
Requires-Dist: bottleneck
|
|
25
|
+
Requires-Dist: XlsxWriter
|
|
26
|
+
Requires-Dist: pyarrow
|
|
27
|
+
Requires-Dist: tabulate
|
|
28
|
+
Requires-Dist: openpyxl
|
|
29
|
+
Requires-Dist: xarray
|
|
30
|
+
Requires-Dist: cvxopt
|
|
31
|
+
Requires-Dist: numba
|
|
32
|
+
Requires-Dist: dash
|
|
33
|
+
Requires-Dist: dash-bootstrap-components
|
|
34
|
+
Requires-Dist: dash-interactive-graphviz
|
|
35
|
+
Requires-Dist: lmfit
|
|
36
|
+
Requires-Dist: jupyter
|
|
37
|
+
Requires-Dist: ipywidgets
|
|
38
|
+
Requires-Dist: ipydatagrid
|
|
39
|
+
Provides-Extra: shiny
|
|
40
|
+
Requires-Dist: shiny; extra == "shiny"
|
|
41
|
+
Requires-Dist: shinywidgets; extra == "shiny"
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
# ModelFlow
|
|
45
|
+
A Python toolkit to manage models
|
|
46
|
+
|
|
47
|
+
The easy way to start is here [](https://mybinder.org/v2/gh/IbHansen/modelflow/master).
|
|
48
|
+
|
|
49
|
+
This will start **ModelFlow** as an online Jupyter notebook. Select one of the notebooks (files with the extension .ipynb). Some of the
|
|
50
|
+
notebooks are ModelFlow related. Other implement different model
|
|
51
|
+
|
|
52
|
+
- FRBUS will start the FRB/US model from the Federal reserve board.
|
|
53
|
+
- Q-JEM will start the Quarterly Japanese Economic Model from Bank of Japan
|
|
54
|
+
- ADAM will start the Anual Danish Aggregate Model for the Danish Statistical agency.
|
|
55
|
+
- Solow will implement a simple textybook Solow model.
|
|
56
|
+
|
|
57
|
+
You can also look at the python files of the system. Located at the **modelflow/** folder
|
|
58
|
+
|
|
59
|
+
Alternative look at the *getting started* section later in this file. It explains how to run ModelFlow localy.
|
|
60
|
+
|
|
61
|
+
The **Pandas** library is a great library to handle all kinds of datamanipulation and transformations.
|
|
62
|
+
|
|
63
|
+
However when it comes to models which contains lags or models which requires solving simultanous equations, Pandas is not quite helpful.
|
|
64
|
+
|
|
65
|
+
ModelFlow extends Pandas to handle a range of such models. And they can be large.
|
|
66
|
+
|
|
67
|
+
It requires you to specify the *model* specified as equations (the **business logic**) and place the *data* in a Pandas **DataFrame**. ModelFlow allows the model to meet the data and return the result as a new DataFrame.
|
|
68
|
+
|
|
69
|
+
A number of **analytical tools for model and result analytic** helps to understand the model and its results.
|
|
70
|
+
|
|
71
|
+
The user can **extend and modify the tools** to her or his needs.
|
|
72
|
+
|
|
73
|
+
**Onboarding models and combining models from different sources**. Creating a Macro prudential model often entails recycling several models specified in different ways: Excel, Latex, Dynare, Python or other languages. Python's ecosystem makes it possible to transform many different models into ModelFlow models or to wrap them into functions which can be called from ModelFlow models.
|
|
74
|
+
|
|
75
|
+
**Models can be specified in a high level Business logic language (a Domain Specific language)**. This allows the formulation of a model in a concise and expressive language which is close to the economic of the model. The user can concentrate on the economic or financial content - not the coding of the solution. The code for solving the model is generated by the tool. Then you can *solve the
|
|
76
|
+
simultaneous* (or *non-simultaneous* model) in an efficient way.
|
|
77
|
+
|
|
78
|
+
## Introduction
|
|
79
|
+
|
|
80
|
+
**ModelFlow is written in Python**. Python comes "batteries included" and is
|
|
81
|
+
the basis of a very rich ecosystem, which consists of a wide array of
|
|
82
|
+
libraries. ModelFlow is just another library. It supplements the existing
|
|
83
|
+
libraries regarding modeling language and solving and allows the use of
|
|
84
|
+
Python as a model management framework.
|
|
85
|
+
|
|
86
|
+
**Data handling and wrangling is done in the Pandas library**. This
|
|
87
|
+
library is the Swiss army knife of data science in Python. It can import and export data to most systems and it is very powerful in manipulating and transforming data.
|
|
88
|
+
The core
|
|
89
|
+
element of Pandas is the *Dataframe*. A Dataframe is a two-dimensional
|
|
90
|
+
tabular data structure. Each *column* consists of cells of the same type
|
|
91
|
+
-- it can be a number, a string, a matrix or another Python data object.This includes matrices and other dataframes. Each *row is indexed.* The index can basically be any type of variable
|
|
92
|
+
including dates, which is especially relevant for economic and financial models.
|
|
93
|
+
|
|
94
|
+
**ModelFlow gives the user tools for more than solving models**. This
|
|
95
|
+
includes:
|
|
96
|
+
|
|
97
|
+
- *Visualization* and comparison of results
|
|
98
|
+
|
|
99
|
+
- *Integration* of models from different sources
|
|
100
|
+
|
|
101
|
+
- *Analyze the logical structure of a model*. By applying graph theory,
|
|
102
|
+
ModelFlow can find data lineage, find a suitable calculating sequence and trace
|
|
103
|
+
causes of changes through the calculations.
|
|
104
|
+
|
|
105
|
+
- *Inverting* the model to calculating the necessary instruments to
|
|
106
|
+
achieve a desired target.
|
|
107
|
+
|
|
108
|
+
- Calculating the *attributions* from input to the results of a model.
|
|
109
|
+
|
|
110
|
+
- Calculating the *attribution* from input to the result of each
|
|
111
|
+
formula.
|
|
112
|
+
|
|
113
|
+
- Finding and calculating partial *derivatives* of formulas
|
|
114
|
+
|
|
115
|
+
- *Integrating user defined python functions* in the Business logic
|
|
116
|
+
language (like optimization, calculating risk weights or to make a matrices consistent with the RAS algorithm )
|
|
117
|
+
|
|
118
|
+
- *Wrap matlab* models so they can be used in the Business logic
|
|
119
|
+
language.
|
|
120
|
+
|
|
121
|
+
- *Speed up* solving using "Just in time compilation"
|
|
122
|
+
|
|
123
|
+
- Analyze the model structure through tools from graph theory
|
|
124
|
+
|
|
125
|
+
- Handle *large models.* 1,000,000 formulas is not a problem.
|
|
126
|
+
|
|
127
|
+
- Integrate model management in Jupyter notebooks for *agile and user
|
|
128
|
+
friendly model use*
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
**The core code of ModelFlow is small and
|
|
132
|
+
documented.** Thus it can easily be modified and expanded to the specific need of the user. *ModelFlow is a toolset*. It can handle models, which conform to the tools.
|
|
133
|
+
|
|
134
|
+
If you need a feature or have a model which can't be handled in ModelFlow,
|
|
135
|
+
you are encouraged to improve ModelFlow. Please share the
|
|
136
|
+
improvement, other users may have the same need, or can be inspired by
|
|
137
|
+
your work.
|
|
138
|
+
|
|
139
|
+
Also bear in mind that ModelFlow is experimental. It is provided ”as is”, without any representation or warranty of any kind either express or implied.
|
|
140
|
+
|
|
141
|
+
## Getting started
|
|
142
|
+
|
|
143
|
+
You need Python 3.7+ with asssociated libraries. The easy way is to install Anaconda Python is to download the distribution from [https://www.anaconda.com/distribution](https://www.anaconda.com/distribution)
|
|
144
|
+
|
|
145
|
+
Now you can install ModelFlow by running a command window from the Anaconda prompt and execute theese commands
|
|
146
|
+
```
|
|
147
|
+
conda install modelflow -c ibh -c defaults -c conda-forge
|
|
148
|
+
```
|
|
149
|
+
You will find the anaconda prompt by searching "anaconda" in the start menu search field
|
|
150
|
+
|
|
151
|
+
Try out one of the workbooks. to do this you:
|
|
152
|
+
```
|
|
153
|
+
click on the Adaconda prompth in the Anaconda folder
|
|
154
|
+
cd <The location you have downloaded the workbooks>
|
|
155
|
+
jupyter notebook
|
|
156
|
+
```
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# ModelFlow
|
|
2
|
+
A Python toolkit to manage models
|
|
3
|
+
|
|
4
|
+
The easy way to start is here [](https://mybinder.org/v2/gh/IbHansen/modelflow/master).
|
|
5
|
+
|
|
6
|
+
This will start **ModelFlow** as an online Jupyter notebook. Select one of the notebooks (files with the extension .ipynb). Some of the
|
|
7
|
+
notebooks are ModelFlow related. Other implement different model
|
|
8
|
+
|
|
9
|
+
- FRBUS will start the FRB/US model from the Federal reserve board.
|
|
10
|
+
- Q-JEM will start the Quarterly Japanese Economic Model from Bank of Japan
|
|
11
|
+
- ADAM will start the Anual Danish Aggregate Model for the Danish Statistical agency.
|
|
12
|
+
- Solow will implement a simple textybook Solow model.
|
|
13
|
+
|
|
14
|
+
You can also look at the python files of the system. Located at the **modelflow/** folder
|
|
15
|
+
|
|
16
|
+
Alternative look at the *getting started* section later in this file. It explains how to run ModelFlow localy.
|
|
17
|
+
|
|
18
|
+
The **Pandas** library is a great library to handle all kinds of datamanipulation and transformations.
|
|
19
|
+
|
|
20
|
+
However when it comes to models which contains lags or models which requires solving simultanous equations, Pandas is not quite helpful.
|
|
21
|
+
|
|
22
|
+
ModelFlow extends Pandas to handle a range of such models. And they can be large.
|
|
23
|
+
|
|
24
|
+
It requires you to specify the *model* specified as equations (the **business logic**) and place the *data* in a Pandas **DataFrame**. ModelFlow allows the model to meet the data and return the result as a new DataFrame.
|
|
25
|
+
|
|
26
|
+
A number of **analytical tools for model and result analytic** helps to understand the model and its results.
|
|
27
|
+
|
|
28
|
+
The user can **extend and modify the tools** to her or his needs.
|
|
29
|
+
|
|
30
|
+
**Onboarding models and combining models from different sources**. Creating a Macro prudential model often entails recycling several models specified in different ways: Excel, Latex, Dynare, Python or other languages. Python's ecosystem makes it possible to transform many different models into ModelFlow models or to wrap them into functions which can be called from ModelFlow models.
|
|
31
|
+
|
|
32
|
+
**Models can be specified in a high level Business logic language (a Domain Specific language)**. This allows the formulation of a model in a concise and expressive language which is close to the economic of the model. The user can concentrate on the economic or financial content - not the coding of the solution. The code for solving the model is generated by the tool. Then you can *solve the
|
|
33
|
+
simultaneous* (or *non-simultaneous* model) in an efficient way.
|
|
34
|
+
|
|
35
|
+
## Introduction
|
|
36
|
+
|
|
37
|
+
**ModelFlow is written in Python**. Python comes "batteries included" and is
|
|
38
|
+
the basis of a very rich ecosystem, which consists of a wide array of
|
|
39
|
+
libraries. ModelFlow is just another library. It supplements the existing
|
|
40
|
+
libraries regarding modeling language and solving and allows the use of
|
|
41
|
+
Python as a model management framework.
|
|
42
|
+
|
|
43
|
+
**Data handling and wrangling is done in the Pandas library**. This
|
|
44
|
+
library is the Swiss army knife of data science in Python. It can import and export data to most systems and it is very powerful in manipulating and transforming data.
|
|
45
|
+
The core
|
|
46
|
+
element of Pandas is the *Dataframe*. A Dataframe is a two-dimensional
|
|
47
|
+
tabular data structure. Each *column* consists of cells of the same type
|
|
48
|
+
-- it can be a number, a string, a matrix or another Python data object.This includes matrices and other dataframes. Each *row is indexed.* The index can basically be any type of variable
|
|
49
|
+
including dates, which is especially relevant for economic and financial models.
|
|
50
|
+
|
|
51
|
+
**ModelFlow gives the user tools for more than solving models**. This
|
|
52
|
+
includes:
|
|
53
|
+
|
|
54
|
+
- *Visualization* and comparison of results
|
|
55
|
+
|
|
56
|
+
- *Integration* of models from different sources
|
|
57
|
+
|
|
58
|
+
- *Analyze the logical structure of a model*. By applying graph theory,
|
|
59
|
+
ModelFlow can find data lineage, find a suitable calculating sequence and trace
|
|
60
|
+
causes of changes through the calculations.
|
|
61
|
+
|
|
62
|
+
- *Inverting* the model to calculating the necessary instruments to
|
|
63
|
+
achieve a desired target.
|
|
64
|
+
|
|
65
|
+
- Calculating the *attributions* from input to the results of a model.
|
|
66
|
+
|
|
67
|
+
- Calculating the *attribution* from input to the result of each
|
|
68
|
+
formula.
|
|
69
|
+
|
|
70
|
+
- Finding and calculating partial *derivatives* of formulas
|
|
71
|
+
|
|
72
|
+
- *Integrating user defined python functions* in the Business logic
|
|
73
|
+
language (like optimization, calculating risk weights or to make a matrices consistent with the RAS algorithm )
|
|
74
|
+
|
|
75
|
+
- *Wrap matlab* models so they can be used in the Business logic
|
|
76
|
+
language.
|
|
77
|
+
|
|
78
|
+
- *Speed up* solving using "Just in time compilation"
|
|
79
|
+
|
|
80
|
+
- Analyze the model structure through tools from graph theory
|
|
81
|
+
|
|
82
|
+
- Handle *large models.* 1,000,000 formulas is not a problem.
|
|
83
|
+
|
|
84
|
+
- Integrate model management in Jupyter notebooks for *agile and user
|
|
85
|
+
friendly model use*
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
**The core code of ModelFlow is small and
|
|
89
|
+
documented.** Thus it can easily be modified and expanded to the specific need of the user. *ModelFlow is a toolset*. It can handle models, which conform to the tools.
|
|
90
|
+
|
|
91
|
+
If you need a feature or have a model which can't be handled in ModelFlow,
|
|
92
|
+
you are encouraged to improve ModelFlow. Please share the
|
|
93
|
+
improvement, other users may have the same need, or can be inspired by
|
|
94
|
+
your work.
|
|
95
|
+
|
|
96
|
+
Also bear in mind that ModelFlow is experimental. It is provided ”as is”, without any representation or warranty of any kind either express or implied.
|
|
97
|
+
|
|
98
|
+
## Getting started
|
|
99
|
+
|
|
100
|
+
You need Python 3.7+ with asssociated libraries. The easy way is to install Anaconda Python is to download the distribution from [https://www.anaconda.com/distribution](https://www.anaconda.com/distribution)
|
|
101
|
+
|
|
102
|
+
Now you can install ModelFlow by running a command window from the Anaconda prompt and execute theese commands
|
|
103
|
+
```
|
|
104
|
+
conda install modelflow -c ibh -c defaults -c conda-forge
|
|
105
|
+
```
|
|
106
|
+
You will find the anaconda prompt by searching "anaconda" in the start menu search field
|
|
107
|
+
|
|
108
|
+
Try out one of the workbooks. to do this you:
|
|
109
|
+
```
|
|
110
|
+
click on the Adaconda prompth in the Anaconda folder
|
|
111
|
+
cd <The location you have downloaded the workbooks>
|
|
112
|
+
jupyter notebook
|
|
113
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Copyright © 2019 Danmarks Nationalbank and ECB
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Soft-ware"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
7
|
+
|
|
8
|
+
Except as contained in this notice, the name of Danmarks Nationalbank and ECB shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Danmarks Nationalbank and ECB.
|
|
9
|
+
|
|
10
|
+
Based on MIT License.
|
|
@@ -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,isclose
|
|
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
|
+
|