Simulation
Downloads
Today’s work is in: matlab_lec03.m
Datasets we need today: data_msft.m
Histograms: hist()
>>X=[2*ones(3,1); 3*ones(5,1); 7*ones(4,1)];>>subplot(2,1,1); >>hist(X); %draws histogram of X>>subplot(2,1,2); >>hist(X,[0:.25:10]); %draws histogram of X %on the interval [0 10] with bins of size .25 Default is a histogram with 20 bins,
from min(X) to max(X) hist(X,n) will keep default min and max
but make n bins
Uniform rv: rand()
A uniform random variable (rv) has equal probability of occurring at any point on its support
>>T=1000;>>X=rand(T,1); %creates a matrix of size %(Tx1) of uniform rv’s on (0,1)>>a=5; b=50;>>Y=a+b*rand(T,1); %creates a matrix of %size (Tx1) of unifrom rv’s on (a,a+b)
Using hist() and rand()
>>subplot(3,1,1);>>hist(X,[-.25:.025:1.25]); %draws %histogram of X>>subplot(3,1,2); >>hist(Y,[.9*a:(b/30):1.1*(a+b)]); %draw>>T=100000; Z=a+b*rand(T,1); >>subplot(3,1,3);>>hist(Z,[.9*a:(b/30):1.1*(a+b)]); subplot;
Law of Large Numbers (LLN)
Note that E[X]=.5>>X=rand(5,1); disp(mean(X));>>X=rand(10,1); disp(mean(X));>>clear Y; for i=1:200; Y(i)=mean(rand(i,1)); end;>>plot(Y); How quickly does Y tend to .5? CLT will tell us
Discrete rv’s
Oftentimes you will need to simulate rv’s with a small number of possible outcomes
You can use the uniform rv to create discrete rv’s (ie coinflips)
%x=1 with probability p and 0 with (1-p)>>p=.25; if rand(1,1)<p; x=1; else; x=0; end;%x=3 with p=.25, 2 with p=.5, 1 with p=.25>>y=rand(20,1); >>x=3.*(y>.75)+2*(y<=.75 & y>.25)+1*(y<=.25);>>hist(x,[0:.25:4]);
Central Limit Theorem (CLT)
Tells us that means of many rv’s converge to a normal rv
This is why normals are so common in nature!
Let x=uniform rv Let y=0 if x<.3 and 1 if x>=.3 Let z be the mean of j binomial rv’s Note that z itself is a rv, in particular,
when j=1, y=z
CLT (cont’d)
Think of y as a biased coin flip Think of z as the mean of j coinflips>>A=[1 5 10 25 50 100]; for k=1:6; j=A(k); for i=1:5000; x=rand(j,1); y=(x<.3)*0+(x>=.3)*1; z(i,k)=mean(y); end; end;
CLT (con’d)
>>for k=1:6; subplot(3,2,k); hist(z(:,k),50); end; subplot; We will now have 6 plots. Each will have a
histogram of rv z, which is a mean of j binomial rv’s y.
What do distributions with high j look like? Ever wonder why distribution of human
heights looks like a normal rv?
Normal rv:randn()
Works same as uniform, but produces a normal of mean 0, standard deviation 1
>>X=randn(10000,1);>>subplot(2,1,1); hist(X,50); To produce normal rv’s with different
mean or variance, just skew and shift>>m=1.1; s=.16; X=m+s*randn(10000,1);>>subplot(2,1,2); hist(X,50);
A simple security process
R(t)=mu+sigma*x(t) (x is normal, R is normal)
10% annual return and 30% annual standard deviation are quite typical for equity
>>T=10000; mu=1.1; sigma=.3; >>x=randn(T,1);>>R=mu+sigma*x;>>subplot; hist(R,50); Do you notice anything “strange” about
this process or the histogram?
A better process
R=exp(mu+sigma*X(t)) (R is log-normal)
exp(x) is approximately 1+x, so if want mean of process approximately 1.1, you need x to be approximately .1
Can this return be negative?
Calibration Issue: Jensen’s Inequality
Jensen’s Inequality: E[f(X)] ≠ f(E[X]) Stein’s Lemma:
E[exp(X)]=exp(m+.5*s2) where X is normal rv with mean m, standard deviation s
If you want R to have mean exp(m), than make sure rv X inside of exponent has mean m-.5*s2
With non-normal processes (ie jumps), things will be more complicated
Calibration Issue: Interval length
The “right” way to simulate is:
X(t) is N(0,1) dt=1/T where T is the number simulations per period m is the mean per period, σ is the standard deviation
per period For example, if one period is one year and we are
simulating monthly, than T=12, m is the annual mean (ie 10%), σ is the annual standard dev (ie 20%)
When the length of the period (over which we measure parameters) is equal to the simulation period, than T=1 and this reduces to what we saw earlier
dttXdtmtR **)(*exp)(
Calibrate Microsoft
Get daily microsoft data from CRSP or course website
>>data_msft;>>disp(mean(msft(:,4)));>>disp(std(msft(:,4)));%Microsoft returns have a daily mean of%.097% and standard deviation of 2.21%>>subplot(2,1,1); hist(msft(:,4),[-.2:.01:.2]);>>axis([-.2 .2 0 800]); xlabel('Actual');
Simulate Microsoft
>>T=3022; >>mu=.00097-.5*.0221^2; sigma=.0221;>>x=randn(T,1);>>r=exp(mu+sigma*x)-1; >>subplot(2,1,2);>>hist(r,[-.2:.01:.2]); axis([-.2 .2 0 800]);>>disp(mean(r)); disp(std(r));>>disp([skewness(r) skewness(msft(:,4))]);>>disp([kurtosis(r) kurtosis(msft(:,4))]);
Compare simulated to actual
Mean, standard deviation, skewness match well
Kurtosis (extreme events) does not match well
Actual has much more mass in the tails (fat tails)
This is extremely important for option pricing!
CLT fails when tails are “too” fat
Next Week: Modelling Volatility
How to make tales fatter? Add jumps to log normal distribution to
make tails fatter Jumps also help with modeling default Make volatility predictable: Stochastic volatility, governed by state
variable ARCH process (2003 Nobel prize, Rob
Engle)
Optional Homework (1)
Create a function that will simulate microsoft stock using a log-normal process
The function should take in arguments mu (mean), sigma (standard deviation), and T (number of days)
Its output should be a vector of daily returns