-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathprior_laplace.m
145 lines (128 loc) · 3.41 KB
/
prior_laplace.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
function p = prior_laplace(varargin)
%PRIOR_LAPLACE Laplace (double exponential) prior structure
%
% Description
% P = PRIOR_LAPLACE('PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% creates Laplace prior structure in which the named parameters
% have the specified values. Any unspecified parameters are set
% to default values.
%
% P = PRIOR_LAPLACE(P, 'PARAM1', VALUE1, 'PARAM2', VALUE2, ...)
% modify a prior structure with the named parameters altered
% with the specified values.
%
% Parameters for Laplace prior [default]
% mu - location [0]
% s - scale [1]
% mu_prior - prior for mu [prior_fixed]
% s_prior - prior for s [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_LAPLACE';
ip.addOptional('p', [], @isstruct);
ip.addParamValue('mu',0, @(x) isscalar(x) && x>0);
ip.addParamValue('mu_prior',[], @(x) isstruct(x) || isempty(x));
ip.addParamValue('s',1, @(x) isscalar(x) && x>0);
ip.addParamValue('s_prior',[], @(x) isstruct(x) || isempty(x));
ip.parse(varargin{:});
p=ip.Results.p;
if isempty(p)
init=true;
p.type = 'Laplace';
else
if ~isfield(p,'type') && ~isequal(p.type,'Laplace')
error('First argument does not seem to be a valid prior structure')
end
init=false;
end
% Initialize parameters
if init || ~ismember('mu',ip.UsingDefaults)
p.mu = ip.Results.mu;
end
if init || ~ismember('s',ip.UsingDefaults)
p.s = ip.Results.s;
end
% Initialize prior structure
if init
p.p=[];
end
if init || ~ismember('mu_prior',ip.UsingDefaults)
p.p.mu=ip.Results.mu_prior;
end
if init || ~ismember('s_prior',ip.UsingDefaults)
p.p.s=ip.Results.s_prior;
end
if init
% set functions
p.fh.pak = @prior_laplace_pak;
p.fh.unpak = @prior_laplace_unpak;
p.fh.lp = @prior_laplace_lp;
p.fh.lpg = @prior_laplace_lpg;
p.fh.recappend = @prior_laplace_recappend;
end
end
function [w, s, h] = prior_laplace_pak(p)
w=[];
s={};
h=[];
if ~isempty(p.p.mu)
w = p.mu;
s=[s; 'Laplace.mu'];
h = 1;
end
if ~isempty(p.p.s)
w = [w log(p.s)];
s=[s; 'log(Laplace.s)'];
h = [h 1];
end
end
function [p, w] = prior_laplace_unpak(p, w)
if ~isempty(p.p.mu)
i1=1;
p.mu = w(i1);
w = w(i1+1:end);
end
if ~isempty(p.p.s)
i1=1;
p.s = exp(w(i1));
w = w(i1+1:end);
end
end
function lp = prior_laplace_lp(x, p)
lp = sum(-log(2*p.s) - 1./p.s.* abs(x-p.mu));
if ~isempty(p.p.mu)
lp = lp + p.p.mu.fh.lp(p.mu, p.p.mu);
end
if ~isempty(p.p.s)
lp = lp + p.p.s.fh.lp(p.s, p.p.s) + log(p.s);
end
end
function lpg = prior_laplace_lpg(x, p)
lpg = -sign(x-p.mu)./p.s;
if ~isempty(p.p.mu)
lpgmu = sum(sign(x-p.mu)./p.s) + p.p.mu.fh.lpg(p.mu, p.p.mu);
lpg = [lpg lpgmu];
end
if ~isempty(p.p.s)
lpgs = (sum(-1./p.s +1./p.s.^2.*abs(x-p.mu)) + p.p.s.fh.lpg(p.s, p.p.s)).*p.s + 1;
lpg = [lpg lpgs];
end
end
function rec = prior_laplace_recappend(rec, ri, p)
% The parameters are not sampled in any case.
rec = rec;
if ~isempty(p.p.mu)
rec.mu(ri,:) = p.mu;
end
if ~isempty(p.p.s)
rec.s(ri,:) = p.s;
end
end