Using fmincon to optimize montecarlo

4 views (last 30 days)
Janki
Janki on 18 Jun 2014
Edited: Matt J on 18 Jun 2014
Hi All! I've written a montecarlo function for option pricing. I've pasted the code below. Now I am trying to use fmincon to find what value of s would maximize the output (avg) (so minimize -avg). i've tried reading the online help but i'm still pretty confused. can anyone point me in the right direction on how to start? i want p0 and k to be 50 and 52 respectively.
function [avg] = montecarlo(p0,k,s)
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes here
shocks = zeros (1000,20);
y = randn(1000,20);
shocks = y;
logps = zeros (1000,20);
logps(:,1)= log(p0);
for i = 2:20
for j = 1:1000
logps(j,i) = logps(j,i-1)+.09*shocks(j,i);
end
end
prices = exp(logps);
profits = zeros(1000,20);
payoffs = zeros(1000,1);
for i = 1:1000
for j = 1:20
if (prices(i,j) > s)
payoffs(i) = prices(i,j) - k;
break
else
payoffs(i) = 0;
end
end
end
avg = mean(payoffs);
end

Answers (1)

Matt J
Matt J on 18 Jun 2014
Edited: Matt J on 18 Jun 2014
Your montecarlo() function is a piecewise constant function of s, and therefore is not differentiable. fmincon is therefore the wrong tool.
Really, what you should do is pre-compute the "prices" matrix - it never changes throughout the problem - and just evaluate montecarlo() for all s in the set of values returned by
>> [unique(prices);-Inf]
and look for the minimum. Since there are at most 20000 such values, it should be an easy brute force search.
You should also simplify your computation of avg to this more efficient one-liner
avg = -k*mean(any(prices>s,2))
  2 Comments
Matt J
Matt J on 18 Jun 2014
In fact, your function is also a monotonic function of s, with a trivial minimum at
s=min(prices(:))-c; %c>0 is any positive constant
So, I wonder if there isn't a mistake in your formulation.
Matt J
Matt J on 18 Jun 2014
Edited: Matt J on 18 Jun 2014
Really, what you should do is pre-compute the "prices" matrix - it never changes throughout the problem
Another reason to remove "prices" from your function and to pre-compute it is because it is random. It is impossible to iteratively minimize a function whose definition changes randomly every time the function is evaluated.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!