使用结构matlab代码

时间:2015-04-25 17:59:17

标签: matlab structure

我最近一直在Matlab工作,所以有问题。请看代码。如果amountOfOptions = 2,我应该得到OptionPrice = [x x]。矢量大小OptionPrice(1x2)。看来他收到了它产生的输出,但是它给出了作业的最后结果。

function [startStockPrice, strike, barrier, riskFreeRate, timeToExpiry, volatility, CallOrPut, UpOrDown, OptionPrice, time] = OutBarrierOption(amountOfOptions)
    %%%%%%%%%% Option parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    startStockPrice   = 70 + 40 * rand(1,amountOfOptions);      % Stock price starts at $100.
    strike = 120 + 30 * rand(1,amountOfOptions);                 % Strike price for the option ($).
    barrier = 300 + 300 * rand(1,amountOfOptions);              % Barrier price for the option ($).
    riskFreeRate = 0.05 + 0.1 * rand(1,amountOfOptions);        % 0.5 annual percent.
    timeToExpiry = fix(1 + 3 * rand(1, amountOfOptions));       % Lifetime of the option in years. (Time to maturity)
    volatility   = 0.35 + 0.3 * rand(1,amountOfOptions);        % 20% annual volatility.                                          % UpOrDown - 'U' or 'D'
    M = 1e4; % Number of Monte-Carlo trials
    N = 100; % Number of observations
    OptionPrice = 0;
    tic;
    for k = 1:amountOfOptions
        dt(k)=timeToExpiry(k)/N;
        for i=1:M
            S(1) = startStockPrice(k)*exp((riskFreeRate(k)-(volatility(k)*volatility(k)/2)*dt(k)) + volatility(k)*sqrt(dt(k))*randn);
            final_vals=[S(1)];
            for j=1:N-1;
                S(j + 1) = S(j)* exp((riskFreeRate(k) - 0.5* volatility(k)*volatility(k))*dt(k) + volatility(k)* sqrt(dt(k))* randn);
                final_vals=S(1:j+1);
            end
        end
        if max(final_vals) <= barrier(k)
            option_values=max(final_vals - strike(k),0); % Evaluate the Call option options
            present_vals = exp(-riskFreeRate(k)*timeToExpiry(k))*option_values; % Discount under r-n assumption
            OptionPrice = mean(present_vals); % Take the average
        else
            % Hit the barrier, so the option is withdrawn.
            present_vals = 0;
            OptionPrice = mean(present_vals);
        end

    end
    time = toc;
    end

0 个答案:

没有答案