This document demonstrate how to use the library to define a "density dependent population process" and to compute its mean-field approximation and refined mean-field approximation
In [1]:
# To load the library
import src.rmf_tool as rmf
import importlib
importlib.reload(rmf)
# To plot the results
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
%matplotlib notebook
Suppose that we want to simulate a model composed of $N$ agents where each agent has 3 possible states : $S$ (susceptible) $I$ (infected) and $R$ (recoverd). If we denote by $x_S$, $x_I$ and $x_S$ the population of agent and assume that :
In terms of population process, we get the following transitions:
In [2]:
# This code creates an object that represents a "density dependent population process"
ddpp = rmf.DDPP()
# We then add the three transitions :
ddpp.add_transition([-1,1,0],lambda x:x[0]+2*x[0]*x[1])
ddpp.add_transition([0,-1,+1],lambda x:x[1])
ddpp.add_transition([1,0,-1],lambda x:3*x[2])
In [3]:
ddpp.set_initial_state([.3,.2,.5]) # We first need to define an initial stater
T,X = ddpp.simulate(100,time=10) # We first plot a trajectory for $N=100$
plt.plot(T,X)
T,X = ddpp.simulate(1000,time=10) # Then for $N=1000$
plt.plot(T,X,'--')
Out[3]:
In [4]:
plt.figure()
ddpp.plot_ODE_vs_simulation(N=100)
plt.figure()
ddpp.plot_ODE_vs_simulation(N=1000)
(reference to be added)
This class also contains some functions to compute the fixed point of the mean-field approximation, to compute the "refined mean-field approximation" and to compare it with simulations.
If $\pi$ is the fixed point of the ODE, and $V$ the constant calculated by the function "meanFieldExpansionSteadyState(order=1)", then we have
$$E[X^N] = \pi + \frac1N V + o(\frac1N) $$To compute these constants :
In [5]:
%time pi,V,W = ddpp.meanFieldExpansionSteadyState(order=1)
print(pi,V)
In [6]:
print(pi,'(mean-field)')
for N in [10,50,100]:
Xs,Vs = ddpp.steady_state_simulation(N=N,time=100000/N)
print(Xs,'(Simulation, N={})'.format(N))
print('+/-',Vs)
print(pi+V/N,'(refined mean-field, N={})'.format(N))
The function compare_refinedMF can be used to compare the refined mean-field "x+C/N" to the expectation of $X^N$. Note that the expectation is computed by using forward simulation up to time "time"; the value $E[X^N]$ is then the temporal average of $X^N(t)$ from t="time/2" to "time". (Hence, "time" should be manualy chosen so as to minimize the variance).
In [7]:
Xm,Xrmf,Xs,Vs = ddpp.compare_refinedMF(N=10,time=10000)
print(Xm, 'mean-field')
print(Xrmf,'Refined mean-field')
print(Xs, 'Simulation')
print(Vs,'Confidence inverval of simulation (rough estimation)')
In [8]:
n=3
T,X,V,A,XVWABCD=ddpp.meanFieldExpansionTransient(order=2,time=10)
N=10
plt.figure()
plt.plot(T,X,'-')
plt.plot(T,X+V/N,'--')
plt.plot(T,X+V/N+A/N**2,':')
plt.legend(['Mean field approx.','','','$O(1/N)$-expansion','','','$O(1/N^2)$-expansion'])
Out[8]:
In [ ]:
In [ ]:
In [ ]: