Reputation: 432
I have the following simulation running in Matlab. For a period of 25 years, it simulates "Assets", which grow according to geometric brownian motion, and "Liabilities", which grow at a fixed rate of 7% each year. At the end of the simulation, I take the ratio of Assets to Liabilities, and the trial is successful if this is greater than 90%.
All inputs are fixed except for Sigma (the standard deviation). My goal is to find the lowest possible value of sigma that will result in a ratio of assets to liabilities > 0.9 for every year.
Is there anything in Matlab designed to solve this kind of optimization problem?
The code below sets up the simulation for a fixed value of sigma.
%set up inputs
nPeriods = 25;
years = 2016:(2016+nPeriods);
rate = Assumptions.Returns;
sigma = 0.15; %This is the input that I want to optimize
dt = 1;
T = nPeriods*dt;
nTrials = 500;
StartAsset = 81.2419;
%calculate fixed liabilities
StartLiab = 86.9590;
Liabilities = zeros(size(years))'
Liabilities(1) = StartLiab
for idx = 2:length(years)
Liabilities(idx) = Liabilities(idx-1)*(1 + Assumptions.Discount)
end
%run simulation
obj = gbm(rate,sigma,'StartState',StartAsset);
%rng(1,'twister');
[X1,T] = simulate(obj,nPeriods,'DeltaTime',dt, 'nTrials', nTrials);
Ratio = zeros(size(X1))
for i = 1:nTrials
Ratio(:,:,i)= X1(:,:,i)./Liabilities;
end
Unsuccessful = Ratio < 0.9
UnsuccessfulCount = sum(sum(Unsuccessful))
Upvotes: 1
Views: 147
Reputation: 28826
First make your simulation a function that takes sigma
as the input:
function f = asset(sigma)
%set up inputs
nPeriods = 25;
years = 2016:(2016+nPeriods);
rate = Assumptions.Returns;
%sigma = %##.##; %This is the input of the function that I want to optimize
dt = 1;
T = nPeriods*dt;
nTrials = 500;
StartAsset = 81.2419;
%calculate fixed liabilities
StartLiab = 86.9590;
Liabilities = zeros(size(years))'
Liabilities(1) = StartLiab
for idx = 2:length(years)
Liabilities(idx) = Liabilities(idx-1)*(1 + Assumptions.Discount)
end
%run simulation
obj = gbm(rate,sigma,'StartState',StartAsset);
%rng(1,'twister');
[X1,T] = simulate(obj,nPeriods,'DeltaTime',dt, 'nTrials', nTrials);
Ratio = zeros(size(X1))
for i = 1:nTrials
Ratio(:,:,i)= X1(:,:,i)./Liabilities;
end
Unsuccessful = Ratio < 0.9
UnsuccessfulCount = sum(sum(Unsuccessful))
f = sigma + UnsuccessfulCount
end
Then you can use fminbnd
(or fminsearch
for multiple inputs) to find the minimized value of sigma.
Sigma1 = 0.001;
Sigma2 = 0.999;
optSigma = fminbnd(asset,Sigma1,Sigma2)
Upvotes: 1