Discrete distribution example

In this example, we're going to illustrate how discrete data can be used as a 2D distribution. To illustrate this, we're going to take samples from this 2D distribution to use as the geographic location of the people in the simulation. We'll then make a histogram of these locations to show that we've indeed been sampling from the specified discrete distribution.


In [1]:
%matplotlib inline
import pysimpactcyan
import pandas as pd
import matplotlib.pyplot as plt

In [2]:
simpact = pysimpactcyan.PySimpactCyan()


Setting data directory to /usr/local/share/simpact-cyan/

In [3]:
cfg = { }
cfg["population.nummen"] = 100000
cfg["population.numwomen"] = 100000
cfg["population.maxevents"] = 1 # We don't need an actual simulation, just the 2D locations
cfg["population.eyecap.fraction"] = 0 # Don't initialize formation events (would not even be possible for this many people)

# We're going to sample the geographic location (which is not currently used for anything)
# from a discrete distribution, based on data in a tiff file
cfg["person.geo.dist2d.type"] = "discrete"
cfg["person.geo.dist2d.discrete.densfile"] = "/tmp/simptest/up32.tiff"
cfg["person.geo.dist2d.discrete.maskfile"] = "" # This option must be set, but we leave it blank to disable the mask
cfg["person.geo.dist2d.discrete.width"] = 320
cfg["person.geo.dist2d.discrete.height"] = 240

In [4]:
# The data in 'up32.tiff' corresponds to the following image
plt.imshow(plt.imread("up.png"));



In [5]:
res = simpact.run(cfg, "/tmp/simptest")


Using identifier 'simpact-cyan-2015-08-10-13-23-02_26158_TnTwb89U-'
Results will be stored in directory '/tmp/simptest'
Running simpact executable...
Done.

# read seed from /dev/urandom
# Using seed 842668060
# Performing extra check on read configuration parameters
# WARNING: ignoring consistency check for config key person.geo.dist2d.discrete.densfile (config value is '/tmp/simptest/up32.tiff')
# WARNING: ignoring consistency check for config key person.geo.dist2d.discrete.maskfile (config value is '')
# WARNING: ignoring consistency check for config key population.agedistfile (config value is '/usr/local/share/simpact-cyan/sa_2003.csv')
# mNRM: using advanced algorithm
# Release version
# Simpact version is: 0.19.0
# Number of events executed is 1
# Started with 200000 people, ending with 200000 (difference is 0)

In [6]:
# We're going to get the information from the persons log, in which the X and Y
# coordinate for each person will be saved
persons = pd.read_csv(res["logpersons"])

In [7]:
# Let's make a histogram of these coordinates, which should show that we're
# sampling from the discrete distribution that corresponds to the image shown
# above
plt.hist2d(persons["XCoord"], persons["YCoord"],bins=100,range=[[0,320],[0,240]]);



In [8]:
# As explained in the Simpact Cyan documentation, the default value of the following
# parameter is 'yes', to account for a difference in Y-axis orientation between images
# and regular plots. To illustrate what would happen otherwise, we'll set it to 'no'
# in the next test
cfg["person.geo.dist2d.discrete.flipy"] = "no"

# Again run the simulation and plot the histogram
res = simpact.run(cfg, "/tmp/simptest")
persons = pd.read_csv(res["logpersons"])
plt.hist2d(persons["XCoord"], persons["YCoord"],bins=100,range=[[0,320],[0,240]]);


Using identifier 'simpact-cyan-2015-08-10-13-23-08_26158_8Ug91KHn-'
Results will be stored in directory '/tmp/simptest'
Running simpact executable...
Done.

# read seed from /dev/urandom
# Using seed 1858299704
# Performing extra check on read configuration parameters
# WARNING: ignoring consistency check for config key person.geo.dist2d.discrete.densfile (config value is '/tmp/simptest/up32.tiff')
# WARNING: ignoring consistency check for config key person.geo.dist2d.discrete.maskfile (config value is '')
# WARNING: ignoring consistency check for config key population.agedistfile (config value is '/usr/local/share/simpact-cyan/sa_2003.csv')
# mNRM: using advanced algorithm
# Release version
# Simpact version is: 0.19.0
# Number of events executed is 1
# Started with 200000 people, ending with 200000 (difference is 0)

In [ ]: