Compatibility of splitapply and MertonByTimeSeries functions

1 次查看(过去 30 天)
This question is regarding MertonByTimeSeries from the Risk Management toolbox.
I have panel data (which I unfortunately cannot share) that divides a particular entity and its time series into unique groups. I would like to run MertonByTimeSeries on each of these groups, using the splitapply function.
[x1, x2, x3, x4] = splitapply(@mertonByTimeSeries, 4, T1.def, T1.rf,'numPeriods',T1.p1, T1.group);
However, the two functions seem to disagree on the parameters they expect.
Below is some source code for MertonByTimeSeries. As you can see, the "NumPeriods" parameter expects a scalar.
parser.addRequired('Equity',@(x)validateattributes(x,{'double'},{'vector','real','positive'}));
parser.addRequired('Liability',@(x)validateattributes(x,{'double'},{'vector','real','positive'}));
parser.addRequired('Rate',@(x)validateattributes(x,{'double'},{'vector','real'}));
*parser.addParameter('NumPeriods',250,@(x)validateattributes(x,{'double'},{'scalar','real','positive'}));*
However, the splitapply function expects all arguments to be vectors of equal length. Below is the error I get when running the line shown first.
Error using splitapply (line 99)
The data variables must have the same number of rows as the vector of group numbers. The group number vector has 15127 row(s), and data variable 4 has 1 row(s).
Error in mertonscript (line 17)
[x1, x2, x3, x4] = splitapply(@mertonByTimeSeries, T1.eq, T1.def, T1.rf,'numPeriods',T1.p1, T1.group);
As you can see, for the 'NumPeriods' parameter, it seems like MertonByTimeSeries is expecting a scalar, and splitapply expects a vector. Any advice would be appreciated.
EDIT: The first line of code should read:
[x1, x2, x3, x4] = splitapply(@mertonByTimeSeries, T1.eq, T1.def, T1.rf,'numPeriods',4, T1.group);
I tried editing but the changes don't seem to be applying

回答(1 个)

Alain Kuchta
Alain Kuchta 2017-4-21
You can use an anonymous function to make mertonByTimeSeries callable by splitapply. In the following example, I use an anonymous function to call a function which doesn't quite work the way splitapply expects.
First, we make a simple function to stand in place of mertonByTimeSeries which takes two vectors and a keyword argument:
function desiredFunction(multiRowDataArray, keyword, multiRowNumPeriods)
fprintf('Data Sum: %d, Keyword: %s, numPeriods Sum: %d \n', sum(multiRowDataArray), keyword, sum(multiRowNumPeriods));
end
Then we call splitapply with an anonymous function instead of a function handle to desiredFunction. The anonymous function accepts two arrays and passes these arrays along with the keyword 'numPeriods' to desiredFunction
data = [2; 2; 3];
groups = [1; 1; 2];
numPeriods = [1; 3; 7];
splitapply( ...
@(d, np) desiredFunction(d, 'numPeriods', np),...
data,...
numPeriods, ...
groups);
The output shows that the function was called correctly:
Data Sum: 4, Keyword: numPeriods, numPeriodsSum: 4
Data Sum: 3, Keyword: numPeriods, numPeriodsSum: 7
For more information about anonymous functions please refer to the following link: https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by