In the previous chapters, we have already discussed stochastic models using the Monte Carlo simulation. So far, we have been discussing graphs and networks, so purely from that context, a community structure can also be viewed as a graph. In such graphs, nodes often cluster together as densely connected subgraphs. In general, the probability of an edge between two such nodes is a function of the cluster to which the node belongs.
A popular choice for such a network partition is the stochastic block model. A simple definition of a stochastic block model is characterized by a scalar n. This represents the number of groups or the number of clusters and a matrix that shows the nodes and their connections. For a more rigorous mathematical definition, you can refer to a statistics book.
Among a few Python packages that support stochastic models, PyMC is one that offers Markov Chain Monte Carlo (MCMC) and three building blocks for probability models, such as stochastic, deterministic, and potential. In addition to PyMC, there is another interesting package called StochPy for Stochastic Modeling. The SSA module in particular offers convenient methods (http://stochpy.sourceforge.net/examples.html). The first example uses pymc
with a normal distribution to display a composite plot and another with an MCMC model, as shown in the following code:
import pymc as mc from pylab import rcParams # set the graph display size as 10 by 10 inches rcParams['figure.figsize'] = 12, 12 z = -1. #instead of 0 and 1, some unknown mu and std goes here: X = mc.Normal( "x", 0, 1, value = -3. ) #Here below, one can place unknowns here in place of 1, 0.4 @mc.potential def Y(x=X, z=z): return mc.lognormal_like( z-x, 1, 0.4, ) mcmc = mc.MCMC([X]) mcmc.sample(10000,500) mc.Matplot.plot(mcmc)
The example shown here is to illustrate how you can display a complex model in very few lines of code:
There are examples in PyMC for disaster_model
, and with MCMC and 50,000 simple iterations, the model display appears as follows:
from pymc.examples import disaster_model from pymc import MCMC from pylab import hist, show, rcParams rcParams['figure.figsize'] = 10, 10 M = MCMC(disaster_model) M.sample(iter=65536, burn=8000, thin=16) hist(M.trace('late_mean')[:], color='#b02a2a') show()
If we were to show the histogram plot of mean values from the model, this is one option of using PyMC:
The following code uses the stochpy
timeseries trajectory data for simulation:
import stochpy as stp smod = stp.SSA() from pylab import rcParams # set the graph display size as 10 by 10 inches rcParams['figure.figsize'] = 12, 12 smod.Model('dsmts-003-04.xml.psc') smod.DoStochSim(end=35,mode='time',trajectories=2000) smod.GetRegularGrid() smod.PlotAverageSpeciesTimeSeries()
StochPy has several convenient methods to simulate stochastic models and display the results, as shown in the following image: