Shock and scenario analysis
Scenario Analysis and Shock Implementation in MARIO¶
Implementing a shock without using mario excel platform¶
A scenario can be implemented in multiple ways within mario. Simple shocks may be implemented with mario as follow:
In [1]:
from mario import load_test, slicer
In [2]:
# loading the baseline scenario (using the load_test)
test = load_test('IOT')
In [3]:
# In order to implement a scenario with respect to the baseline, we can clone the baseline database using the clone function
# and creating a new scenario
test.clone_scenario(
scenario='baseline', # from which scenario clone
name='Final demand increase',# what will be the name of the new scenario
)
In [4]:
#checking the scenarios of the model
print(test.scenarios)
['baseline', 'Final demand increase']
lets increase the local final demand of Italy by 10%
In [5]:
# using the loc function of pd.DataFrames, we can implement a change in the final demand of the new scenario
# we may use the slicer function to do it easily
Y_rows = slicer(matrix='Y',axis= 0,Region=['Italy'],Item=['Agriculture'])
Y_cols = slicer(matrix='Y',axis= 1,Region=['Italy'],Item=['Final Demand'])
In [6]:
Y_new = test.matrices['Final demand increase']['Y']
Y_new.loc[Y_rows,Y_cols]*=1.1
In [7]:
# updating the matrix using the update function to track in the metadata
test.update_scenarios(scenario='Final demand increase',Y=Y_new)
In [8]:
# as the new scenario is cloning all the data from baseline, we need to reset the new scenario to coefficients and re calculte
# all the flows
test.reset_to_coefficients(scenario='Final demand increase')
In [9]:
# now using the get_data function, we can see the change in the different matrices. For example:
delta_X = test.get_data(
matrices=['X'],
scenarios= ['Final demand increase'],
base_scenario = 'baseline', # requsting the differential results for matrix X between two scenarios,
format= 'dict',# asking the results in form of a dictionary,
indeces=False,# not intrested at looking at the units information
units=False, # not intrested at lookning database information
)
In [10]:
# looking at the changes
print(delta_X)
{'Final demand increase': {'X': production Region Level Item Italy Sector Agriculture 2961.338965 Construction 57.329630 Manufacturing 597.267115 Mining 6.332699 Services 745.379267 Transport 185.686254 RoW Sector Agriculture 121.156060 Construction 5.421589 Manufacturing 302.857308 Mining 45.949173 Services 193.241123 Transport 36.432992}}
In [11]:
# the output of the get_data function is a comprehensive data on the all the scenarios and matrices asked
print(delta_X['Final demand increase']['X'])
production Region Level Item Italy Sector Agriculture 2961.338965 Construction 57.329630 Manufacturing 597.267115 Mining 6.332699 Services 745.379267 Transport 185.686254 RoW Sector Agriculture 121.156060 Construction 5.421589 Manufacturing 302.857308 Mining 45.949173 Services 193.241123 Transport 36.432992
In [12]:
# The plot_matrix function can be used to plot the changes of the X
test.plot_matrix(
matrix='X', # plotting the X matrix
x='Region_from', # putting the origin regions on the X axis
color='Sector_from', # colors are defined tby the origin sectors
base_scenario='baseline', # printing the delta_x with respect to baseline scenario,
path = 'delta_X.html'
)