# Free C++ Matlab VBA Python code for derivatives pricing

#### bordin

This is amazing!!! Thank you very much Andy

#### Karren

Hi, I'm new to MATLAB as well as to this forum. I'm currently doing a research on pricing convertible bonds using Longstaff and Schwartz (2011) model or better known as the LSM. I've managed to determine the model price for the convertible bond by minor adjustment to the Matlab code provided by this link http://www.volopta.com/About.html.(I'm really thankful for this).

Then, I calculate the pricing error by comparing the model price with the observed price of the convertible bond for that particular date.

Nonetheless, I can only determine the model price for one data point, for example at the issuance date. I would need to repeat the pricing process for multiple data points, say for example the model price one day after the issuance, two days after the issuance date and so on..For every data point, the Spot Price for the underlying equity (Spot) would be different and the number of time increments (T) would also be reduced by one. Briefly, I can only figure out these but I don't know how I should modify the Matlab code so that it can repeat the pricing process accordingly instead of changing the Spot Price and T manually.
Thanks, very much.

#### Yike Lu

##### Finder of biased coins.
The simplest solution is to put everything into a for loop which accesses spot and expiry lists that you initialize outside of the loop.

#### Karren

Thanks, Yike for the suggestion. I'll try it out. Hope I'll get it right.

#### Karren

Hi there,

I would need some help here. I have tried out Yike's suggestion but still I failed to get the desired outputs. Followings are some of the code that I'm currently trying out. I would like to simulate the underlying stock price for a convertible bond for 12 different spot price that is a column vector.

6.35 6.38 6.40 6.42 6.41 6.44 6.43 6.45 6.38 6.37 6.36 6.36 (save under the name 'data')

function SPaths = GenPaths(S0, mu, sigma, T, NSteps, NRepl)
S0 = data(1,; % Spot Price.
mu = -0.6883; % expected stock return.
sigma = 0.6569; % volatility.
T = 1; % Maturity in year.
NSteps = 12; % Number of time increments
NRepl = 50; % Number of simulated paths
dt = T/NSteps; % Time increments
for S0 = data(1,
nudt = (mu-0.5*sigma^2)*dt;
sidt = sigma*sqrt(dt);
RandMat = randn(round(NRepl/2), NSteps);
Increments = [nudt + sidt*RandMat ; nudt - sidt*RandMat];
LogPaths = cumsum([log(S0)*ones(NRepl,1) , Increments] , 2);
SPaths = exp(LogPaths);
end

When I ran the code, the output only show simulated price when the S0 = 6.35 (ie the first Spot Price). it did not repeat the simulation for different S0.

2nd question: I also have no idea how I should modify the NSteps. It should reduce by 1 each time I rerun the simulation with different spot price.

Kindly help.

Thank,
Karren

#### cgorac

I'm not sure I understand properly what you're trying to do, but you may wish to organize your Matlab code so that you have script GenPaths.m with GenPaths() function definition inside:
Code:
function SPaths = GenPaths(S0, mu, sigma, T, NSteps, NRepl)
dt = T / NSteps; % Time increments
nudt = (mu - 0.5 * sigma^2) * dt;
sidt = sigma * sqrt(dt);
RandMat = randn(round(NRepl / 2), NSteps);
Increments = [nudt + sidt * RandMat; nudt - sidt * RandMat];
LogPaths = cumsum([log(S0) * ones(NRepl,1) , Increments], 2);
SPaths = exp(LogPaths);
end
and then another file, say GenPathsTest.m containing test script, with double loop over spot prices and number of times increment:
Code:
data = [6.35 6.38 6.40 6.42 6.41 6.44 6.43 6.45 6.38 6.37 6.36 6.36];

mu = -0.6883; % expected stock return.
sigma = 0.6569; % volatility.
T = 1; % Maturity in year.
NSteps = 12; % Number of time increments
NRepl = 50; % Number of simulated paths

for k = 1:length(data)
S0 = data(k); % Spot Price.
for l = NSteps:-1:1
SPaths = GenPaths(S0, mu, sigma, T, l, NRepl);
% here, do something with SPaths
end
end

But overall, I think you really should learn a bit about Matlab before proceeding with this work...

#### Karren

Thanks very much to Cgorac. It helps a lot. Really appreciate it.

#### Kieluke

Hey Andy, table of contents looks great but apparently the site is off. Does anyone has a copy of the code or comparable code? That would be great! Thanks a lot in advance!

#### Andy Nguyen

The site is backup but Heston code has been removed and will be included in the author's upcoming book.
The Heston Model and its Extensions in Matlab and C#

#### Shanil

The site is backup but Heston code has been removed and will be included in the author's upcoming book.
The Heston Model and its Extensions in Matlab and C#
Seems the page is not hosted anymore - would you know where I could get the code? Thanks in advance!!

#### Daniel Duffy

##### C++ author, trainer
Thanks Daniel - Did you mean the memorial page has a link to the files? I see "Rouah links" on the memorial page but link does not work. Would you happen to have the zipped files?
I was more mentoning RIP.

Maybe the publisher has code on its site?

Replies
30
Views
2K
Replies
0
Views
780
Replies
9
Views
3K
Replies
3
Views
4K
Replies
5
Views
4K