A rational stochastic bubble can be simulated in MATLAB using this MATLAB code (made by Niklas Brønager, November 2010):