Simulation geometric brownian motions
5 views (last 30 days)
Show older comments
Hey I am trying to simulate wealth dynamics over time using the fact that stock price follows a geometric brownian motion and the income is also a stochastic proces. There is a problem in the code regarding matrix algebra, stating the error "Matrix dimensions must agree"
Can anyone help me solve the problem? :)
% function [F]=Wealth(F0,rate,phi,muS,volS,Y0,muY,volY,rho,T,N,n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function simulates a geometric brownian motion.
%--------------------------------------------------------------------------
% Note:
% F0: Initial wealth of the simulation.
% rate: Risk free rate
% phi: Fraction of risky assets
% muS: Growth rate of stocks.
% volS: Volatility of stocks.
% Y0: Initial income of the simulation.
% muY: Growth rate of income.
% volY: Volatility of income.
% rho: Correlation between income and equity
% N: number of simulation per path.
% n: number of simulated paths.
%--------------------------------------------------------------------------
% Each row in the output of this function is one individual simulation of
% a geometric brownian motion path.
%--------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Construct price process:
dt=T/N;
dW1=sqrt(dt)*randn(N-1,n);
dW2=sqrt(dt)*randn(N-1,n);
Y=Y0*exp(cumsum(muY-1/2*volY^2)*dt+volY*rho*dW1+sqrt(1-rho)*volY*dW2);
Y=[ones(1,n)*Y0;Y];
F=F0*exp(cumsum((rate+phi.'*(muS-rate))-1/2*phi.'^2*volS^2)*dt+phi.'*volS*dW1+Y*dt);
F=[ones(1,n)*F0;F];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
1 Comment
Star Strider
on 22 Oct 2016
We cannot run your code.
Check the size of all your matrices. Be certain they are appropriate to the mathematical operations you want to perform on them.
Also, consider using the bsxfun function. It may do the calculations you want to perform.
Answers (0)
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!