-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprior_pcnu.m
149 lines (120 loc) · 3.81 KB
/
prior_pcnu.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
147
148
149
function p = prior_pcnu(varargin)
% PRIOR_PCNU penalized complexity priors structure
%
% Description
% P = PRIOR_PCNU('PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% creates penalized-complexity prior in which the
% named parameters have the specified values. Any unspecified
% parameters are set to default values.
%
% P = PRIOR_PCNU(P, 'PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% modify a prior structure with the named parameters altered
% with the specified values.
%
% The parameterization is on the real line (x) : 1/τ = σ²
%
% Parameters for pc-priors [default]
% lambda - penalization parameters [1]
% ualpha - vector (U, α)
%
% See also
% PRIOR_*
%
% Penalizing model component complexity: A principled practical approach
% to constructing priors. Daniel Simpson, Håvard Hue. Thiago G. Martins.
% Andrea Riebler and Sigrrun Sørbye. ArXiv 2015. Submitted to Statistical Science.
%
% Copyright (c) 2000-2001,2010 Aki Vehtari
% Copyright (c) 2009 Jarno Vanhatalo
% Copyright (c) 2010 Jaakko Riihimäki
% ───────────── 2015 Marcelo Hartmann
% 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_PCNU';
ip.addOptional('p', [], @isstruct);
ip.addParamValue('lambda', 1, @(x) isscalar(x) && x > 0);
ip.addParamValue('lambda_prior',[], @(x) isstruct(x) || isempty(x));
ip.addParamValue('ualpha', [], @(x) isvector(x) && length(x) == 2);
ip.parse(varargin{:});
p = ip.Results.p;
if isempty(p)
init = true;
p.type = 'pcnu';
else
if ~isfield(p, 'type') && ~isequal(p.type, 'pc')
error('First argument does not seem to be a valid prior structure')
end
init = false;
end
% initialize
if init && ~ismember('ualpha', ip.UsingDefaults)
p.ualpha = ip.Results.ualpha;
% solve for λ. P(ν < c) = p
p.lambda = -log(p.ualpha(2)) .* p.ualpha(1);
elseif init || ~ismember('lambda', ip.UsingDefaults)
p.lambda = ip.Results.lambda;
end
% Initialize prior structure
if init
p.p = [];
end
if init || ~ismember('lambda_prior', ip.UsingDefaults)
p.p.lambda = ip.Results.lambda_prior;
end
if init
% set functions
p.fh.pak = @prior_pc_pak;
p.fh.unpak = @prior_pc_unpak;
p.fh.lp = @prior_pc_lp;
p.fh.lpg = @prior_pc_lpg;
p.fh.recappend = @prior_pc_recappend;
end
end
function [w, s, h] = prior_pc_pak(p)
% This is a mandatory subfunction used for example
% in energy and gradient computations.
w = [];
s = {};
h = [];
if ~isempty(p.p.lambda)
w = log(p.lambda);
s = [s; 'log(pc.lambda)'];
h = 1;
end
end
function [p, w] = prior_pc_unpak(p, w)
% This is a mandatory subfunction used for example
% in energy and gradient computations.
if ~isempty(p.p.lambda)
i1 = 1;
p.lambda = exp(w(i1));
w = w(i1 + 1:end);
end
end
function lp = prior_pc_lp(x, p)
% This is a mandatory subfunction used for example
% in energy computations.
lp = log(p.lambda) - 2*log(x) - p.lambda./x;
if ~isempty(p.p.lambda)
lp = lp + p.p.lambda.fh.lp(p.lambda, p.p.lambda);
end
end
function lpg = prior_pc_lpg(x, p)
% This is a mandatory subfunction used for example
% in gradient computations.
lpg = -2./x + p.lambda./(x.^2);
if ~isempty(p.p.lambda)
lpglambda = -sqrt(x) + 1/p.lambda + p.p.lambda.fh.lpg(p.lambda, p.p.lambda);
lpg = [lpg lpglambda];
end
end
function rec = prior_pc_recappend(rec, ri, p)
% This subfunction is needed when using MCMC sampling (gp_mc).
% The parameters are not sampled in any case.
rec = rec;
if ~isempty(p.p.lambda)
rec.mu(ri, :) = p.lambda;
end
end