modelclass module

Created on Mon Sep 02 19:41:11 2013

This module creates the model class (model), which is the main class for managing models.

To make life easy the model class is constructed through a numner of mixin classes. Each of these handles more or less common tasks.

In addition the upd class is defines, It defines a pandas dataframe extension which allows the user to use the upd methods on dataframes.

The core function of model is BaseModel.analyzemodelnew() which first tokenizes a model specification then anlyzes the model. Then the model can be solved by one of the methods in the Solver_Mixin class.

@author: Ib

class modelclass.BaseModel(i_eq='', modelname='testmodel', silent=False, straight=False, funks=[], tabcomplete=True, previousbase=False, use_preorder=True, normalized=True, safeorder=False, var_description={}, model_description='', var_groups={}, reports={}, equations_latex='', eviews_dict={}, use_fbmin=True, **kwargs)[source]

Bases: object

Class which defines a model from equations

In itself the BaseModel is of no use.

The model class enriches BaseModel with additional Mixin classes which has additional methods and properties.

A model instance has a number of properties among which theese can be particular useful:

allvar:

Information regarding all variables

basedf:

A dataframe with first result created with this model instance

lastdf:

A dataframe with the last result created with this model instance

The two result dataframes are used for comparision and visualisation. The user can set both basedf and altdf.

initialize a model

classmethod from_eq(equations, modelname='testmodel', silent=False, straight=False, funks=[], params={}, tabcomplete=True, previousbase=False, normalized=True, norm=True, sym=False, sep='\n', **kwargs)[source]

Creates a model from macro Business logic language.

That is the model specification is first exploded.

Parameters:
  • equations – The model

  • modelname – Name of the model. Defaults to ‘testmodel’.

  • silent – Suppress messages. Defaults to False.

  • straigth – Don’t reorder the model. Defaults to False.

  • funks – Functions incorporated in the model specification . Defaults to [].

  • params – For later use. Defaults to {}.

  • tabcomplete – Allow tab compleetion in editor, for large model time consuming. Defaults to True.

  • previousbase – Use previous run as basedf not the first. Defaults to False.

  • norm – Normalize the model. Defaults to True.

  • sym – If normalize do it symbolic. Defaults to False.

  • sep – Seperate the equations. Defaults to newline.

Returns:

A model instance

get_histmodel()[source]

return a model instance with a model which generates historic values for equations marked by a frml name I or IDENT

Uses find_hist_model

analyzemodelnew(silent)[source]

Analyze a model

The function creats:Self.allvar is a dictory with an entry for every variable in the model the key is the variable name. For each endogeneous variable there is a directory with thees keys:

Maxlag:

The max lag for this variable

Maxlead:

The max Lead for this variable

Endo:

