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
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!