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?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 28 Okt. 2013

0 Stimmen

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 Kommentar

Tiancong Sui
Tiancong Sui am 28 Okt. 2013
Thank you SO MUCH ! very helpful! I can keep going now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by