1 if the variable is endogeneous (ie on the left hand side of =

Frml:

String with the formular for this variable

Frmlnumber:

The number of the formular

Varnr:

Number of this variable

Terms:

The frml for this variable translated to terms

Frmlname:

The frmlname for this variable

Startnr:

Start of this variable in gauss seidel solutio vector :Advanced:

Matrix:

This lhs element is a matrix

Dropfrml:

If this frml shoud be excluded from the evaluation.

In addition theese properties will be created:

Endogene:

Set of endogeneous variable in the model

Exogene:

Se exogeneous variable in the model

Maxnavlen:

The longest variable name

Blank:

An emty string which can contain the longest variable name

Solveorder:

The order in which the model is solved - initaly the order of the equations in the model

Normalized:

This model is normalized

Endogene_true:

Set of endogeneous variables in model if normalized, else the set of declared endogeneous variables

smpl(start='', end='', df=None)[source]

Defines the model.current_per which is used for calculation period/index when no parameters are issues the current current period is returned

Either none or all parameters have to be provided

check_sim_smpl(databank)[source]

Checks if the current period (the SMPL) is can contain the lags and the leads

set_smpl(start='', end='', df=None)[source]

Sets the scope for the models time range, and restors it afterward

Parameters:
  • start – Start time. Defaults to ‘’.

  • end – End time. Defaults to ‘’.

  • df (Dataframe, optional) – Used on a dataframe not self.basedf. Defaults to None.

set_smpl_relative(start_ofset=0, end_ofset=0)[source]

Sets the scope for the models time range relative to the current, and restores it afterward

keepswitch(switch=False, base_last=False, scenarios='*')[source]

temporary place basedf,lastdf in keep_solutions if scenarios contains * or ? they are separated by | else space

property endograph

Dependencygraph for currrent periode endogeneous variable, used for reorder the equations if self.safeorder is true feedback for all lags are included

safeorder was a fix to handle lags = -0 which unexpected was used in WB models. Now it is handeled in modelpattern

property calculate_freq

The number of operators in the model

property flop_get

The number of operators in the model prolog,core and epilog

calculate_freq_list(varlist)[source]
Parameters:

varlist (TYPE) – calculates number of operations in list of variables.

Returns:

DESCRIPTION.

Return type:

calculate_freq (TYPE)

get_columnsnr(df)[source]

returns a dict a databanks variables as keys and column number as item used for fast getting and setting of variable values in the dataframe

outeval(databank)[source]

takes a list of terms and translates to a evaluater function called los

The model axcess the data through:Dataframe.value[rowindex+lag,coloumnindex] which is very efficient

eqcolumns(a, b)[source]

compares two lists

xgenr(databank, start='', end='', silent=0, samedata=1, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

findpos()[source]

find a startposition in the calculation array for a model places startposition for each variable in model.allvar[variable][‘startpos’] places the max startposition in model.maxstart

make_gaussline(vx, nodamp=False)[source]

takes a list of terms and translates to a line in a gauss-seidel solver for simultanius models the variables are mapped to position in a vector which has all relevant varaibles lagged this is in order to provide opertunity to optimise data and solving

New version to take hand of several lhs variables. Dampning is not allowed for this. But can easely be implemented by makeing a function to multiply tupels

make_resline(vx)[source]

takes a list of terms and translates to a line calculating line

createstuff3(dfxx)[source]

Connect a dataframe with the solution vector used by the iterative sim2 solver) return a function to place data in solution vector and to retrieve it again.

outsolve(order='', exclude=[])[source]

returns a string with a function which calculates a Gauss-Seidle iteration of a model exclude is list of endogeneous variables not to be solved uses: model.solveorder the order in which the variables is calculated model.allvar[v][“gauss”] the ccalculation

make_solver(ljit=False, order='', exclude=[], cache=False)[source]

makes a function which performs a Gaus-Seidle iteration if ljit=True a Jittet function will also be created. The functions will be placed in: model.solve model.solve_jit

base_sim(databank, start='', end='', max_iterations=1, first_test=1, ljit=False, exclude=[], silent=False, new=False, conv=[], samedata=True, dumpvar=[], ldumpvar=False, dumpwith=15, dumpdecimal=5, lcython=False, setbase=False, setlast=True, alfa=0.2, sim=True, absconv=0.01, relconv=1e-05, debug=False, stats=False, **kwargs)[source]

solves a model with data from a databank if the model has a solve function else it will be created.

The default options are resonable for most use:

Parameters:
  • start,end – Start and end of simulation, default as much as possible taking max lag into acount

  • max_iterations – Max interations

  • first_test – First iteration where convergence is tested

  • ljit – If True Numba is used to compile just in time - takes time but speeds solving up

  • new – Force creation a new version of the solver (for testing)

  • exclude – Don’t use use theese foormulas

  • silent – Suppres solving informations

  • conv – Variables on which to measure if convergence has been achived

  • samedata – If False force a remap of datatrframe to solving vector (for testing)

  • dumpvar – Variables to dump

  • ldumpvar – toggels dumping of dumpvar

  • dumpwith – with of dumps

  • dumpdecimal – decimals in dumps

  • lcython – Use Cython to compile the model (experimental )

  • alfa – Dampning of formulas marked for dampning (<Z> in frml name)

  • sim – For later use

  • absconv – Treshold for applying relconv to test convergence

  • relconv – Test for convergence

  • debug – Output debug information

  • stats – Output solving statistics

Return outdf:

A dataframe with the solution

outres(order='', exclude=[])[source]

returns a string with a function which calculates a calculation for residual check exclude is list of endogeneous variables not to be solved uses: model.solveorder the order in which the variables is calculated

make_res(order='', exclude=[])[source]

makes a function which performs a Gaus-Seidle iteration if ljit=True a Jittet function will also be created. The functions will be placed in:

  • model.solve

  • model.solve_jit

base_res(databank, start='', end='', silent=1, **kwargs)[source]

calculates a model with data from a databank Used for check wether each equation gives the same result as in the original databank’

class modelclass.Org_model_Mixin[source]

Bases: object

This mixin enrich the Basemodel class with different utility methods.

property lister

lists used in the equations

Returns:

Dictionary of lists defined in the input equations.

Return type:

dict

property listud

returns a string of the models listdefinitions

used when ceating (small) models based on this model

vlist(pat)[source]

Returns a list of variable in the model matching the pattern, the pattern can be a list of patterns

Parameters:

pat (string or list of strings) – One or more pattern seperated by space wildcards * and ?, special pattern: #ENDO

Returns:

list of variable names matching the pat.

Return type:

out (list)

vlist_names(input, pat)[source]

returns a list of variable in input matching the pattern

static list_names(input, pat, sort=True)[source]

returns a list of variable in input matching the pattern

exodif(a=None, b=None)[source]

Finds the differences between two dataframes in exogeneous variables for the model Defaults to getting the two dataframes (basedf and lastdf) internal to the model instance

Exogeneous with a name ending in <endo>__RES are not taken in, as they are part of a un_normalized model

Parameters:
  • a (TYPE, optional) – DESCRIPTION. Defaults to None. If None model.basedf will be used.

  • b (TYPE, optional) – DESCRIPTION. Defaults to None. If None model.lastdf will be used.

Returns:

the difference between the models exogenous variables in a and b.

Return type:

DataFrame

get_eq_values(varnavn, last=True, databank=None, nolag=False, per=None, showvar=False, alsoendo=False)[source]

Returns a dataframe with values from a frml determining a variable

options:
last:

the lastdf is used else baseline dataframe

nolag:

only line for each variable

get_eq_dif(varnavn, filter=False, nolag=False, showvar=False)[source]

returns a dataframe with difference of values from formula

get_var_growth(varname, showname=False, diff=False)[source]

Returns the growth rate of this variable in the base and the last dataframe

get_values(v, pct=False)[source]

returns a dataframe with the data points for a node, including lags

__getitem__(name)[source]

To execute the index operator []

Uses the modelvis.vis operator

__getattr__(name)[source]

To execute the . operator

__dir__()[source]

Default dir() implementation.

todynare(paravars=[], paravalues=[])[source]

This is a function which converts a Modelflow model instance to Dynare .mod format

class modelclass.Model_help_Mixin[source]

Bases: object

Helpers to model

var_with_frmlname(kwname='')[source]

returns endogenous variables where the frmlname contains kwname

frml_with_frmlname(kwname='')[source]

returns equations where the frmlname contains kwname

property model_exogene

True model exogeneous

  • all exogenous excluding _D _A and _X variables

property model_endogene

True endogenous, for normalized models

all endogenoue - fitted variables

static timer(input='test', show=True, short=True)[source]

A timer context manager, implemented using a generator function. This one will report time even if an exception occurs the time in seconds can be retrieved by <retunr value>.seconds “””

Parameters:
  • input (string, optional) – a name. The default is ‘test’.

  • show (bool, optional) – show the results. The default is True.

  • short (bool, optional) – . The default is False.

Return type:

None.

static update_from_list(indf, basis, lprint=False)[source]
static update_from_list_new(indf, basis, lprint=False)[source]
static update_old(indf, updates, lprint=False, scale=1.0, create=True, keep_growth=False, start='', end='')[source]

Updates a dataframe and returns a dataframe

Parameters:
  • indf (DataFrame) – input dataframe.

  • basis (string) – lines with variable updates look below.

  • lprint (bool, optional) – if True each update is printed Defaults to False.

  • scale (float, optional) – A multiplier used on all update input . Defaults to 1.0.

  • create (bool, optional) – Creates a variables if not in the dataframe . Defaults to True.

  • keep_growth (bool, optional) – Keep the growth rate after the update time frame. Defaults to False.

  • start (string, optional) – Global start

  • end – Global end

static update(indf, updates, lprint=False, scale=1.0, create=True, keep_growth=False)[source]

Updates a dataframe and returns a dataframe

Parameters:
  • indf (DataFrame) – input dataframe.

  • basis (string) – lines with variable updates look below.

  • lprint (bool, optional) – if True each update is printed Defaults to False.

  • scale (float, optional) – A multiplier used on all update input . Defaults to 1.0.

  • create (bool, optional) – Creates a variables if not in the dataframe . Defaults to True.

  • keep_growth (bool, optional) – Keep the growth rate after the update time frame. Defaults to False.

Returns:

the updated dataframe .

Return type:

df (TYPE)

A line in updates looks like this:

[<[[start] end]>] <var> <=|+|*|%|=growth|+growth|=diff> <value>... [--keep_growth_rate|--kg|--no_keep_growth_rate|--nkg]
insertModelVar(dataframe, addmodel=[])[source]

Inserts all variables from this model, not already in the dataframe. If model is specified, the dataframw will contain all variables from this and model models.

also located at the module level for backward compability

static in_notebook()[source]
static is_running_in_jupyter()[source]
class defsub[source]

Bases: dict

A subclass of dict. if a defsub is indexed by a nonexisting keyword it just return the keyword

test_model(base_input, start=None, end=None, maxvar=1000000, maxerr=100, tol=0.0001, showall=False, dec=8, width=30, ref_df=None)[source]

Compares a straight calculation with the input dataframe.

shows which variables dont have the same value

Very useful when implementing a model where the results are known

Parameters:
  • df (DataFrame) – dataframe to run.

  • start (index, optional) – start period. Defaults to None.

  • end (index, optional) – end period. Defaults to None.

  • maxvar (int, optional) – how many variables are to be chekked. Defaults to 1_000_000.

  • maxerr (int, optional) – how many errors to check Defaults to 100.

  • tol (float, optional) – check for absolute value of difference. Defaults to 0.0001.

  • showall (bolean, optional) – show more . Defaults to False.

  • ref_df (DataFrame, optional) – this dataframe is used for reference, used if add_factors has been calculated

Returns:

None.

property print_model
property print_model_latex
static get_a_repo_yaml(owner: str = 'IbHansen', repo_name: str = 'modelflow-manual', branch: str = 'main', file: str = 'papers/mfbook/repo_def.yaml', **kwargs)[source]

Retrieve and load a YAML file from a specified GitHub repository.

Parameters:
  • owner (str) – The owner of the GitHub repository. Default is “IbHansen”.

  • repo_name (str) – The name of the GitHub repository. Default is “modelflow-manual”.

  • branch (str) – The branch where the file is located. Default is “main”.

  • file (str) – The path to the file within the GitHub repository. Default is “papers/mfbook/repo_def.yaml”.

Returns:

A dictionary or list containing the loaded YAML data.

Return type:

dict or list

Exceptions:
Raises an exception if there is an error retrieving or loading the file,

with the exception message printed to the console.

static download_github_repo_old(owner: str = 'IbHansen', repo_name: str = 'wb-repos', branch: str = 'main', destination='.', go=True, silent=False, reset=False, **kwargs)[source]

Download an entire GitHub repository and extract it to a specified location.

Parameters:
  • owner – The owner of the GitHub repository.

  • repo_name – The name of the repository.

  • branch – The branch to download.

  • destination – The local path where the repository should be extracted.

  • go – display toc of notebooks

  • silent – keep silent

  • description – optional description which will be used as folder name

Returns:

A message indicating whether the download was successful or not.

static download_github_repo(owner: str = 'IbHansen', repo_name: str = 'wb-repos', branch: str = 'main', destination='./wb-repos', go=True, colab=False, silent=True, replace=False)[source]

Download an entire GitHub repository and extract it to a specified location.

Parameters:
  • owner – The owner of the GitHub repository.

  • repo_name – The name of the repository.

  • branch – The branch to download.

  • destination – The local path where the repository should be extracted.

  • go – display toc of notebooks

  • silent – keep silent

  • replace – if True replace existing files with the files from repo

  • description – optional description which will be used as folder name

Returns:

A message indicating whether the download was successful or not.

static display_toc_github(text='**Jupyter notebooks**', folder='.', all=False, nocwd=False, colab=True, owner: str = 'IbHansen', repo_name: str = 'wb-repos', branch: str = 'main', destination='./wb-repos')[source]

In a jupyter notebook this function displays a clickable table of content of all jupyter notebooks in this and sub folders

static Worldbank_Models(owner: str = 'worldbank', repo_name: str = 'MFMod-ModelFlow', branch: str = 'main', destination='./WorldbankModels', go=True, silent=True, replace=False)[source]

Download an entire GitHub repository and extract it to a specified location.

Parameters:
  • owner – The owner of the GitHub repository.

  • repo_name – The name of the repository.

  • branch – The branch to download.

  • destination – The local path where the repository should be extracted.

  • go – display toc of notebooks

  • silent – keep silent

Returns:

A message indicating whether the download was successful or not.

static load_repo_widget(loadspec=None, nocwd=True, silent=False, **kwargs)[source]

Create an interactive widget for selecting and downloading repositories from GitHub.

This function displays a list of repositories in a SelectMultiple widget, allowing the user to select one or more repositories. Upon clicking the “Submit” button, the selected repositories are downloaded, and a confirmation message is displayed for each successful download.

Finaly a clicable toc of all notebooks is displayed

Parameters:
  • loadspec (list of dict, optional) – A list of dictionaries containing repository information. Each dictionary

  • provided (should have keys like 'description' and 'repo_name'. If not)

  • model.get_a_repo_yaml() (the function will call)

  • github (which retrives the default list from)

  • None. (to retrieve the repository data. Default is)

Returns:

The function displays widgets and handles user interactions but does not return a value.

Return type:

None

Raises:

Exception – Prints exception messages to the output widget if errors occur during the download process.

class modelclass.Dekomp_Mixin[source]

Bases: object

This class defines methods and properties related to equation attribution analyses (dekomp)

dekomp(varnavn, start='', end='', basedf=None, altdf=None, lprint=True, time_att=False)[source]

Print all variables that determines input variable (varnavn) optional – enter period and databank to get var values for chosen period Optionally, enter the period and databank to get variable values for the chosen period.

Parameters:
  • varnavn (str) – Input variable name.

  • start (str, optional) – Start period for retrieving variable values (default is ‘’).

  • end (str, optional) – End period for retrieving variable values (default is ‘’).

  • basedf (pd.DataFrame, optional) – Base dataframe to use (default is None).

  • altdf (pd.DataFrame, optional) – Alternative dataframe to use (default is None).

  • lprint (bool, optional) – Flag to print the results (default is True).

  • time_att (bool, optional) – Flag to do a timewise attribute (default is False).

Returns:

A named tuple containing the following decomposition results: - diff_level : pd.DataFrame

DataFrame with level differences.

  • att_levelpd.DataFrame

    DataFrame with attributions to the level difference.

  • att_pctpd.DataFrame

    DataFrame with the share of attributions to the difference in level.

  • diff_growthpd.DataFrame

    DataFrame with differences in growth rate.

  • att_growthpd.DataFrame

    DataFrame with attributions to the difference in growth rate.

Return type:

namedtuple

impact(var, ldekomp=False, leq=False, adverse=None, base=None, maxlevel=3, start='', end='')[source]
dekomp_plot_per(varnavn, sort=False, pct=True, per='', threshold=0.0, rename=True, nametrans=<function Dekomp_Mixin.<lambda>>, time_att=False, ysize=7)[source]

Returns a waterfall diagram with attribution for a variable in one time frame

Parameters:
  • varnavn (TYPE) – variable name.

  • sort (TYPE, optional) – . The default is False.

  • pct (TYPE, optional) – display pct contribution . The default is True.

  • per (TYPE, optional) – DESCRIPTION. The default is ‘’.

  • threshold (TYPE, optional) – cutoff. The default is 0.0.

  • rename (TYPE, optional) – Use descriptions instead of variable names. The default is True.

  • time_att (TYPE, optional) – Do time attribution . The default is False.

Return type:

a matplotlib figure instance .

get_att_diff(n, type='pct', start='', end='', time_att=False, **kwargs)[source]
get_att(n, type='pct', filter=False, lag=True, start='', end='', time_att=False, threshold=0.0)[source]

Calculate the attribution percentage for a variable.

Parameters:
  • n (str) – Name of the variable to calculate attribution for.

  • type (str) – Type of attribution calculation. Options: ‘pct’ (percentage), ‘level’, ‘growth’. Default: ‘pct’.

  • filter (bool) – [Deprecated] Use threshold instead of filter. Default: False.

  • lag (bool) – Flag to indicate whether to include lag information in the output. Default: True.

  • start (str) – Start period for calculation. If not provided, uses the first period in the model instance. Default: ‘’.

  • end (str) – End period for calculation. If not provided, uses the last period in the model instance. Default: ‘’.

  • time_att (bool) – Flag to indicate time attribute calculation. Default: False.

  • threshold (float) – Threshold value for excluding rows with values close to zero. Default: 0.0.

Returns:

DataFrame containing the calculated attribution results.

Return type:

pandas.DataFrame

Raises:

Exception – If an invalid type is provided.

get_att_pct(n, filter=False, lag=True, start='', end='', time_att=False, threshold=0.0)[source]

det attribution pct for a variable. I little effort to change from multiindex to single node name

get_att_pct_to_from(to_var, from_var, lag=False, time_att=False)[source]

Get the attribution for a singel variable

get_att_level(n, filter=False, lag=True, start='', end='', time_att=False)[source]

det attribution pct for a variable. I little effort to change from multiindex to single node name

dekomp_plot(varnavn, sort=True, pct=True, per='', top=0.9, threshold=0.0, lag=True, rename=True, nametrans=<function Dekomp_Mixin.<lambda>>, time_att=False)[source]

Returns a chart with attribution for a variable over the smpl

Parameters:
  • varnavn (TYPE) – variable name.

  • sort (TYPE, optional) – . The default is False.

  • pct (TYPE, optional) – display pct contribution . The default is True.

  • per (TYPE, optional) – DESCRIPTION. The default is ‘’.

  • threshold (TYPE, optional) – cutoff. The default is 0.0.

  • rename (TYPE, optional) – Use descriptions instead of variable names. The default is True.

  • time_att (TYPE, optional) – Do time attribution . The default is False.

  • lag (TYPE, optional) – separete by lags The default is True.

  • top (TYPE, optional) – where to place the title

Return type:

a matplotlib figure instance .

get_dekom_gui(var='')[source]

Interactive wrapper around dekomp_plot and dekomp_plot_per

Parameters:

var (TYPE, optional) – start variable . Defaults to ‘’.

Returns:

dict of matplotlib figs .

Return type:

show (TYPE)

totexplain(pat='*', vtype='all', stacked=True, kind='bar', per='', top=0.9, title='', use='level', threshold=0.0)[source]

makes a total explanation for the variables defined by pat

Parameters:
  • pat (TYPE, optional) – DESCRIPTION. Defaults to ‘*’.

  • vtype (TYPE, optional) – DESCRIPTION. Defaults to ‘all’.

  • stacked (TYPE, optional) – DESCRIPTION. Defaults to True.

  • kind (TYPE, optional) – DESCRIPTION. Defaults to ‘bar’.

  • per (TYPE, optional) – DESCRIPTION. Defaults to ‘’.

  • top (TYPE, optional) – DESCRIPTION. Defaults to 0.9.

  • title (TYPE, optional) – DESCRIPTION. Defaults to ‘’.

  • use (TYPE, optional) – DESCRIPTION. Defaults to ‘level’.

  • threshold (TYPE, optional) – DESCRIPTION. Defaults to 0.0.

Returns:

DESCRIPTION.

Return type:

fig (TYPE)

totdif(summaryvar='*', desdic=None, experiments=None)[source]
get_att_gui(var='FY', spat='*', desdic={}, use='level', ysize=7)[source]

Creates a jupyter ipywidget to display model level attributions

class modelclass.Description_Mixin[source]

Bases: object

This Class defines description related methods and properties

property var_description

A dictionary with variable descriptions, if no value matching the key the variable name is returned

property var_description_reverse
set_var_description(a_dict)[source]

sets var_description Obsolete now just use = to set a new var_description

var_description_add(a_dict)[source]

adds a dict with var_descriptions to the var_description, new will overwrite old values

set_var_description_old(a_dict)[source]
static html_replace(ind)[source]

Replace special characters in html

var_des(var)[source]

Returns blank if no description

get_eq_des(var, show_all=False)[source]

Returns a string of descriptions for all variables in an equation:

get_des_html(var, show=1)[source]
static read_wb_xml_var_des(filename)[source]

Read a xml file with variable description world bank style

static languages_wb_xml_var_des(filename)[source]

Find languages in a xml file with variable description world bank style

set_wb_xml_var_description(filename, language='English')[source]

set variable descriptions from a xml file with variable description world bank style

enrich_var_description(var_description)[source]

Takes a dict of variable descriptions and enhance it for the standard suffixes for generated variables

deslist(pat)[source]

Returns a list of variable where the description in the model matching the pattern, the pattern can be a list of patterns

Parameters:

pat (string or list of strings) – One or more pattern seperated by space wildcards * and ?, special pattern: #ENDO

Returns:

list of variable names where the description matching the pat.

Return type:

out (list)

class modelclass.Modify_Mixin[source]

Bases: object

Class to modify a model with new equations, (later alse delete, and new normalization)

copy_properties(mmodel)[source]

Transfer useful propoerties from self to a new model

eqflip(flip=None, calc_add=True, newname='', sep='\n')[source]
Parameters:
  • newnormalisation (TYPE, optional) – Not implementet yet . The default is None.

  • newfunks (TYPE, optional) – Additional userspecified functions. The default is [].

  • calc_add (bool, optional) – Additional userspecified functions. The default is [].

Returns:

  • newmodel (TYPE) – The new model with the new and deleted equations .

  • newdf (TYPE) – a dataframe with calculated add factors. Origin is the original models lastdf.

eqdelete(deleteeq=None, newname='')[source]
Parameters:

deleteeq (TYPE, optional) – Variables where equations are to be deleted. The default is None.

Returns:

  • newmodel (TYPE) – The new model with the new and deleted equations .

  • newdf (TYPE) – a dataframe with calculated add factors. Origin is the original models lastdf.

equpdate(updateeq=None, newfunks=[], add_add_factor=False, calc_add=True, do_preprocess=True, newname='', silent=True)[source]
Parameters:
  • updateeq (TYPE) – new equations seperated by newline .

  • newfunks (TYPE, optional) – Additional userspecified functions. The default is [].

  • calc_add (bool, optional) – Additional userspecified functions. The default is [].

Returns:

  • newmodel (TYPE) – The new model with the new and deleted equations .

  • newdf (TYPE) – a dataframe with calculated add factors. Origin is the original models lastdf.

equpdate_old(updateeq=None, newfunks=[], add_adjust=False, calc_add=True, do_preprocess=True, newname='')[source]
Parameters:
  • updateeq (TYPE) – new equations seperated by newline .

  • newfunks (TYPE, optional) – Additional userspecified functions. The default is [].

  • calc_add (bool, optional) – Additional userspecified functions. The default is [].

Returns:

  • newmodel (TYPE) – The new model with the new and deleted equations .

  • newdf (TYPE) – a dataframe with calculated add factors. Origin is the original models lastdf.

class modelclass.Graph_Mixin[source]

Bases: object

This class defines graph related methods and properties

static create_strong_network(g, name='Network', typeout=False, show=False)[source]

create a solveorder and blockordering of af graph uses networkx to find the core of the model

property strongorder
property strongblock
property strongtype
property strongfrml

To search simultaneity (circularity) in a model this function returns the equations in each strong block

superblock()[source]

finds prolog, core and epilog variables

property prevar

returns a set with names of endogenopus variables which do not depend on current endogenous variables

property epivar

returns a set with names of endogenopus variables which do not influence current endogenous variables

property preorder

the endogenous variables which can be calculated in advance

property epiorder

the endogenous variables which can be calculated in advance

property coreorder

the solution order of the endogenous variables in the simultaneous core of the model

property coreset

The set of variables of the endogenous variables in the simultaneous core of the model

property precoreepiorder
property prune_endograph
property use_preorder
property totgraph_nolag

The graph of all variables, lagged variables condensed

property totgraph

Returns the total graph of the model, including leads and lags

property endograph_nolag

Dependencygraph for all periode endogeneous variable, shows total dependencies

property endograph_lag_lead

Returns the graph of all endogeneous variables including lags and leads

totgraph_get(onlyendo=False)[source]

The graph of all variables including and seperate lagged and leaded variable

onlyendo : only endogenous variables are part of the graph

graph_remove(paralist)[source]

Removes a list of variables from the totgraph and totgraph_nolag mostly used to remove parmeters from the graph, makes it less crowded

graph_restore()[source]

If nodes has been removed by the graph_remove, calling this function will restore them

static get_minimal_feedback_set(G)[source]

Compute an approximate minimal feedback vertex set for a directed graph and perform a topological sort on the resulting acyclic graph.

This function attempts to make the input directed graph acyclic by removing a minimal set of vertices (feedback vertex set). It does so by iteratively finding and removing vertices from cycles, preferring those with the highest degree (number of edges). After removing sufficient vertices to break all cycles, it performs a topological sort on the modified graph.

Parameters: G (nx.DiGraph): A directed graph represented as a NetworkX DiGraph.

Returns: tuple: A tuple containing two elements:

  • A set of vertices constituting the approximate minimal feedback vertex set.

  • A list representing the topological sort order of the modified graph.

If the input graph G is already acyclic, the feedback vertex set will be empty, and the topological sort order will be of the original graph G.

Note: This function does not guarantee the absolute minimal feedback vertex set due to the NP-hard nature of the problem. The topological sort is only valid if the resulting graph is acyclic.

class modelclass.Graph_Draw_Mixin[source]

Bases: object

This class defines methods and properties which draws and vizualize using different graphs of the model

treewalk(g, navn, level=0, parent='Start', maxlevel=20, lpre=True)[source]

Traverse the call tree from name, and returns a generator

to get a list just write: list(treewalk(…)) maxlevel determins the number of generations to back up

lpre=0 we walk the dependent lpre=1 we walk the precednc nodes

drawendo(**kwargs)[source]

draws a graph of of the whole model

drawendo_lag_lead(**kwargs)[source]

draws a graph of of the whole model

drawmodel(lag=True, **kwargs)[source]

draws a graph of of the whole model

plotadjacency(size=(5, 5), title='Structure', nolag=False)[source]

Draws an adjacendy matrix

Parameters:
  • size (TYPE, optional) – DESCRIPTION. Defaults to (5, 5).

  • title (TYPE, optional) – DESCRIPTION. Defaults to ‘Structure’.

  • nolag (TYPE, optional) – DESCRIPTION. Defaults to False.

Returns:

A adjacency matrix drawing.

Return type:

fig (matplotlib figure)

draw(navn, down=1, up=1, lag=False, endo=False, filter=0, **kwargs)[source]

draws a graph of dependensies of navn up to maxlevel

Lag:

show the complete graph including lagged variables else only variables.

Endo:

Show only the graph for current endogenous variables

Down:

level downstream

Up:

level upstream

trans(ind, root, transdic=None, debug=False)[source]

as there are many variable starting with SHOCK, the can renamed to save nodes

color(v, navn='')[source]
upwalk(g, navn, level=0, parent='Start', maxlevel=20, filter=0.0, lpre=True)[source]

Traverse the call tree from name, and returns a generator

to get a list just write: list(upwalk(…)) maxlevel determins the number of generations to back maxlevel

upwalk_old(g, navn, level=0, parent='Start', up=20, select=0.0, lpre=True)[source]

Traverse the call tree from name, and returns a generator

to get a list just write: list(upwalk(…)) up determins the number of generations to back up

explain(var, up=1, start='', end='', filter=0, showatt=True, lag=True, debug=0, noshow=False, dot=False, **kwargs)[source]

Walks a tree to explain the difference between basedf and lastdf

Parameters: :var: the variable we are looking at :up: how far up the tree will we climb :select: Only show the nodes which contributes :showatt: Show the explanation in pct :lag: If true, show all lags, else aggregate lags for each variable. :HR: if true make horisontal graph :title: Title :saveas: Filename :pdf: open the pdf file :svg: display the svg file :browser: if true open the svg file in browser :noshow: Only return the resulting graph :dot: Return the dot file only

dftodottable(df, dec=0)[source]
todot(g, navn='', browser=False, **kwargs)[source]

makes a drawing of subtree originating from navn all is the edges attributex can be shown

Sink:

variale to use as sink

Svg:

Display the svg image

gdraw(g, **kwargs)[source]

draws a graph of of the whole model

maketip(v, html=False)[source]

Return a tooltip for variable v.

For use when generating .dot files for Graphviz

If html==True it can be incorporated into html string

makedotnew(alledges, navn='', **kwargs)[source]

makes a drawing of all edges in list alledges all is the edges

this can handle both attribution and plain

All:

show values for .dfbase and .lastdf

Last:

show the values for .lastdf

Growthshow:

Show growthrates

Attshow:

Show attributiuons

Filter:

Prune tree branches where all(abs(attribution)<filter value)

Sink:

variale to use as sink

Source:

variale to use as ssource

Svg:

Display the svg image in browser

Pdf:

display the pdf result in acrobat reader

Saveas:

Save the drawing as name

Size:

figure size default (6,6)

Warnings:

warnings displayed in command console, default =False

Invisible:

set of invisible nodes

Labels:

dict of labels for edges

Transdic:

dict of translations for consolidation of nodes {‘SHOCK[_A-Z]*__J’:’SHOCK__J’,’DEV__[_A-Z]*’:’DEV’}

Dec:

decimal places in numbers

HR:

horisontal orientation default = False

Des:

inject variable descriptions

Fokus2:

Variable for which values are shown

Fokus2all:

Show values for all variables

todot2(alledges, navn='', **kwargs)[source]

makes a drawing of all edges in list alledges all is the edges

All:

show values for .dfbase and .dflaste

Last:

show the values for .dflast

Sink:

variale to use as sink

Source:

variale to use as ssource

Svg:

Display the svg image in browser

Pdf:

display the pdf result in acrobat reader

Saveas:

Save the drawing as name

Size:

figure size default (6,6)

Warnings:

warnings displayed in command console, default =False

Invisible:

set of invisible nodes

Labels:

dict of labels for edges

Transdic:

dict of translations for consolidation of nodes {‘SHOCK[_A-Z]*__J’:’SHOCK__J’,’DEV__[_A-Z]*’:’DEV’}

Dec:

decimal places in numbers

HR:

horisontal orientation default = False

Des:

inject variable descriptions

display_graph_old(dot, fname, browser, kwargs)[source]
static display_graph(out, fname, **kwargs)[source]

Generates a graphviz file from the commands in out.

The file is placed in cwd/graph

A png and a svg file is generated, and the svg file is displayed if possible.

options pdf and eps determins if a pdf and an eps file is genrated.

option fpdf will cause the graph displayed in a seperate pdf window

option browser determins if a seperate browser window is open

class modelclass.Display_Mixin[source]

Bases: object

vis(*args, **kwargs)[source]

Visualize the data of this model instance if the user has another vis class she can place it in _vis, then that will be used

varvis(*args, **kwargs)[source]
compvis(*args, **kwargs)[source]
ibsstyle_old(df, description_dict=None, dec=2, transpose=None)[source]
Parameters:
  • df (TYPE) – Dataframe.

  • description_dict (TYPE, optional) – Defaults to None then the var_description else this dict .

  • dec (TYPE, optional) – decimals. Defaults to 2. Deciu

  • transpose (TYPE, optional) – if Trus then rows are time else thje. Defaults to 0.

Returns:

DESCRIPTION.

Return type:

TYPE

ibsstyle(df, description_dict=None, dec=2, transpose=None, use_tooltip=True, percent=False)[source]
Parameters:
  • df (TYPE) – Dataframe.

  • description_dict (TYPE, optional) – Defaults to None then the var_description else this dict .

  • dec (TYPE, optional) – decimals. Defaults to 2. Deciu

  • transpose (TYPE, optional) – if Trus then rows are time else thje. Defaults to 0.

Returns:

DESCRIPTION.

Return type:

TYPE

static compstyle(df)[source]

returns a styled dataframe of complex numners

Parameters:

df (dataframe .)

Return type:

None.

write_eq(name='My_model.fru', lf=True)[source]

writes the formulas to file, can be input into model

lf=True -> new lines are put after each frml

print_eq(varnavn, data='', start='', end='')[source]

Print all variables that determines input variable (varnavn) optional – enter period and databank to get var values for chosen period

print_eq_values(varname, databank=None, all=False, dec=1, lprint=1, per=None)[source]

for an endogeneous variable, this function prints out the frml and input variale for each periode in the current_per. The function takes special acount of dataframes and series

print_all_eq_values(databank=None, dec=1)[source]
print_eq_mul(varnavn, grund='', mul='', start='', end='', impact=False)[source]

Print all variables that determines input variable (varnavn) optional – enter period and databank to get var values for chosen period

print_all_equations(inputdata, start, end)[source]

Print values and formulas for alle equations in the model, based input database and period

Example: stress.print_all_equations(bankdata,’2013Q3’)

print_lister()[source]

prints the lists used in defining the model

keep_print(pat='*', start='', end='', start_ofset=0, end_ofset=0, diff=True)[source]

prints variables from experiments look at keep_get_dict for options

keep_get_df(pat='*')[source]
keep_var_dict(pat='*', start='', end='', start_ofset=0, end_ofset=0, diff=False, trans=True)[source]

Returns a dict of the keept experiments. Key is the scrnario names, values are a dataframe with values for each variable

Args:

pat (TYPE, optional): variable selection. Defaults to ‘*’. start (TYPE, optional): start period. Defaults to ‘’. end (TYPE, optional): end period. Defaults to ‘’. start_ofset (TYPE, optional): start ofset period. Defaults to 0. end_ofset (TYPE, optional): end ofste period. Defaults to 0.

Returns:

res (dictionary): a dict with a dataframe for each experiment

keep_get_dict(pat='*', start='', end='', start_ofset=0, end_ofset=0, diff=False, trans=False)[source]

Returns a dict of the keept experiments. Key is the variable names, values are a dataframe with variable values for each experiment

Args:

pat (TYPE, optional): variable selection. Defaults to ‘*’. start (TYPE, optional): start period. Defaults to ‘’. end (TYPE, optional): end period. Defaults to ‘’. start_ofset (TYPE, optional): start ofset period. Defaults to 0. end_ofset (TYPE, optional): end ofste period. Defaults to 0.

Returns:

res (dictionary): a dict with a dataframe for each experiment

keep_get_plotdict(pat='*', start='', end='', start_ofset=0, end_ofset=0, showtype='level', diff=False, diffpct=False, by_var=1)[source]

returns - a dict of {variable in pat :dfs scenarios as columnns } if by_var = 1 - a dict of {scenarios :dfs with variables in pat as columnns } if by_var = 1

Parameters:
  • pat (string, optional) – Variable selection. Defaults to ‘*’.

  • start (TYPE, optional) – start periode. Defaults to ‘’.

  • end (TYPE, optional) – end periode. Defaults to ‘’.

  • showtype (str, optional) – ‘level’,’growth’ or change’ transformation of data. Defaults to ‘level’.

  • diff (Logical, optional) – if True shows the difference to the first experiment or the first scenario. Defaults to False.

  • diffpct (logical,optional) – if True shows the difference in percent instead of level

  • mul (float, optional) – multiplier of data. Defaults to 1.0.

Returns:

a dict with data

Return type:

figs

keep_get_plotdict_new(pat='*', start='', end='', showtype='level', diftype='nodif', by_var=True)[source]

returns - a dict of {variable in pat :dfs scenarios as columnns } if by_var = 1 - a dict of {scenarios :dfs with variables in pat as columnns } if by_var = 0

Parameters:
  • pat (string, optional) – Variable selection. Defaults to ‘*’.

  • start (TYPE, optional) – start periode. Defaults to ‘’.

  • end (TYPE, optional) – end periode. Defaults to ‘’.

  • start_ofset (integer,optional) – relativ start ofset

  • end_ofset (integer,optional) – relativ end ofset

  • showtype (str, optional) – ‘level’,’growth’, ‘gdppct’ or change’ transformation of data. Defaults to ‘level’.

  • diftype (str, optional) – ‘nodif’,’dif’ or ‘difpct’ transformation of data.

findgdpvar(varname)[source]

Find matching GDP variable - for worldbank models

inputwidget(start='', end='', basedf=None, **kwargs)[source]

calls modeljupyter input widget, and keeps the period scope

plot_basis(var, df, title='', suptitle='', legend=True, scale='linear', trans={}, dec='', ylabel='', yunit='', xlabel='', kind='line', multi=False)[source]
static plot_basis_ax(ax, var, df, ax_title='', suptitle='', legend=True, scale='linear', trans={}, dec='', ylabel='', yunit='', xlabel='', kind='line', samefig=False)[source]
static savefigs(figs, location='./graph', experimentname='experiment1', addname='', extensions=['svg'], xopen=False)[source]

Saves a collection of matplotlib figures to a specified directory.

Parameters: - figs (dict): A dictionary of matplotlib figures where the key is the figure name. - location (str): The base folder in which to save the charts. Defaults to ‘./graph’. - experimentname (str): A subfolder under ‘location’ where charts are saved. Defaults to ‘experiment1’. - addname (str): An additional name added to each figure filename. Defaults to an empty string. - extensions (list): A list of string file extensions for saving the figures. Defaults to [‘svg’]. - xopen (bool): If True, open the saved figure locations in a web browser.

Returns: str: The absolute path to the folder where figures are saved.

Raises: Exception: If the folder cannot be created or a figure cannot be saved/opened.

keep_plot(pat='*', start='', end='', start_ofset=0, end_ofset=0, showtype='level', diff=False, diffpct=False, mul=1.0, title='Scenarios', legend=False, scale='linear', yunit='', ylabel='', dec='', trans=None, showfig=True, kind='line', size=(10, 6), vline=None, savefig='', by_var=True, dataonly=False, samefig=False, ncol=2)[source]

Generate and display plots for specified scenarios and variables.

Parameters:
  • pat (str, optional) – Pattern to select variables for plotting. Defaults to ‘*’.

  • start (str, optional) – Start period for the plot. Defaults to ‘’ .

  • end (str, optional) – End period for the plot. Defaults to ‘’ .

  • start_ofset (int, optional) – Offset to shift the start period, relative to current. Defaults to 0.

  • end_ofset (int, optional) – Offset to shift the end period, relative to current. Defaults to 0.

  • showtype (str, optional) – Type of data transformation for plotting (‘level’, ‘growth’, ‘change’). Defaults to ‘level’.

  • diff (bool, optional) – If True, shows the difference relative to the first experiment. Defaults to False.

  • diffpct (bool, optional) – If True, shows the percentage difference relative to the first experiment. Defaults to False.

  • mul (float, optional) – Multiplier to scale the data. Defaults to 1.0.

  • title (str, optional) – Title of the plot. Defaults to ‘Scenarios’.

  • legend (bool, optional) – If True, displays a legend. Defaults to False.

  • scale (str, optional) – Y-axis scale (‘linear’ or ‘log’). Defaults to ‘linear’.

  • yunit (str, optional) – Units for the Y-axis. Defaults to ‘’.

  • ylabel (str, optional) – Label for the Y-axis. Defaults to ‘’.

  • dec (str, optional) – String format for decimal places. If ‘’ then automatically determined. Defaults to ‘’.

  • trans (dict, optional) – Alternative dictionary for translating variable names to desciptions. Defaults to None.

  • showfig (bool, optional) – If True, displays the figure. Defaults to True.

  • kind (str, optional) – Type of plot (‘line’, ‘bar’, etc.). Defaults to ‘line’.

  • size (tuple, optional) – Figure size as (width, height). Defaults to (10, 6).

  • vline (list, optional) – List of tuples (time, text) for vertical lines in the plot. vline is persistent, to reset vline=None. Defaults to an empty list.

  • by_var (bool, optional) – If True, each line represents a scenario, else each line represents a variable. Defaults to True.

  • dataonly (bool, optional) – If True, only the dataframes are returned, no plot is generated. Defaults to False.

  • samefig (bool, optional) – If True, all plots are displayed in the same figure. Defaults to False.

  • ncol (int, optional) – Number of columns for subplots when using samefig. Defaults to 2.

Returns:

A dictionary of Matplotlib figures, with keys being the variable names and values being the figure objects.

Return type:

dict

Raises:

ZeroDivisionError – If no kept solution is available for plotting.

static keep_add_vline(figs, time, text='  Calibration time')[source]

adds a vertical line with text to figs a dict with matplotlib figures) from keep_plot

