how to simulate 10000 times

stock(1) = 675.15;
delta_t = 1 / 30;
volatility = 0.2;
drift = 0.02;
for i = 2 : 30
stock(i) = stock(i-1) .* exp( volatility .* sqrt ( delta_t ) .* randn(1) + drift .* delta_t );
end
disp( stock(30) )
How do I generate 10,000 random results?

 Accepted Answer

Just wrap the for loop in another loop that runs 10,000 experiments, like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
stock(1) = 675.15;
delta_t = 1 / 30;
volatility = 0.2;
drift = 0.02;
for experiment = 1 : 10000
for i = 2 : 30
stock(i) = stock(i-1) .* exp( volatility .* sqrt ( delta_t ) .* randn(1) + drift .* delta_t );
end
lastPrice(experiment) = stock(end);
disp( stock(30) )
end
plot(lastPrice, 'b-');
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
title('Monte Carlo Experiment on Ending Stock Price', 'fontSize', fontSize);
ylabel('Ending Stock Price', 'fontSize', fontSize);
xlabel('Experiment number', 'fontSize', fontSize);

1 Comment

Thank you SO MUCH ! very helpful! I can keep going now

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Parallel Server in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!