-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathprior_sinvchi2.m
146 lines (131 loc) · 3.77 KB
/
prior_sinvchi2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function p = prior_sinvchi2(varargin)
%PRIOR_SINVCHI2 Scaled-Inv-Chi^2 prior structure
%
% Description
% P = PRIOR_SINVCHI2('PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% creates Scaled-Inv-Chi^2 prior structure in which the named
% parameters have the specified values. Any unspecified
% parameters are set to default values.
%
% P = PRIOR_SINVCHI2(P, 'PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% modify a prior structure with the named parameters altered
% with the specified values.
%
% The parameterization is as in Gelman, Carlin, Stern, Dunson, Vehtari,
% and Rubin (2013). Bayesian Data Analysis, third edition.
%
% Parameters for Scaled-Inv-Chi^2 [default]
% s2 - scale squared (variance) [1]
% nu - degrees of freedom [4]
% s2_prior - prior for s2 [prior_fixed]
% nu_prior - prior for nu [prior_fixed]
%
% See also
% PRIOR_*
%
% Copyright (c) 2000-2001,2010 Aki Vehtari
% Copyright (c) 2010 Jaakko Riihimäki
% This software is distributed under the GNU General Public
% License (version 3 or later); please refer to the file
% License.txt, included with the software, for details.
ip=inputParser;
ip.FunctionName = 'PRIOR_SINVCHI2';
ip.addOptional('p', [], @isstruct);
ip.addParamValue('s2',1, @(x) isscalar(x) && x>0);
ip.addParamValue('s2_prior',[], @(x) isstruct(x) || isempty(x));
ip.addParamValue('nu',4, @(x) isscalar(x) && x>0);
ip.addParamValue('nu_prior',[], @(x) isstruct(x) || isempty(x));
ip.parse(varargin{:});
p=ip.Results.p;
if isempty(p)
init=true;
p.type = 'S-Inv-Chi2';
else
if ~isfield(p,'type') && ~isequal(p.type,'S-Inv-Chi2')
error('First argument does not seem to be a valid prior structure')
end
init=false;
end
% Initialize parameters
if init || ~ismember('s2',ip.UsingDefaults)
p.s2 = ip.Results.s2;
end
if init || ~ismember('nu',ip.UsingDefaults)
p.nu = ip.Results.nu;
end
% Initialize prior structure
if init
p.p=[];
end
if init || ~ismember('s2_prior',ip.UsingDefaults)
p.p.s2=ip.Results.s2_prior;
end
if init || ~ismember('nu_prior',ip.UsingDefaults)
p.p.nu=ip.Results.nu_prior;
end
if init
% set functions
p.fh.pak = @prior_sinvchi2_pak;
p.fh.unpak = @prior_sinvchi2_unpak;
p.fh.lp = @prior_sinvchi2_lp;
p.fh.lpg = @prior_sinvchi2_lpg;
p.fh.recappend = @prior_sinvchi2_recappend;
end
end
function [w, s, h] = prior_sinvchi2_pak(p)
w=[];
s={};
h=[];
if ~isempty(p.p.s2)
w = log(p.s2);
s=[s; 'log(Sinvchi2.s2)'];
h = 1;
end
if ~isempty(p.p.nu)
w = [w log(p.nu)];
s=[s; 'log(Sinvchi2.nu)'];
h = [h 1];
end
end
function [p, w] = prior_sinvchi2_unpak(p, w)
if ~isempty(p.p.s2)
i1=1;
p.s2 = exp(w(i1));
w = w(i1+1:end);
end
if ~isempty(p.p.nu)
i1=1;
p.nu = exp(w(i1));
w = w(i1+1:end);
end
end
function lp = prior_sinvchi2_lp(x, p)
lp = -sum((p.nu./2+1) .* log(x) + (p.s2.*p.nu./2./x) + (p.nu/2) .* log(2./(p.s2.*p.nu)) + gammaln(p.nu/2)) ;
if ~isempty(p.p.s2)
lp = lp + p.p.s2.fh.lp(p.s2, p.p.s2) + log(p.s2);
end
if ~isempty(p.p.nu)
lp = lp + p.p.nu.fh.lp(p.nu, p.p.nu) + log(p.nu);
end
end
function lpg = prior_sinvchi2_lpg(x, p)
lpg = -(p.nu/2+1)./x +p.nu.*p.s2./(2*x.^2);
if ~isempty(p.p.s2)
lpgs2 = (-sum(p.nu/2.*(1./x-1./p.s2)) + p.p.s2.fh.lpg(p.s2, p.p.s2)).*p.s2 + 1;
lpg = [lpg lpgs2];
end
if ~isempty(p.p.nu)
lpgnu = (-sum(0.5*(log(x) + p.s2./x + log(2./p.s2./p.nu) - 1 + digamma1(p.nu/2))) + p.p.nu.fh.lpg(p.nu, p.p.nu)).*p.nu + 1;
lpg = [lpg lpgnu];
end
end
function rec = prior_sinvchi2_recappend(rec, ri, p)
% The parameters are not sampled in any case.
rec = rec;
if ~isempty(p.p.s2)
rec.s2(ri,:) = p.s2;
end
if ~isempty(p.p.nu)
rec.nu(ri,:) = p.nu;
end
end