modelmanipulation module

Created on Mon Sep 02 19:41:11 2013

This module is a textprocessing module which is used to transforms a template model for a generic bank into into a unrolled and expande model which covers all banks - under control of a list feature.

The resulting model can be solved after beeing proccesed in the modelclass module.

In addition to creating a model for forecasting, the module can also create a model which calculates residulas and and variables in historic periods. This model can also be solved by the modelclass module.

@author: Ib

class modelmanipulation.oldsafesub[source]

Bases: dict

A subclass of dict. if a safesub is indexed by a nonexisting keyword it just return the keyword this alows missing keywords when substitution text inspired by Python cookbook

class modelmanipulation.safesub[source]

Bases: dict

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

  • key-<number> where key is in integer returns the numeric value of key-number

  • key+<number> where key is in integer returns the numeric value of key+number

this alows missing keywords when substitution text inspired by Python cookbook

modelmanipulation.sub(text, katalog)[source]

Substitutes keywords from dictionary by returning text.format_map(safesub(katalog)) Allows missing keywords by using safesub subclass

modelmanipulation.oldsub_frml(ibdic, text, plus='', var='', lig='', sep='\n')[source]

to repeat substitution from list

  • plus is a seperator, used for creating sums

  • var and lig Determins for which items the substitution should take place by var=abe, lig=’ko’ substitution is only performed for entries where var==’ko’

modelmanipulation.sub_frml(ibdic, text, plus='', xvar='', lig='', sep='\n')[source]

to repeat substitution from list

  • plus is a seperator, used for creating sums

  • xvar and lig Determins for which items the substitution should take place by var=abe, lig=’ko’ substitution is only performed for entries where var==’ko’

  • xvar is the variable to chek against selected in list

  • select list is a list of elements in the xvar list to be included

  • matc is the entry in select list from which to select from xvar

modelmanipulation.find_res(f)[source]

Finds the expression which calculates the residual in a formel. FRML <res=a,endo=b> x=a*b+c $

modelmanipulation.find_res_dynare(equations)[source]

equations to calculat _res formulas FRML <> x=a*b+c +x_RES $ -> FRML <> x_res =x-a*b+c $

modelmanipulation.find_res_dynare_new(equations)[source]

equations to calculat _res formulas FRML <> x=a*b+c +x_RES $ -> FRML <> x_res =x-a*b+c $ not finished to speed time up

modelmanipulation.find_hist_model(equations)[source]

takes a unrolled model and create a model which can be run for historic periode

and the identities are also calculeted

modelmanipulation.exounroll(in_equations)[source]

takes a model and makes a new model by enhancing frml’s with <exo=,j=,jr=> in their frml name.

Exo:

the value can be fixed in to a value valuename_x by setting valuename_d=1

Jled:

a additiv adjustment element is added to the frml

Jrled:

a multiplicativ adjustment element is added to the frml

modelmanipulation.tofrml(expressions, sep='\n')[source]

a function, wich adds FRML to all expressions seperated by <sep> if no start is specified the max lag will be used

modelmanipulation.dounloop(in_equations, listin=False)[source]

Expands (unrolls do loops in a model template goes trough a model template until there is no more nested do loops

modelmanipulation.find_arg(funk, streng)[source]

chops a string in 3 parts

  1. before ‘funk(’

  2. in the matching parantesis

  3. after the last matching parenthesis

modelmanipulation.sumunroll_old(in_equations, listin=False)[source]

expands all sum(list,’expression’) in a model returns a new model

modelmanipulation.lagarray_unroll(in_equations, funks=[])[source]

expands all sum(list,’expression’) in a model returns a new model

modelmanipulation.sumunroll(in_equations, listin=False)[source]

expands all sum(list,’expression’) in a model if sum(list xvar=lig,’expression’) only list elements where the condition is satisfied wil be summed

returns a new model

modelmanipulation.funkunroll(in_equations, funk='MAX', listin=False, replacefunk='')[source]

Expands all funk(list, ‘expression’) in a model.

If funk(list xvar=lig, ‘expression’) is used, only list elements where the condition is satisfied will be summed.

Parameters:
  • in_equations (str) – A string representing the model to be modified.

  • funk (str, optional) – The name of the function to be expanded. Defaults to ‘MAX’.

  • listin (list, optional) – A list of dictionaries representing the lists in the model. If not provided, the function will search the model for lists.

  • replacefunk (str, optional) – The name of the function to replace the expanded funk. If not provided, the original name will be used.

Returns:

A string representing the modified model.

Return type:

str

modelmanipulation.argunroll(in_equations, listin=False)[source]

expands all ARGEXPAND(list,’expression’) in a model returns a new model

modelmanipulation.creatematrix(in_equations, listin=False)[source]

expands all ARGEXPAND(list,’expression’) in a model returns a new model

modelmanipulation.createarray(in_equations, listin=False)[source]

expands all to_array(list) in a model returns a new model

modelmanipulation.kaedeunroll(in_equations, funks=[])[source]

unrolls a chain (kaede) expression - used in the SMEC moedel

modelmanipulation.check_syntax_frml(frml)[source]

check syntax of frml

modelmanipulation.check_syntax_udtryk(udtryk)[source]

check syntax of frml

modelmanipulation.normalize_a_frml(frml, show=False)[source]

Normalize and show a frml

modelmanipulation.nomalize_a_model(equations)[source]

a symbolic normalization is performed if there is a syntaxerror

modelmanipulation.normalize(in_equations, sym=False, funks=[])[source]

Normalize an equation with log or several variables at the left hand side, the first variable is considerd the endogeneeus

modelmanipulation.udrul_model(model, norm=True)[source]
modelmanipulation.explode(model, norm=True, sym=False, funks=[], sep='\n')[source]

prepares a model from a model template.

Returns a expanded model which is ready to solve

Eksempel: model = udrul_model(MinModel.txt)

modelmanipulation.modelprint(ind, title=' A model', udfil='', short=0)[source]

prettyprinter for a a model. :udfil: if present is output file :short: if present condences the model Can handle both model templates and models

modelmanipulation.lagone(ind, funks=[], laglead=-1)[source]

All variables in a string i s lagged one more time

modelmanipulation.lag_n(udtryk, n=1, funks=[], laglead=-1)[source]
modelmanipulation.lag_n_tup(udtryk, n=-1, funks=[])[source]

return a tuppel og lagged expressions from lag = 0 to lag = n)

modelmanipulation.pastestring(ind, post, funks=[], onlylags=False)[source]

All variable names in a in a string ind is pasted with the string post

This function can be used to awoid variable name conflict with the internal variable names in sympy.

an advanced function

modelmanipulation.stripstring(ind, post, funks=[])[source]

All variable names in a in a string is ind is stripped of the string post.

This function reverses the pastestring process

modelmanipulation.findindex(ind00)[source]

find the index variables meaning variables on the left hand side of = braced by {}

modelmanipulation.doablelist(expressions, sep='\n')[source]

create a list of tupels from expressions seperated by sep, each element in the list is a tupel (index, number og expression, the expression)

we want make group the expressions acording to index index is elements on the left of = braced by {}

modelmanipulation.dosubst(index, formular)[source]
modelmanipulation.doablekeep(formulars)[source]

takes index in the lhs and creates a do loop around the lines with same indexes on the right side you can use %0_, %1_ an so on to indicate the index, just to awoid typing to much

Also %i_ will be changed to all the indexes

modelmanipulation.doable(formulars, funks=[])[source]

takes index in the lhs and creates a do loop around the line on the right side you can use %0_, %1_ an so on to indicate the index, just to awoid typing to much

Also %i_ will be changed to all the indexes

modelmanipulation.findindex_gams(ind00)[source]
  • an equation looks like this

  • <frmlname> [index] lhs = rhs

this function find frmlname and index variables on the left hand side. meaning variables braced by {}

modelmanipulation.un_normalize_expression(frml)[source]

This function makes sure that all formulas are unnormalized. if the formula is already decorated with <endo=name> this is kept else the lhs_varriable is used in <endo=>

modelmanipulation.un_normalize_model(in_equations, funks=[])[source]

un normalize a model

modelmanipulation.un_normalize_simpel(in_equations, funks=[])[source]

un-normalize expressions delimeted by linebreaks

modelmanipulation.eksempel(ind)[source]

takes a template model as input, creates a model and a histmodel and prints the models