## Matlab program files for Stochastic Differential Equations

#### General

Matlab introduction contains step by step directions to get started with Matlab.
all.zip contains all Matlab program files listed here.

randomwalks.zip contains versions of some programs converted to work with SciLab. The conversion was done by Nate Iverson. You can download SciLab for free at www.scilab.org. Click on downloads, and for Windows, on the installer for binary version.

#### Random walks

walk(N); displays outcomes of the N-step random walk. Output
walkcount counts the number of N-step paths which have a given property.
scaledwalk(N); scales an N-step random walk into time [0,1]. Output
scaledwalks shows rescaled walks with 50, 100, 200, 1000, 5000, and 25000 steps. The same path is used in every plot. Output
scaled_general_walks shows rescaled walks of various types with 50, 100, 200, 1000, 5000, and 25000 steps. The step sizes can be +n/-1 with probabilities chosen to make the mean step be zero. The same path is used in every plot. Output with -1 / 10 valued steps. Output with normally distributed steps.
scaled2dwalk(N); scales two N-step random walks into time [0,1] and plots them as a two-dimensional trajectory. Output
scaled3dwalk(N); scales three N-step random walks into time [0,1] and plots them as a three-dimensional trajectory. Output

#### Ordinary differential equations

ode.m shows solutions and flow field for y' = -y (exponential decay) and for y' = y - y2 (logistic model). Output
ode_animate.m animates several solutions to the exponential decay equation.
ode_euler.m uses the Euler method to solve an ODE numerically Output

#### Stochastic differential equations

ode_euler_noise.m compares solutions of the exponential decay equation with noise sigma for 5, 10, 100, 1000, 10000 steps. The equation is dx(t)=-x(t)dt+sigma*dW(t) (Langevin equation). Output with sigma = 0. Output with sigma = 0.2. Output with sigma = 0.4.
langevin.m shows outcomes of exponential decay with noise dx(t)=-x(t)dt+sigma*dB(t) (Langevin equation). Output
add_noise.m shows the effect of adding noise to exponential decay in different ways. Output
ehrenfest.m simulates the number of white balls in an Ehrenfest urn scheme.
sde_sine.m generates outcomes of solutions of the SDE dx(t)=sin(x(t))+sigma*dB(t). Output

#### Kolmogorov's equations

kolm_backward.m solves Kolmogorov's backward equation, which finds the expected value of a given function as a function of time and starting location.
kolm_forward.m solves Kolmogorov's forward equation, which finds the density of the solution of a stochastic differential equation.
make_A1.m make_A2.mmake_A3.m are needed for the previous two programs.

wiener_forward.m sets up Kolmogorov's forward equation for the Wiener process.
langevin_forward.m sets up Kolmogorov's forward equation for Langevin's equation dX(t) = -X(t)dt + sigma * dB(t) with X(0)=0.
sine_forward.m sets up Kolmogorov's forward equation for the SDE dX(t) = sin(X(t))dt + sigma * dB(t) with X(0)=0.

wiener_backward.m sets up Kolmogorov's backward equation for the Wiener process.
sine_backward.m sets up Kolmogorov's backward equation for the SDE dX(t) = sin(X(t))dt + sigma * dB(t) with X(0)=0.

#### Mathematical finance and option pricing

market.m generates realizations of bond and stock prices and plots the density of the stock distribution over time. Output
utility.m shows the expected utility that results from holding a constant fraction of your wealth in a stock, as described in Section 24. Then you can decide what fraction you want to put in the stock. Output
two_gambles.m displays the distribution of gain/loss under two gambles, the first being buying and holding a stock, the second being buying an option to purchase the stock at a particular price. Output
wealth(p,t,c,r,sigma) returns the wealth of a portfolio being managed to match the value of an option with strike price c when the current stock price is p and the time remaining is t. The stock volatility is sigma and the risk-free interest rate is r.
num_shares(p,t,c,r,sigma) returns the number of shares of stock held in the portfolio described above.
option_price.m displays the price of an option as a function of the strike price and remaining time until expiration. Output
black_scholes.m simulates the wealth of the Black-Scholes portfolio and plots it in 3d on the surface of the graph of u. Output
portfolio.m generates bond and stock prices and uses the Black-Scholes portfolio that is equivalent to holding an option to buy the stock. Output
portfolio2.m generates bond and stock prices and simulates the Black-Scholes portfolio that is equivalent to holding an option to buy the stock

#### Predator-prey models

pp1_euler.m uses the Euler scheme to numerically solve a predator-prey model (called model #1). The model stabilizes to (20,1000).
pp1_noise.m uses the Euler scheme to numerically simulate model #1 with noise.
pp2_euler.m uses the Euler scheme to numerically solve a predator-prey model (called model #2). The model cycles around (20,1000), slowly spinning outward, an artefact of the Euler method.
pp2_animate.m animates predator and prey population sizes in predator-prey model #2.
pp2_flow_field.m shows the two-dimensional flow field for a simple predator-prey model. Output
pp_simple_animate.m animates a simple predator-prey model with noise.
pp_logistic_animate.m animates a logistic predator-prey model with noise.
pp_logistic_noise.m shows the effect of noise on a logistic predator-prey model. Output