keep_viz(pat='*', smpl=('', ''), selectfrom={}, legend=1, dec='', use_descriptions=True, select_width='', select_height='200px', vline=[])[source]

Plots the keept dataframes

Parameters:
  • pat (str, optional) – a string of variables to select pr default. Defaults to ‘*’.

  • smpl (tuple with 2 elements, optional) – the selected smpl, has to match the dataframe index used. Defaults to (‘’,’’).

  • selectfrom (list, optional) – the variables to select from, Defaults to [] -> all endogeneous variables .

  • legend (bool, optional) – DESCRIPTION. legends or to the right of the curve. Defaults to 1.

  • dec (string, optional) – decimals on the y-axis. Defaults to ‘0’.

  • use_descriptions – Use the variable descriptions from the model

Returns:

None.

self.keep_wiz_figs is set to a dictionary containing the figures. Can be used to produce publication quality files.

keep_viz_prefix(pat='*', smpl=('', ''), selectfrom={}, legend=1, dec='', use_descriptions=True, select_width='', select_height='200px', vline=[], prefix_dict={}, add_var_name=False, short=False)[source]

Plots the keept dataframes

Parameters:
  • pat (str, optional) – a string of variables to select pr default. Defaults to ‘*’.

  • smpl (tuple with 2 elements, optional) – the selected smpl, has to match the dataframe index used. Defaults to (‘’,’’).

  • selectfrom (list, optional) – the variables to select from, Defaults to [] -> all keept variables .

  • legend (bool, optional) – DESCRIPTION. legends or to the right of the curve. Defaults to 1.

  • dec (string, optional) – decimals on the y-axis. Defaults to ‘0’.

  • use_descriptions – Use the variable descriptions from the model

Returns:

None.

self.keep_wiz_figs is set to a dictionary containing the figures. Can be used to produce publication quality files.

keep_plot_widget(*args, **kwargs)[source]

Provides an interactive widget to plot data from ModelFlow model instances. It allows for selection of variables, scenarios, and display types, and can show differences between scenarios in various formats.

Parameters:
  • mmodel – A ModelFlow model instance.

  • smpl (Tuple[str, str], optional) – Sample period for plotting (start, end). Defaults to (‘’, ‘’).

  • use_smpl (bool, optional) – If True, enables a sample period selection slider. Defaults to False.

  • selectfrom (str, optional) – Space-separated string of variable names to select from. If empty, all kept variables are available. Defaults to ‘*’.

  • legend (bool, optional) – If True, displays legends next to the plots. Defaults to False.

  • dec (str, optional) – Format string for decimal places on the y-axis. Defaults to ‘’.

  • use_descriptions (bool, optional) – If True, uses variable descriptions from the model. Defaults to True.

  • vline (any, optional) – List of vertical lines for the plot (position, text). Defaults to None.

  • add_var_name (bool, optional) – If True, adds variable names to descriptions. Defaults to False.

  • short (any, optional) – If set, reduces the number of input fields. Defaults to 0.

  • select_scenario (bool, optional) – If True, allows selecting scenarios to display. Defaults to True.

  • switch (bool, optional) – If True, uses scenarios from mmodel.basedf and mmodel.lastdf. Defaults to False.

  • var_groups (dict, optional) – Dictionary of variable patterns for selection, like country groups. If empty use mmodel.var_groups. Defaults to an empty dict.

  • use_var_groups (bool, optional) – If True, enables selection using var_groups. Defaults to True.

  • displaytype (str, optional) – Type of display (‘tab’, ‘accordion’, or other). Defaults to ‘tab’.

  • save_location (str, optional) – Default location for saving plots. Defaults to ‘./graph’.

  • use_smpl – If True, enables a sample selection slider. Defaults to False.

Properties:

show: Displays the widget. datawidget: The actual interactive widget.

Returns:

An instance of keep_plot_widget. This instance’s ‘keep_wiz_figs’ property is set to a dictionary containing the figures, which can be used for creating publication-quality files.

keep_show(*args, **kwargs)[source]

Provides an interactive widget to plot data from ModelFlow model instances. It allows for selection of variables, scenarios, and display types, and can show differences between scenarios in various formats.

Parameters:
  • mmodel – A ModelFlow model instance.

  • smpl (Tuple[str, str], optional) – Sample period for plotting (start, end). Defaults to (‘’, ‘’).

  • use_smpl (bool, optional) – If True, enables a sample period selection slider. Defaults to False.

  • selectfrom (str, optional) – Space-separated string of variable names to select from. If empty, all kept variables are available. Defaults to ‘*’.

  • legend (bool, optional) – If True, displays legends next to the plots. Defaults to False.

  • dec (str, optional) – Format string for decimal places on the y-axis. Defaults to ‘’.

  • use_descriptions (bool, optional) – If True, uses variable descriptions from the model. Defaults to True.

  • vline (any, optional) – List of vertical lines for the plot (position, text). Defaults to None.

  • add_var_name (bool, optional) – If True, adds variable names to descriptions. Defaults to False.

  • short (any, optional) – If set, reduces the number of input fields. Defaults to 0.

  • select_scenario (bool, optional) – If True, allows selecting scenarios to display. Defaults to True.

  • switch (bool, optional) – If True, uses scenarios from mmodel.basedf and mmodel.lastdf. Defaults to False.

  • var_groups (dict, optional) – Dictionary of variable patterns for selection, like country groups. If empty use mmodel.var_groups. Defaults to an empty dict.

  • use_var_groups (bool, optional) – If True, enables selection using var_groups. Defaults to True.

  • displaytype (str, optional) – Type of display (‘tab’, ‘accordion’, or other). Defaults to ‘tab’.

  • save_location (str, optional) – Default location for saving plots. Defaults to ‘./graph’.

  • use_smpl – If True, enables a sample selection slider. Defaults to False.

