In [1]:
# First, we'll load some modules that we're going to need
%matplotlib inline
import pysimpactcyan
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
# Create an instance of the PySimpactCyan class, which we can then use to run simulations
simpact = pysimpactcyan.PySimpactCyan()
In [3]:
# This is a helper function that runs a simulation, analyzes the generated data
# and creates a plot from it: a point in the figure indicates a relationship,
# the X-axis is the age of the man when the relationship was formed and the Y-axis
# is the relationship of the woman
def showAges(cfg, path="/tmp/simptest", seed=-1):
ret = simpact.run(cfg, path, seed=seed)
rel = pd.read_csv(ret["logrelations"])
ppl = pd.read_csv(ret["logpersons"])
numRel = rel.shape[0]
agesMen = []
agesWomen = []
for r in range(numRel):
idm = rel.iloc[r]["ID1"]
idw = rel.iloc[r]["ID2"]
t = rel.iloc[r]["FormTime"]
tobm = float(ppl[ppl["ID"] == idm]["TOB"])
tobw = float(ppl[ppl["ID"] == idw]["TOB"])
agesMen.append(t-tobm)
agesWomen.append(t-tobw)
plt.figure(1, figsize=(6,6), dpi=80)
plt.gca().set_aspect('equal')
plt.xlabel("Age man")
plt.ylabel("Age woman")
plt.plot(agesMen, agesWomen, 'o')
plt.plot([0,100],[0,100])
In [4]:
# First, we'll run a simulation with all the default settings. This uses
# the 'simple' formation hazard with settings that do not specify any
# particular age difference.
showAges(None);
In [5]:
# In the following example, we're going to use the 'agegapry' hazard. To be able to
# use this, we also need to specify that the reference year is synchronized periodically.
# By default, the 'agegapry' hazard expects that this sync is performed at least yearly
# (the default value of 'formation.hazard.agegapry.maxageref.diff' is 1), so we'll set
# 'syncrefyear.interval' to 1 as well.
#
# In the hazard, we're not going to change the slope of the resulting distribution, but
# we're going to set the parameters that make the importance of the age gap term dependent
# on the age of the person. This way, we get a funnel-like shape of the distribution.
cfg = {}
cfg["population.simtime"] = 50
cfg["population.nummen"] = 400
cfg["population.numwomen"] = 400
cfg["syncrefyear.interval"] = 1
cfg["formation.hazard.type"] = "agegapry"
cfg["formation.hazard.agegapry.baseline"] = 0.1
cfg["formation.hazard.agegapry.gap_factor_man_const"] = 0
cfg["formation.hazard.agegapry.gap_factor_man_exp"] = -0.7
cfg["formation.hazard.agegapry.gap_factor_man_age"] = -0.06
cfg["formation.hazard.agegapry.gap_factor_woman_const"] = 0
cfg["formation.hazard.agegapry.gap_factor_woman_exp"] = -0.7
cfg["formation.hazard.agegapry.gap_factor_woman_age"] = -0.06
showAges(cfg);
In [6]:
# This is a simular example as before, but instead of getting less picky as they
# age, in this simulation people will put more importance in the age difference
# as they get older.
cfg = {}
cfg["population.simtime"] = 50
cfg["population.nummen"] = 400
cfg["population.numwomen"] = 400
cfg["syncrefyear.interval"] = 1
cfg["formation.hazard.type"] = "agegapry"
cfg["formation.hazard.agegapry.baseline"] = 0.1
cfg["formation.hazard.agegapry.gap_factor_man_const"] = 0
cfg["formation.hazard.agegapry.gap_factor_man_exp"] = -0.05
cfg["formation.hazard.agegapry.gap_factor_man_age"] = 0.03
cfg["formation.hazard.agegapry.gap_factor_woman_const"] = 0
cfg["formation.hazard.agegapry.gap_factor_woman_exp"] = -0.05
cfg["formation.hazard.agegapry.gap_factor_woman_age"] = 0.03
showAges(cfg);
In [ ]: