How to plot Wiener process for stock prices

Hi,
I want to plot a Wiener process for a stock price. I want the Random Walk to start from the first point of the series. Then I want to compare the progress with the Original series.
My code is:
if true
0 + sqrt(var(price)) * randn(length(price)) ; % set the state of randn T =length(price) ; N = length(price); dt = T/N; dW = zeros(1,N); % preallocate arrays ... W = zeros(1,N); % for efficiency
dW(1) = price(1,1); % first approximation outside the loop ... W(1) = dW(1); % since W(0) = 0 is not allowed for j = 2:N dW(j) = sqrt(var(price))* (0 + sqrt(var(price)) * randn(1)); % general increment W(j) = W(j-1) + dW(j); end
plot([0:dt:T],[0,W],'r-') % plot W against t xlabel('t','FontSize',16) ylabel('W(t)','FontSize',16,'Rotation',0) end
Unfortunately the price goes into negative which is not possible. I think this happens because of the variance set-up. What is wrong with the code?
Thanks a lot in advance
Danyo

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFinancial Toolbox についてさらに検索

質問済み:

2013 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by