Properties:

show: Displays the widget. datawidget: The actual interactive widget.

Returns:

An instance of keep_plot_widget. This instance’s ‘keep_wiz_figs’ property is set to a dictionary containing the figures, which can be used for creating publication-quality files.

df_show(*args, **kwargs)[source]
df_plot(*args, **kwargs)[source]

Generate and display plots for specified scenarios and variables.

Parameters:
  • pat (str, optional) – Pattern to select variables for plotting. Defaults to ‘*’.

  • start (str, optional) – Start period for the plot. Defaults to ‘’ .

  • end (str, optional) – End period for the plot. Defaults to ‘’ .

  • start_ofset (int, optional) – Offset to shift the start period, relative to current. Defaults to 0.

  • end_ofset (int, optional) – Offset to shift the end period, relative to current. Defaults to 0.

  • showtype (str, optional) – Type of data transformation for plotting (‘level’, ‘growth’, ‘change’). Defaults to ‘level’.

  • diff (bool, optional) – If True, shows the difference relative to the first experiment. Defaults to False.

  • diffpct (bool, optional) – If True, shows the percentage difference relative to the first experiment. Defaults to False.

  • mul (float, optional) – Multiplier to scale the data. Defaults to 1.0.

  • title (str, optional) – Title of the plot. Defaults to ‘Scenarios’.

  • legend (bool, optional) – If True, displays a legend. Defaults to False.

  • scale (str, optional) – Y-axis scale (‘linear’ or ‘log’). Defaults to ‘linear’.

  • yunit (str, optional) – Units for the Y-axis. Defaults to ‘’.

  • ylabel (str, optional) – Label for the Y-axis. Defaults to ‘’.

  • dec (str, optional) – String format for decimal places. If ‘’ then automatically determined. Defaults to ‘’.

  • trans (dict, optional) – Alternative dictionary for translating variable names to desciptions. Defaults to None.

  • showfig (bool, optional) – If True, displays the figure. Defaults to True.

  • kind (str, optional) – Type of plot (‘line’, ‘bar’, etc.). Defaults to ‘line’.

  • size (tuple, optional) – Figure size as (width, height). Defaults to (10, 6).

  • vline (list, optional) – List of tuples (time, text) for vertical lines in the plot. vline is persistent, to reset vline=None. Defaults to an empty list.

  • by_var (bool, optional) – If True, each line represents a scenario, else each line represents a variable. Defaults to True.

  • dataonly (bool, optional) – If True, only the dataframes are returned, no plot is generated. Defaults to False.

  • samefig (bool, optional) – If True, all plots are displayed in the same figure. Defaults to False.

  • ncol (int, optional) – Number of columns for subplots when using samefig. Defaults to 2.

Returns:

A dictionary of Matplotlib figures, with keys being the variable names and values being the figure objects.

Return type:

dict

Raises:

ZeroDivisionError – If no kept solution is available for plotting.

static display_toc(text='**Jupyter notebooks**', folder='.', all=False, nocwd=False)[source]

In a jupyter notebook this function displays a clickable table of content of all jupyter notebooks in this and sub folders

static display_toc_this(pat='*', text='**Jupyter notebooks**', path='.', ext='ipynb', showext=False)[source]

In a jupyter notebook this function displays a clickable table of content in the folder pat with name in path

static widescreen()[source]

Makes a jupyter notebook use all the avaiable real estate

static scroll_off()[source]
static scroll_on()[source]
static modelflow_auto(run=True)[source]

In a jupyter notebook this function activate autorun of the notebook.

Also it makes Jupyter use a larger portion of the browser width

The function should be run before the notebook is saved, and the output should not be cleared

class modelclass.Json_Mixin[source]

Bases: object

This mixin class can dump a model and solution as json serialiation to a file.

allows the precooking of a model and solution, so a user can use a model without specifying it in a session.

modeldump_base(outfile='', keep=False)[source]

Dumps a model and its lastdf to a json file

if keep=True the model.keep_solutions will alse be dumped

classmethod modelload(infile, funks=[], run=False, keep_json=False, default_url='https://raw.githubusercontent.com/IbHansen/modelflow-manual/main/model_repo/', **kwargs)[source]
Parameters:
  • infile (A file name or an url with a .pcim file)

  • funks (Functions to use in the resulting model)

  • run (simulate the model with the saved time and options)

  • keep_json (save a dict (self.json_keep) in the model instance)

  • default_url (TYPE, optional) – Where to look if the file is not avaiable. The default is r’https://raw.githubusercontent.com/IbHansen/modelflow-manual/main/model_repo/’.

  • **kwargs (These options are used by the simulation if run=True)

Return type:

(<a model instance>,<a dataframe>) .

class modelclass.Zip_Mixin[source]

Bases: object

This experimental class zips a dumped file

modeldump(file_path='', keep=False, zip_file=True)[source]
class modelclass.Excel_Mixin[source]

Bases: object

This Mixin handels dumps and loads models into excel

modeldump_excel(file, fromfile='control.xlsm', keep_open=False)[source]

Dump model and dataframe to excel workbook

Parameters:
  • file (TYPE) – filename.

  • keep_open (TYPE, optional) – Keep the workbook open in excel after returning, The default is False.

Returns:

wb – xlwings instance of workbook .

Return type:

TYPE

classmethod modelload_excel(infile='pak', funks=[], run=False, keep_open=False, **kwargs)[source]

Loads a model from a excel workbook dumped by modeldump_excel

Parameters:
  • cls (TYPE) – DESCRIPTION.

  • infile (TYPE, optional) – DESCRIPTION. Defaults to ‘pak’.

  • funks (TYPE, optional) – DESCRIPTION. Defaults to [].

  • run (TYPE, optional) – DESCRIPTION. Defaults to False.

  • keep_open (TYPE, optional) – DESCRIPTION. Defaults to False.

  • **kwargs (TYPE) – DESCRIPTION.

Returns:

DESCRIPTION. res (TYPE): DESCRIPTION. TYPE: DESCRIPTION.

Return type:

mmodel (TYPE)

class modelclass.Solver_Mixin[source]

Bases: object

This Mixin handels the solving of models.

DEFAULT_relconv = 1e-07
__call__(*args, **kwargs)[source]

Runs a model.

Default a straight model is calculated by xgenr a simultaneous model is solved by sim

Sim:

If False forces a model to be calculated (not solved) if True force simulation

Setbase:

If True, place the result in model.basedf

Setlast:

if False don’t place the results in model.lastdf

if the modelproperty previousbase is true, the previous run is used as basedf.

Parameters:
  • sim (bool, optional) – If False, forces a model to be calculated (not solved); if True, forces simulation. The default behavior is determined by the model’s properties.

  • setbase (bool, optional) – If True, places the result in model.basedf. Default is determined by the model’s state.

  • setlast (bool, optional) – If False, doesn’t place the results in model.lastdf. Default is True.

  • antal (not applicable, optional) – This option is not valid. If provided, an assertion error is raised.

  • do_calc_add_factor (bool, optional) – Determines whether to calculate the adjustment factor if the calc adjust model is present. Default is True.

  • reset_options (bool, optional) – If True, the previous options will be reset. Default is False.

  • save (bool, optional) – If True, saves the current state. The default behavior is determined by the model’s properties.

  • solver (str, optional) – Specifies the solver to be used. The default solver is chosen based on the model’s properties.

  • silent (bool, optional) – If True, the solver runs silently without printing output to the console. Default is True.

  • cache_clear (bool, optional) – If True, clears the cache after solving. Default is True.

  • keep (str, optional) – If provided, keeps the solutions. The exact behavior depends on the ‘keep_variables’ option.

  • keep_variables (str or list of str, optional) – Specifies which variables to keep if the ‘keep’ option is provided. Default is to keep all variables.

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

outdf – The DataFrame containing the results of the model run.

Return type:

pandas.DataFrame

Raises:

AssertionError – If the ‘antal’ option is provided, as it is not a valid simulation option.

calc_add_factor(outdf, silent=True)[source]
property showstartnr
makelos(databank, ljit=0, stringjit=False, solvename='sim', chunk=30, transpile_reset=False, newdata=False, silent=True, **kwargs)[source]
is_newdata(databank)[source]

Determins if thius is the same databank as in the previous solution

