在Matlab中寻找模拟的最佳输入

时间:2017-05-03 14:29:36

标签: matlab optimization simulation

我在Matlab中运行了以下模拟。在25年的时间里,它模拟"资产"它们根据几何布朗运动而增长,以及"负债",它们以每年7%的固定比率增长。在模拟结束时,我采用资产与负债的比率,如果这大于90%,则试验成功。

除Sigma(标准偏差)外,所有输入均已固定。我的目标是找到最低可能的sigma值,这将导致资产与负债的比率>每年0.9。

Matlab中有什么东西可以解决这种优化问题吗?

以下代码为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))

1 个答案:

答案 0 :(得分:1)

首先让你的模拟成为一个以sigma为输入的函数:

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

然后您可以使用fminbnd(或多个输入fminsearch)来查找西格玛的最小值。

Sigma1 = 0.001;
Sigma2 = 0.999;
optSigma = fminbnd(asset,Sigma1,Sigma2)