• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Repeat simulations

Joined
10/11/11
Messages
6
Points
11
Hi,

I need to repeat many times of MATLAB simulations by changing two parameters. Are there some ways / methods/ suggestions in which MATLAB can read the values of the two parameters from an excel file and repeat the simulations automatically. Thus, I can leave the simulations to run overnights without being required to change the values of the two parameters each time the simulation ends.
Actually I'm working on determining the value of convertible bonds. The first part of the valuation is to simulate the underlying stock price with changing volatility using the CEV model. Once I got the simulated stock price, I've to calculate the conversion value and determine the in-the-money path to finally determine the convertible bond value using the least squares Monte Carlo (LSM) model of Longstaff and Schwartz (2001). My major problem is the how can I allow the simulation to repeat with different stock price and nsteps (ie. the first part of the valuation process).

Attached are the matlab code and the values of the two parameters. Each time the simulation ends, the value of S0 (Stock Price) and NSteps have to be changed. The attached Matlab code is a complete code of my model.

Kindly advise. Many thanks

 

Attachments

  • matlabcode.doc
    26.5 KB · Views: 47
  • parameters.xls
    110.5 KB · Views: 26
xls files are hard to read, but you can convert to csv or tsv and read files in that way. I haven't used MATLAB in a few years, but I'm pretty sure it has some built-in functions for reading those files.
 
xls files are hard to read, but you can convert to csv or tsv and read files in that way. I haven't used MATLAB in a few years, but I'm pretty sure it has some built-in functions for reading those files.

Create a dummy matrix in MATLAB:
parameters = [];

Open the excel sheet, select all the values, open 'parameters' from the variable editor and paste the values there. Of course, when dealing with mixed data types & large data, this will cause issues. There are a lot of other ways to approach this as well, but for the problem in hand, it should work just fine.
 
Look up xlsread :) You can read from an excel file, specific sheet, specific range.
 
Thanks for all the suggestions. Really appreciate them. Now I'm able to read the parameters from excel using "xlsread".

Next, I tried to repeat the function (Gen_SP_risk_neutral) as attached with changing parameters (NSteps and S0) each time a simulation ends. I attached a simplified excel file.

I tried by creating "for" loop, but it doesn't seem to work. Can you please guide me further on this?
 

Attachments

  • parameters.csv
    71 bytes · Views: 13
  • Gen_SP_risk_neutral.docx
    13.3 KB · Views: 19
C++:
function [ SP_risk_neutral ] = Gen_SP_risk_neutral(S0, r, d, sigma, T, NSteps,NRepl)
%Generate stock price using risk-neutral approach.
dt = T/NSteps;
dBt = sqrt(dt)*randn(NRepl,NSteps); %Brownian motion.
SP_risk_neutral = zeros(NRepl,NSteps); %Initialise matrix.
SP_risk_neutral(:,1) = S0*ones(NRepl,1); % Vector of initial stock price.
 
for t = 1:NSteps;
    SP_risk_neutral(:,t+1) = SP_risk_neutral(:,t).*exp((r-d-.5*sigma^2)*dt +sigma*dBt(:,t));
end
end
 
==========================================================================
 
T = NSteps/360;        % time to maturity
r = 0.0471;        %drift rate
sigma = .3972;        % volatility
d = 0.015;        % dividend yield
NRepl = 1000;        % Number of replications/simulations
 
============================================================================
NSteps  and S0 change each time a simulation ends.

remove the semi colon at the end of for t = 1:NSteps;
It should be for t=1:Nsteps
 
Back
Top