sim(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=5, max_iterations=200, conv='*', absconv=0.01, relconv=1e-07, stringjit=False, transpile_reset=False, dumpvar='*', init=False, ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, ljit=False, timeon=False, fairopt={'fair_max_iterations ': 1}, progressbar=False, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function

Then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

Solves using Gauss-Seidle

Parameters:
  • databank (dataframe) – Input dataframe

  • start (optional) – start of simulation, defaults to ‘’

  • end (optional) – end of simulation, defaults to ‘’

  • silent (bool, optional) – keep simulation silent , defaults to 1

  • samedata (bool, optional) – the inputdata has exactly same structure as last simulation , defaults to 0

  • alfa (float, optional) – Dampeing factor, defaults to 1.0

  • stats (bool, optional) – Show statistic after finish, defaults to False

  • first_test (int, optional) – Start testing af number og simulation, defaults to 5

  • max_iterations (int, optional) – Max iterations, defaults to 200

  • conv (str, optional) – variables to test for convergence, defaults to ‘*’

  • absconv (float, optional) – Test convergence for values above this, defaults to 0.01

  • relconv (float, optional) – If relative movement is less, then convergence , defaults to DEFAULT_relconv

  • stringjit (bool, optional) – If just in time compilation do it on a string not a file to import, defaults to True

  • transpile_reset (bool, optional) – Ingnore previous transpiled model, defaults to False

  • dumpvar (str, optional) – Variables for which to dump the iterations, defaults to ‘*’

  • init (bool, optional) – If True take previous periods value as starting value, defaults to False

  • ldumpvar (bool, optional) – Dump iterations, defaults to False

  • dumpwith (int, optional) – DESCRIPTION, defaults to 15

  • dumpdecimal (int, optional) – DESCRIPTION, defaults to 5

  • chunk (int, optional) – Chunk size of transpiled model, defaults to 30

  • ljit (bool, optional) – Use just in time compilation, defaults to False

  • timeon (bool, optional) – Time the elements, defaults to False

  • fairopt (TYPE, optional) – Fair taylor options, defaults to {‘fair_max_iterations’: 1}

  • progressbar (TYPE, optional) – Show progress bar , defaults to False

Returns:

A dataframe wilt the results

static grouper(iterable, n, fillvalue='')[source]

Collect data into fixed-length chunks or blocks

outsolve2dcunk(databank, debug=1, chunk=None, ljit=False, type='gauss', cache=False)[source]

takes a list of terms and translates to a evaluater function called los

The model axcess the data through:Dataframe.value[rowindex+lag,coloumnindex] which is very efficient

sim1d(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=1, max_iterations=100, conv='*', absconv=1.0, relconv=1e-07, init=False, dumpvar='*', ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, ljit=False, stringjit=True, transpile_reset=False, fairopt={'fair_max_iterations ': 1}, timeon=0, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

outsolve1dcunk(debug=0, chunk=None, ljit=False, cache='False')[source]

takes a list of terms and translates to a evaluater function called los

The model axcess the data through:Dataframe.value[rowindex+lag,coloumnindex] which is very efficient

newton(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=1, newton_absconv=0.001, max_iterations=20, conv='*', absconv=1.0, relconv=1e-07, nonlin=False, timeit=False, newton_reset=1, dumpvar='*', ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, ljit=False, stringjit=False, transpile_reset=False, lnjit=False, init=False, newtonalfa=1.0, newtonnodamp=0, forcenum=True, fairopt={'fair_max_iterations ': 1}, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

newtonstack(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=1, newton_absconv=0.001, max_iterations=20, conv='*', absconv=1.0, relconv=1e-07, dumpvar='*', ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, nchunk=30, ljit=False, stringjit=False, transpile_reset=False, nljit=0, fairopt={'fair_max_iterations ': 1}, debug=False, timeit=False, nonlin=False, newtonalfa=1.0, newtonnodamp=0, forcenum=True, newton_reset=False, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

newton_un_normalized(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=1, newton_absconv=0.001, max_iterations=20, conv='*', absconv=1.0, relconv=1e-07, nonlin=False, timeit=False, newton_reset=1, dumpvar='*', ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, ljit=False, stringjit=False, transpile_reset=False, lnjit=False, fairopt={'fair_max_iterations ': 1}, newtonalfa=1.0, newtonnodamp=0, forcenum=True, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

newtonstack_un_normalized(databank, start='', end='', silent=1, samedata=0, alfa=1.0, stats=False, first_test=1, newton_absconv=0.001, max_iterations=20, conv='*', absconv=1.0, relconv=1e-07, dumpvar='*', ldumpvar=False, dumpwith=15, dumpdecimal=5, chunk=30, nchunk=None, ljit=False, nljit=0, stringjit=False, transpile_reset=False, fairopt={'fair_max_iterations ': 1}, debug=False, timeit=False, nonlin=False, newtonalfa=1.0, newtonnodamp=0, forcenum=True, newton_reset=False, **kwargs)[source]

Evaluates this model on a databank from start to end (means end in Danish).

First it finds the values in the Dataframe, then creates the evaluater function through the outeval function (modelclass.model.fouteval()) then it evaluates the function and returns the values to a the Dataframe in the databank.

The text for the evaluater function is placed in the model property make_los_text where it can be inspected in case of problems.

res(databank, start='', end='', debug=False, timeit=False, silent=False, chunk=None, ljit=0, stringjit=False, transpile_reset=False, alfa=1, stats=0, samedata=False, **kwargs)[source]

calculates the result of a model, no iteration or interaction The text for the evaluater function is placed in the model property make_res_text where it can be inspected in case of problems.

invert(databank, targets, instruments, silent=1, defaultimpuls=0.01, defaultconv=0.001, nonlin=False, maxiter=30, delay=0, varimpulse=False, progressbar=True, debug=False)[source]

Solves a target instrument problem

Number of targets should be equal to number of instruments

An instrument can comprice of severeral variables

Instruments are inputtet as a list of instruments

To calculate the jacobian each instrument variable has a impuls, which is used as delta when evaluating the jacobi matrix:

[ 'QO_J','TG']   Simple list each variable are shocked by the default impulse
[ ('QO_J',0.5), 'TG']  Here QO_J is getting its own impuls (0.5)
[ [('QO_J',0.5),('ORLOV',1.)] , ('TG',0.01)] here an impuls is given for each variable, and the first instrument consiste of two variables

Targets are list of variables

Convergence is achieved when all targets are within convergens distance from the target value

Convergencedistance can be set individual for a target variable by setting a value in <modelinstance>.targetconv

Targets and target values are provided by a dataframe.

Parameters
databankTYPE

values to run on .

targetsTYPE

dataframe with a column for each target.

instrumentsTYPE

list of instruments .

modelTYPE

the model to use .

DefaultImpulsTYPE, optional

default delta . The default is 0.01.

defaultconvTYPE, optional

default convergence . The default is 0.01.

delayTYPE, optional

delay in effects . The default is 0.

nonlinTYPE, optional

if a number the number of iterations to trigger recalculation of jacobi. The default is False.

silentTYPE, optional

show iterations if false. The default is True.

maxiterTYPE, optional

max newton iteration. The default is 30.

solveoptTYPE, optional

options to bring to the solver. The default is {}.

varimpulseTYPE, optional

if True only update the current period, else update into the future. The default is False.

a dataframe with the result .

errfunk1d(a, linenr, overhead=4, overeq=0)[source]

Handle errors in sim1d

errfunk(values, linenr, overhead=4, overeq=0)[source]

developement function

to handle run time errors in model calculations

show_iterations(pat='*', per='', last=0, change=False, top=0.9)[source]

shows the last iterations for the most recent simulation. iterations are dumped if ldumpvar is set to True variables can be selceted by: dumpvar = ‘<pattern>’

Parameters:
  • pat (TYPE, optional) – Variables for which to show iterations . Defaults to ‘*’.

  • per (TYPE, optional) – The time frame for which to show iterations, Defaults to the last projection .

  • last (TYPE, optional) – Only show the last iterations . Defaults to 0.

  • change (TYPE, optional) – show the changes from iteration to iterations instead of the levels. Defaults to False.

  • top (float,optional) – top of chartss between 1 and 0

Returns:

DESCRIPTION.

Return type:

fig (TYPE)

class modelclass.Dash_Mixin[source]

Bases: object

This mixin wraps call the Dash dashboard

modeldash(var, **kwargs)[source]
class modelclass.Fix_Mixin[source]

Bases: object

This mixin handles a number of world bank enhancements

property wb_behavioral

returns endogeneous where the frml name contains a Z which signals a stocastic equation

property wb_ident

returns endogeneous variables not in wb_behavioral

fix(df, pat='*', start='', end='', silent=0)[source]

Fixes variables to the current values.

for variables where the equation looks like:

var = (rhs)*(1-var_d)+var_x*var_d

The values in the smpl set by start and end will be set to:

var_x = var
var_d = 1

The variables fulfilling this are elements of .self.fix_endo

Parameters:
  • df (TYPE) – Input dataframe should contain a solution and all variables ..

  • pat (TYPE, optional) – Select variables to endogenize. Defaults to ‘*’.

  • start (TYPE, optional) – start periode. Defaults to ‘’.

  • end (TYPE, optional) – end periode. Defaults to ‘’.

Returns:

the resulting daaframe .

Return type:

dataframe (TYPE)

unfix(df, pat='*', start='', end='')[source]

Unfix (endogenize) variables

Parameters:
  • df (Dataframe) – Input dataframe, should contain a solution and all variables .

  • pat (string, optional) – Select variables to endogenize. Defaults to ‘*’.

  • start (TYPE, optional) – start periode. Defaults to ‘’.

  • end (TYPE, optional) – end periode. Defaults to ‘’.

Returns:

A dratframe with all dummies for the selected variablse set to 0 .

Return type:

dataframe (TYPE)

find_fix_dummy_fixed(df=None)[source]

returns names of actiove exogenizing dummies

sets the property self. exodummy_per which defines the time over which the dummies are defined

property fix_dummy_fixed

returns names of actiove exogenizing dummies

sets the property self. exodummy_per which defines the time over which the dummies are defined

property fix_dummy_fixed_old

returns names of actiove exogenizing dummies

sets the property self. exodummy_per which defines the time over which the dummies are defined

property fix_add_factor_fixed

Returns the add factors corrosponding to the active exogenizing dummies

property fix_value_fixed

Returns the exogenizing values corrosponding to the active exogenizing dummies

property fix_endo_fixed

Returns the endogeneous variables corrosponding to the active exogenizing dummies

fix_inf(df=None)[source]

Display information regarding exogenizing

class modelclass.Stability_Mixin[source]

Bases: object

get_newton(**kwargs)[source]
get_eigenvalues(forcenum=False, silent=True, dropvar=None, progressbar=False)[source]
get_eigenvectors(forcenum=False, silent=True, dropvar=None, progressbar=True)[source]
eigenvalues_show(*args, **kwargs)[source]

Generates and displays a polar plot of eigenvalues and their corresponding eigenvectors for a selected year from a dictionary of DataFrames. Each DataFrame contains eigenvalues and eigenvectors of the companion matrix for that year. The first row of the DataFrame consists of eigenvalues, and the subsequent rows contain the corresponding eigenvectors. The user can interact with the plot via a dropdown for year selection, a slider for eigenvalue selection, and a button to toggle additional plot details. The polar plot dynamically updates to reflect the selected eigenvalue, displaying its magnitude and phase. Additional information about the selected eigenvector is also displayed, facilitating a detailed temporal analysis of the eigenvalues.

Parameters: - eig_dic (dict): A dictionary where keys are years (or time periods) and values are DataFrames

containing the first row as eigenvalues and the subsequent rows as the corresponding eigenvectors of the companion matrix for each year.

Side Effects: - Displays interactive widgets including a dropdown for year selection, a plot output area,

and a slider for selecting specific eigenvalues. Additionally, displays textual information about the selected eigenvalue and its eigenvectors.

  • Utilizes Plotly for generating the polar plot and ipywidgets for interactive controls.

  • The method defines and uses several inner functions to handle events like year change, eigenvalue selection, and other interactions.

Returns: - None. The method’s primary function is to display interactive widgets and plots within a Jupyter

notebook environment.

Note: - This method is designed for use within a Jupyter notebook as it relies on IPython.display

for rendering and ipywidgets for interactivity.

  • The actual plotting and widget setup are accomplished through several nested functions within this method, making use of closures and nonlocal variables for state management.

eigenvalues_plot(periode=None, size=(4, 3), maxfig=6, progressbar=False)[source]
get_eigen_jackknife_df(periode=None, maxnames=200000)[source]
jack_largest_reduction(jackdf, eigenvalue_row=0, periode=None, imag_only=False)[source]

Identifies the largest reduction in eigenvalue magnitude for a specified period and optionally focuses on eigenvalues with imaginary parts if imag_only is True.

Parameters: - jackdf (DataFrame): A DataFrame containing jackknife analysis results,

including eigenvalues, their real and imaginary parts, and descriptions of exclusions.

  • eigenvalue_row (int, optional): The row index of the eigenvalue to analyze. Defaults to 0, which typically represents the largest magnitude eigenvalue.

  • periode (int/str, optional): The specific period (year) to analyze. If None, the function processes the first year found in the DataFrame. Defaults to None.

  • imag_only (bool, optional): If True, only considers eigenvalues with non-zero imaginary parts for analysis. Defaults to False.

Returns: DataFrame: A sorted DataFrame with the nth largest length (eigenvalue magnitude) for each excluded variable or condition, including the year, exclusion identifier, length (magnitude of the eigenvalue), description of the exclusion, and the real and imaginary parts of the eigenvalue. The row for ‘excluded == “NONE”’ is moved to the front.

Raises: Exception: If the specified period is not found in the DataFrame’s years.

jack_largest_reduction_plot(jackdf, eigenvalue_row=0, periode=None, imag_only=False)[source]

Creates an interactive Plotly plot to visualize the reduction in eigenvalue magnitude across different exclusions, highlighting the ‘NONE’ exclusion category with a distinct color and displaying detailed information on hover. Optionally focuses on eigenvalues with imaginary parts if imag_only is True.

Parameters: - jackdf (DataFrame): A DataFrame containing the results of a jackknife analysis, including eigenvalues and their

descriptions. The DataFrame is expected to have at least the columns ‘excluded’, ‘length’, ‘excluded_description’, ‘realvalue’, and ‘imagvalue’.

  • eigenvalue_row (int, optional): Specifies the row index of the eigenvalue to analyze. Defaults to 0, which typically corresponds to the largest magnitude eigenvalue.

  • periode (int/str, optional): The specific period (year) to analyze. If None, the function processes data without filtering by period. Defaults to None.

  • imag_only (bool, optional): If True, only considers eigenvalues with non-zero imaginary parts for analysis. Defaults to False.

The function processes the input DataFrame to highlight the ‘NONE’ category in red and all other categories in blue. It then creates a Plotly FigureWidget to plot these data points as markers on a scatter plot. The y-axis tick labels are hidden to emphasize the data points rather than the categorical labels.

An HTML widget is used to display detailed information about a data point (exclusion description, length, and imaginary part of the eigenvalue) when the user hovers over it. This interactive feature provides a deeper insight into the impact of each exclusion on the eigenvalue magnitude.

Displays: - An interactive Plotly scatter plot within the Jupyter notebook. - A dynamic HTML widget that updates with detailed information about the hovered data point.

class modelclass.Report_Mixin[source]

Bases: object

table(pat='#Headline', title='Table', datatype='growth', col_desc='', custom_description={}, dec=2, heading='', mul=1.0, **kwargs)[source]

Generates a table display configuration based on specified parameters and data types, including dynamic adjustments of display options using both standard and keyword arguments.

Parameters:
  • pat (str) – Pattern or identifier used to select data for the line, defaulting to ‘#Headline’.

  • title (str) – Title of the table, passed directly to Options, defaulting to ‘Table’.

  • datatype (str) – Type of data transformation to apply (e.g., ‘growth’, ‘level’), defaulting to ‘growth’.

  • custom_description (dict) – Custom descriptions to augment or override default descriptions, empty by default.

  • dec (int) – Number of decimal places for numerical output, passed directly to Line configuration, defaulting to 2.

  • heading (str) – Optional heading line for the table, empty by default.

  • name (str) – Name for the display, defaults to ‘A_small_table’.

  • foot (str) – Footer text.

  • rename (bool) – Allows renaming of data columns

  • decorate (bool) – Decorates row descriptions based on the showtype, defaulting to False.

  • width (int) – Specifies the width for formatting output in characters, efaulting to 5.

  • chunk_size (int) – Number of columns per chunk in the display output, defaulting to 0.

  • timeslice (List[int]) – Time slice for data display, empty by default.

  • max_cols (int) – Maximum columns when displayed as a string, faulting to the system wide setting.

  • last_cols (int) – Specifies the number of last columns to include in a display slice, particularly in Latex.

  • col_desc (str) – text centered on columns

Returns:

Configured table definition object ready for rendering, which includes detailed specifications

such as units and type of transformation based on the datatype.

Return type:

DisplayVarTableDef

plot(pat='#Headline', title='', datatype='growth', custom_description={}, by_var=True, mul=1.0, ax_title_template='', **kwargs)[source]

Generates a table display configuration based on specified parameters and data types, including dynamic adjustments of display options using both standard and keyword arguments.

Parameters:

pat (str): Pattern or identifier used to select data for the line, defaulting to ‘#Headline’. datatype (str): Type of data transformation to apply (e.g., ‘growth’, ‘level’), defaulting to ‘growth’. title (str): Title of the table, passed directly to Options, defaulting to ‘Table’. custom_description (dict): Custom descriptions to augment or override default descriptions, empty by default. ncol (int):

Returns:

DisplayVarTableDef: Configured table definition object ready for rendering, which includes detailed specifications

such as units and type of transformation based on the datatype.

text(input='')[source]
report_from_spec(json_str)[source]
add_report(a_DisplayDef_list, *args)[source]
get_report(key)[source]
class modelclass.model(i_eq='', modelname='testmodel', silent=False, straight=False, funks=[], tabcomplete=True, previousbase=False, use_preorder=True, normalized=True, safeorder=False, var_description={}, model_description='', var_groups={}, reports={}, equations_latex='', eviews_dict={}, use_fbmin=True, **kwargs)[source]

Bases: Zip_Mixin, Json_Mixin, Model_help_Mixin, Solver_Mixin, Display_Mixin, Graph_Draw_Mixin, Graph_Mixin, Dekomp_Mixin, Org_model_Mixin, BaseModel, Description_Mixin, Excel_Mixin, Dash_Mixin, Modify_Mixin, Fix_Mixin, Stability_Mixin, Report_Mixin

This is the main model definition

initialize a model

class modelclass.upd(pandas_obj)[source]

Bases: object

Extend a dataframe to update variables from string

look at Model_help_Mixin.update for syntax

__call__(updates, lprint=False, scale=1.0, create=True, keep_growth=False)[source]

Call self as a function.

class modelclass.node(lev, parent, child)

Bases: tuple

A named tuple used when to drawing the logical structure. Describes an edge of the dependency graph

Lev:

Level from start

Parent:

The parent

Child:

The child

child

Alias for field number 2

lev

Alias for field number 0

parent

Alias for field number 1

modelclass.ttimer(*args, **kwargs)[source]
modelclass.create_model(navn, hist=0, name='', new=True, finished=False, xmodel=<class 'modelclass.model'>, straight=False, funks=[])[source]

Creates either a model instance or a model and a historic model from formulars.

The formulars can be in a string or in af file withe the extension .txt

if:

Navn:

The model as text or as a file with extension .txt

Name:

Name of the model

New:

If True, ! used for comments, else () can also be used. False should be avoided, only for old PCIM models.

Hist:

If True, a model with calculations of historic value is also created

Xmodel:

The model class used for creating model the model instance. Can be used to create models with model subclasses

Finished:

If True, the model exploder is not used.

Straight:

If True, the formula sequence in the model will be used.

Funks:

A list of user defined funktions used in the model

modelclass.get_a_value(df, per, var, lag=0)[source]

returns a value for row=p+lag, column = var

to take care of non additive row index

modelclass.set_a_value(df, per, var, lag=0, value=nan)[source]

Sets a value for row=p+lag, column = var

to take care of non additive row index

modelclass.insertModelVar(dataframe, model=None)[source]

Inserts all variables from model, not already in the dataframe. Model can be a list of models

modelclass.lineout(vek, pre='', w=20, d=0, pw=20, endline='\n')[source]

Utility to return formated string of vector

modelclass.dfout(df, pre='', w=2, d=0, pw=0)[source]
modelclass.upddfold(base, upd)[source]

takes two dataframes. The values from upd is inserted into base

modelclass.upddf(base, upd)[source]

takes two dataframes. The values from upd is inserted into base

modelclass.randomdf(df, row=False, col=False, same=False, ran=False, cpre='C', rpre='R')[source]

Randomize and rename the rows and columns of a dataframe, keep the values right:

Ran:

If True randomize, if False don’t randomize

Col:

The columns are renamed and randdomized

Row:

The rows are renamed and randdomized

Same:

The row and column index are renamed and randdomized the same way

Cpre:

Column name prefix

Rpre:

Row name prefix

modelclass.join_name_lag(df)[source]

creates a new dataframe where the name and lag from multiindex is joined as input a dataframe where name and lag are two levels in multiindex

modelclass.timer_old(input='test', show=True, short=False)[source]

does not catch exceptrions use model.timer

A timer context manager, implemented using a generator function. This one will report time even if an exception occurs”””

Parameters:
  • input (string, optional) – a name. The default is ‘test’.

  • show (bool, optional) – show the results. The default is True.

  • short (bool, optional) – . The default is False.

Return type:

None.