I'm looking for a decent implementation of Gillespie's Direct Method in Python, as if I code the algorithm myself I'm nigh positive I'll do it inefficiently.

Anyone have a favorite?


(Disclaimer: I haven't used any of the packages or code below.)

Finding any implementation of Gillespie's method in Python was a bit of a challenge; the most fruitful search terms seemed to be "tau leap" or "kinetic Monte Carlo." This blog post implements Gillespie's algorithm, though it's not clear that it's efficient. One of the commenters mentions some other Gillespie/SSA algorithm implementations in Python that solve problems out of a textbook; the relevant problems are 6.3 through 6.6 on this website.

The most promising implementations in Python that I found were StochPy and Python language bindings to COPASI. I also found what looked like promising C++ codes in SPPARKS, STOCKS, and StochKit; they're not Python, but maybe you can wrap around one of them in Python if it looks particularly promising to you.


I would warmly recommend to use StochKit, which is really extremely reliable and efficient. It implements the Gillespie's direct method as well as its most popular optimized variants. Furthermore, it has a built-in mechanism for selecting the appropriate simulation scheme depending on your network.

However, it is written in C++. If you really need to integrate it with a Python, that shouldn't be a problem. I use it extensively in MATLAB scripts (basically, MATLAB writes a descriptor of the simulation, calls StochKit, and finally performs the data analysis). I'm sure you could do pretty much the same thing with NumPy or SciPy.

  • $\begingroup$ Looks like there is a Python2/3 wrapper for StochPy now, and it seems to work very well. Here's the github $\endgroup$ – Enrico Borba Jan 29 '18 at 0:50

As part of the lab the developed and maintains StochKit. I am happy to hear that it is highly recommended in the previous answers. However, I wanted to update everyone. There already is a python wrapper for StochKit: GillesPy.

I would also recommend you checkout StochSS, This is an fully functional modeling and simulation IDE that uses StochKit and as one of it's simulations engines. It also includes spatial simulation with the python package PyURDME. Model building, and 3D visualization tools (plus cloud computing integration).


As you may already know if you're asking this question, Gillespie's method is often used in computational systems biology. There are a number of software system that provide implementations; you may want to browse the SBML Software Guide to find some of them (specifically, the ones that are known to support SBML, something that I realize may not be relevant to your needs). However, Geoff Oxberry is correct that Python-based implementations of Gillespie's algorithms seem to be uncommon compared to C++ and Java implementations. In addition to StochPy, I'm aware of the following Python-based packages (in alphabetical order):

If, in your application, you can use software that's coded in something other than Python but can be wrapped with a Python interface, then there is

  • Cain — the core is C++, but it includes a Python-based interface
  • COPASI language bindings
  • several more options as mentioned by Geoff Oxberry in his reply on this page

Finally, if you don't need your simulation to be run locally and can instead use web services, then there are the following options:

  • (the simulation service is currently off-line, but will return) SBW
  • ByoDyn's online services (see link above)

These lists are still incomplete, but hopefully they will help.


I wrote a small Python package called OOGillespie whose main feature is that it is rather flexible and embraces object-oriented programming.

Efficiency was not the main priority when writing OOGillespie, but I tried to keep things fast, where it did not lead to a major slowdown. OOGillespie will probably not be able to compete with other implementations for standard tasks, but it may be the only implementation that can handle some non-standard ones. If your bottleneck is computing transition rates from large matrices, OOGillespie may be competitive again by allowing you to use NumPy matrix operations for this purpose. Finally, depending on how experienced you are with classes, OOGillespie may cut down your programming time.

At the end of the day, I do not think that you can simply rank Gillespie implementations without knowing the specific application as a major point is what kind of events you have and how you can calculate tehm.


I have made my own simple implementation and created short tutorial, hope it will be helpfull:

i just converted the gillespie's algorithm into python code:

1.Define total simulation time T and set current time t=0; 2.Initialize the state of the system x – choose randomly or set zero state; 3.Find the sum Q of all the possible transition rates from the current state x; 4.Simulate the time Δt until the next transition by drawing from an exponential distribution with parameter Q; 5.Generate a random number r from a uniform distribution; 6.Set the next state n as follows: If 0 < r < q(x,0)/Q, choose transition 0; if q(x,0)/Q < r < (q(x,0) + q(x,1))/Q choose transition 1, and so on; 7.Update the current time: t=Δt+t; 8.Repeat 3-7 until t≤T.

The source code in python:


additionally, the result of simulation is compared with the exact result using mean squared error (MSE).



  • $\begingroup$ Would you describe the implementation? Why is it interesting? $\endgroup$ – nicoguaro Aug 25 '18 at 17:56
  • $\begingroup$ i like it because it works very accurately, and its implementation is very easy the original algorithm is taken from here: pdfs.semanticscholar.org/f70e/… $\endgroup$ – user628176 Aug 26 '18 at 12:41
  • 1
    $\begingroup$ Please describe what you have done, not just your affinity for the solution. $\endgroup$ – nicoguaro Aug 27 '18 at 15:35
  • $\begingroup$ It would be more useful to the OP, as well as any future visitors to this question, to have more than just links to code & video. Try to explain the method that you implemented in your own words and post a copy of the code here. $\endgroup$ – Paul Aug 27 '18 at 18:43
  • $\begingroup$ well i just converted the gillespie's method to python code: $\endgroup$ – user628176 Aug 28 '18 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.