| 1 |
function [Galpha,stdthe] = fn_dirichprior(Glamda,std_maxv) |
| 2 |
% [Galpha,stdthe] = fn_dirichprior(Glamda,std_maxv) |
| 3 |
% Setup of the Dirichlet prior on the transition matrix P. Given the mode of each random |
| 4 |
% variable theta_j, solve for the (hyper)parameter \alpha where one of \alpha, say, \alpha_j |
| 5 |
% is free for controlling the overall variance of \alpha consisdent with the given modes. |
| 6 |
% |
| 7 |
% Glamda: h-by-1 vector of prior modes for the random variables \theta. |
| 8 |
% For each column of P (transitional matrix), Glamda'll be different with a large weight on a diaganol. |
| 9 |
% std_maxv: prior standard deviation for the selected random variable |
| 10 |
% (here the one with the maximum (largest) mode). |
| 11 |
%---------- |
| 12 |
% Galpha: h-by-1 vector of parameters' values for the Dirichlet distribtuion. If one of the |
| 13 |
% elements in Galpha is Inf, we mean a degenerate case where stdthe is set to zeros. |
| 14 |
% stdthe: h-by-1 vector of standard deviations for all random variables \theta. |
| 15 |
% |
| 16 |
% See Gelman, et al p. 476 and TVBvar Note pp. 24-31 |
| 17 |
% Tao Zha, March 2001 |
| 18 |
% |
| 19 |
% Copyright (C) 1997-2012 Tao Zha |
| 20 |
% |
| 21 |
% This free software: you can redistribute it and/or modify |
| 22 |
% it under the terms of the GNU General Public License as published by |
| 23 |
% the Free Software Foundation, either version 3 of the License, or |
| 24 |
% (at your option) any later version. |
| 25 |
% |
| 26 |
% It is distributed in the hope that it will be useful, |
| 27 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
% GNU General Public License for more details. |
| 30 |
% |
| 31 |
% If you did not received a copy of the GNU General Public License |
| 32 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 33 |
% |
| 34 |
|
| 35 |
|
| 36 |
Glamda = Glamda(:); % Column vector! Prior modes for the random variables \theta. |
| 37 |
[maxlam,indxlam] = max(Glamda); % index for the selected hyperparameter that is freely set to match the variance of the corresponding random variable. |
| 38 |
ndiri = length(Glamda); % the number of random variables in the Dirichlet distribution. |
| 39 |
Galpha=zeros(ndiri,1); % \alpha's: hyperparameters that need to be choosen. Must be all greater than 1. |
| 40 |
stdthe = zeros(ndiri,1); % Standard deviations for each \theta |
| 41 |
if indxlam>ndiri |
| 42 |
warning('Make sure indxlam is less than the total number of parameters in the Dirichlet') |
| 43 |
error(' ') |
| 44 |
end |
| 45 |
if (maxlam==1) % We treat this as a degenerate case for convenient coding. |
| 46 |
Galpha(indxlam)=Inf; % Note the use of Inf, NOT NaN. Because |
| 47 |
% 0(or any finite number)/Inf=0 while 0/NaN gives a nonsense (i.e., NaN again). |
| 48 |
% See tveml.m and pp. 27a-27b for more information. |
| 49 |
return % The function stops here. |
| 50 |
end |
| 51 |
|
| 52 |
|
| 53 |
%*** Given std_maxv, find the corresponding hyperparameter Galpha_j by soloving |
| 54 |
% a polynomial of order 3. See Note (*) page 30. |
| 55 |
vj=std_maxv^2; % variance of the selected random variable \theta. |
| 56 |
Gsi = (1-Glamda(indxlam))/Glamda(indxlam); % unchanged value, given Glamda. |
| 57 |
coevec = zeros(4,1); % 4 because we're going to consider a polynomial of order 3 (+ constant). |
| 58 |
coevec(1) = vj*(1+Gsi)^3; |
| 59 |
coevec(2) = vj*(1+Gsi)^2*(3*(ndiri-Gsi)-2) - Gsi; |
| 60 |
coevec(3) = (ndiri-Gsi-1)*(vj*(1+Gsi)*(3*(ndiri-Gsi)-1)-1); |
| 61 |
coevec(4) = vj*(ndiri-Gsi)*(ndiri-Gsi-1)^2; |
| 62 |
|
| 63 |
Galphaj = max(roots(coevec)); % j: jth element, set to match the variance. Must be greater than 1. |
| 64 |
if (Galphaj<=1) |
| 65 |
disp(' ') |
| 66 |
warning('Error! Make sure std_maxv is small enough to have a large Galphaj > 1') |
| 67 |
error(' ') |
| 68 |
end |
| 69 |
|
| 70 |
%*** Solve for the rest of \alpha's. See Note page 27. |
| 71 |
A = eye(ndiri) - repmat(Glamda,[1 ndiri]); |
| 72 |
C = ones(ndiri,1) + (Galphaj-ndiri)*Glamda; |
| 73 |
A1=A; C1=C; |
| 74 |
A1(indxlam,:)=[]; A1(:,indxlam)=[]; C1(indxlam)=[]; |
| 75 |
indxrest=1:ndiri; indxrest(indxlam)=[]; |
| 76 |
Galpha(indxrest) = A1\C1; |
| 77 |
Galpha(indxlam) = Galphaj; |
| 78 |
|
| 79 |
%========= Debugging or checking the properties ========== |
| 80 |
%disp(' ') |
| 81 |
%disp('2nd-to-first-or-rest ratio: it should not change with the last element of \alpha') |
| 82 |
%(Galpha(2)-1)/(Galpha(1)-1) |
| 83 |
%(Galpha(2)-1)/(sum(Galpha([1 3]))-2) |
| 84 |
%disp(' ') |
| 85 |
%disp('Solution for \alpha') |
| 86 |
%Galpha |
| 87 |
|
| 88 |
for k=1:ndiri |
| 89 |
tmp=Galpha; |
| 90 |
tmp(k)=[]; |
| 91 |
stdthe(k)=sqrt( Galpha(k)*sum(tmp)/(sum(Galpha)^2*(sum(Galpha)+1)) ); |
| 92 |
end |
| 93 |
%disp(' ') |
| 94 |
%disp('Standard deviations for each \theta') |
| 95 |
%stdthe |