- Modify the Validation Step: You can create a local copy of the bnlssm implementation and modify the validation step to handle cell arrays. This approach allows you to customize the function according to your needs but comes with the responsibility of maintaining your custom code.
- Alternative Approach: You could potentially reshape your problem or the way you define A to fit within the constraints of the current bnlssm implementation. For example, you could use a single function handle that internally handles the time variation, although this might require a more complex function definition.
Bayesian Non-linear SSM with time varying coefficients
3 次查看(过去 30 天)
显示 更早的评论
I have question about using 'bnlssm' object when the problem involves time-varying coefs. Per this documentation https://www.mathworks.com/help/econ/bnlssm.html , bnlssm allows the transition matrix to be a nonlinear function of the past values of the state vector, while also being time-varying --, i.e. A_t(x_{t-1}). However the documentation contains an example of the parameter mapping function for a fixed coef. case only:
function [A,B,C,D,Mean0,Cov0,StateType] = paramMap(theta)
A = @(x)blkdiag([theta(1) theta(2); 0 1],[theta(3) theta(4); 0 1])*x;
B = [theta(5) 0; 0 0; 0 theta(6); 0 0];
C = @(x)log(exp(x(1)-theta(2)/(1-theta(1))) + ...
exp(x(3)-theta(4)/(1-theta(3))));
D = theta(7);
Mean0 = [theta(2)/(1-theta(1)); 1; theta(4)/(1-theta(3)); 1];
Cov0 = diag([theta(5)^2/(1-theta(1)^2) 0 theta(6)^2/(1-theta(3)^2) 0]);
StateType = [0; 1; 0; 1]; % Stationary state and constant 1 processes
end
Here, A and C are recognised as function handles and show the dimension of 1-by-1 when [A,B,C,D]=paramMap(theta) is run with a given theta. This does not prevent filter/smooth from returning the states estimates, as the function handles become m-by-m matices when the function handles are evaluated.
However, whenever I try replaceing A with a cell array, e.g.:
A=cell(T,1);
for t=1:T
A{t} = @(x)blkdiag([theta(1) theta(2); 0 1],[theta(3) theta(4); 0 1])*x;
end
bnlssm.fixcoeff throws an error that the dimensions of Mean0 are expected to be 1, per validation step in lines 700-707 of 'bnlssm.m':
if iscellA; At = A{1};
else; At = A; end
numStates0 = size(At,2);
if
isscalar(mean0)
mean0 = mean0(ones(numStates0,1),:);
elseif
~isempty(mean0)
mean0 = mean0(:);
validateattributes(mean0,{'numeric'},{'finite','real','numel',numStates0},callerName,'Mean0');
end
Q: Is it just overlook by MathWorks programmers and creating local verions of bnlssm/filter/smooth with that validation step removed is a viable pathway forward, or is it intentional because 'bnlssm' was in fact designed to handle only the time-invariant coef. case correctly?
0 个评论
回答(1 个)
Nipun
2024-6-13
编辑:Nipun
2024-6-13
Hi Oleg,
I understand that you are trying to use the bnlssm object with time-varying coefficients and encountering issues with the parameter mapping function, especially when using cell arrays for the transition matrix A.
The validation step in the bnlssm.m file seems to expect that A is not a cell array, which suggests that the bnlssm object may not fully support time-varying coefficients when they are specified as a cell array. This could be a limitation in the implementation.
To proceed, I recommend the following options:
Here's an example of how you might proceed with modifying the validation step:
Modified paramMap Function
function [A,B,C,D,Mean0,Cov0,StateType] = paramMap(theta, T)
A = cell(T,1);
for t = 1:T
A{t} = @(x) blkdiag([theta(1) theta(2); 0 1], [theta(3) theta(4); 0 1]) * x;
end
B = [theta(5) 0; 0 0; 0 theta(6); 0 0];
C = @(x) log(exp(x(1) - theta(2) / (1 - theta(1))) + exp(x(3) - theta(4) / (1 - theta(3))));
D = theta(7);
Mean0 = [theta(2) / (1 - theta(1)); 1; theta(4) / (1 - theta(3)); 1];
Cov0 = diag([theta(5)^2 / (1 - theta(1)^2) 0 theta(6)^2 / (1 - theta(3)^2) 0]);
StateType = [0; 1; 0; 1]; % Stationary state and constant 1 processes
end
Adjusting the Validation in bnlssm.m
Locate the validation step in bnlssm.m and modify it to handle cell arrays:
if iscell(A)
numStates0 = size(A{1}, 2);
else
numStates0 = size(A, 2);
end
if isscalar(Mean0)
Mean0 = Mean0(ones(numStates0, 1), :);
elseif ~isempty(Mean0)
Mean0 = Mean0(:);
validateattributes(Mean0, {'numeric'}, {'finite', 'real', 'numel', numStates0}, callerName, 'Mean0');
end
For more information on bnlssm, refer to the following MathWorks documentation: https://www.mathworks.com/help/econ/bnlssm.html
Hope this helps.
Regards,
Nipun
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!