Question

My goal is to design a re-usable engine programmed in MATLAB using MATLAB OOP. This is my first attempt to do this. My problem that I would like to resolve is the following: I have an abstract class cPayoffBase that defines the interface for an unknown type payoff. Inheriting cPayoffBase, I have a concrete class cPayoffCall which implements a call option. Now I defined another class cVanillaDerivs that takes in an user-defined payoff object and a strike price. When I pass in the user-defined object to cVanillaDerivs to calculate some quantity an exception Index exceeds matrix dimensions. surfaces. I shall provide the codes in detail.

cPayoffBase.m

classdef (Abstract) cPayoffBase < handle

    methods (Abstract)
        mfGetPayoff(argSpotPrc)
    end

end

cPayoffCall.m

classdef cPayoffCall < cPayoffBase

    properties (GetAccess = private, SetAccess = private)

        dmStrikePrc

    end

    methods

    function obj = cPayoffCall(argStrikePrc)

        obj.dmStrikePrc = argStrikePrc;

    end

    function rslt = mfGetPayoff(obj, argSpotPrc)

        rslt = max(argSpotPrc - obj.dmStrikePrc, 0.0);

    end

end

cVanillaDerivs.m

classdef cVanillaDerivs

%% Data Members
properties (GetAccess = private, SetAccess = private)
    dmPayoffObj
    dmExpiryDt
end

%% Implementation
methods

    % Constructor
    function obj = cVanillaDerivs(argPayoffObj, argExpiryDt)

        obj.dmPayoffObj = argPayoffObj;
        obj.dmExpiryDt  = argExpiryDt;

    end

    % Member Functions
    function rslt = mfGetExpriyDt(obj)

        rslt = obj.dmExpiryDt;

    end

    function rslt = mfGetDerivPayoff(argSpotPrc)

        rslt = obj.dmPayoffObj(argSpotPrc);

    end
end
end

command window

>> clear classes
>> spot = 100; strike = 50; T = 1;
>> payoffObj = cPayoffCall(strike);
>> typeVanilla = cVanillaDerivs(payoffObj, T);
>> mfGetDerivPayoff(typeVanilla, spot)
Index exceeds matrix dimensions.

Error in cVanillaDerivs/mfGetDerivPayoff (line 37)
            rslt = obj.dmPayoffObj(argSpotPrc);

In C++, given that I have a wrapper class and wrap the class object cPayoffBase I can do something like return (*dmPayoff)(dmSpotPrc) for double returning function mfGetDerivPayoff(double dmSpotPrc) const in class cVanillaDerivs. Please kindly let me know my errors and if possible how can I achieve the same process in MATLAB OOP like C++.

Was it helpful?

Solution

You are trying to access element number 100 of the property dmPayoffObj. However, this property is set to payOffObj, which isn't an array. Thus the error.

What I think you wanted is return the Payoff of dmPayoffObj. You should change the method mfGetDerivPayoff of the class cVanillaDerivs as follows:

function rslt = mfGetDerivPayoff(argSpotPrc)
    rslt = obj.dmPayoffObj.mfGetPayoff(argSpotPrc);
end

OTHER TIPS

Did you mean rslt = obj.dmPayoffObj.mfGetPayoff(argSpotPrc); instead of rslt = obj.dmPayoffObj(argSpotPrc);?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top