Skip to content

Commit

Permalink
v1.0.12 Added log pdfs (#6)
Browse files Browse the repository at this point in the history
* feat: added log pdfs and updated example file

* fix: updated version

* Update README.md
  • Loading branch information
lacerbi authored Oct 26, 2022
1 parent 84b29cc commit f3c05d6
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 201 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Variational Bayesian Monte Carlo (VBMC) - v1.0.11
# Variational Bayesian Monte Carlo (VBMC) - v1.0.12

**News:**
- Added a [Presentations](#presentations) section with links to (relatively) recent slides and video recordings of VBMC related work.
Expand Down
65 changes: 65 additions & 0 deletions shared/msmoothboxlogpdf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function y = msmoothboxlogpdf(x,a,b,sigma)
%MSMOOTHBOXLOGPDF Multivariate smooth-box log probability density function.
% Y = MSMOOTHBOXLOGPDF(X,A,B,SIGMA) returns the logarithm of the pdf of
% the multivariate smooth-box distribution with pivots A and B and scale
% SIGMA, evaluated at the values in X. The multivariate smooth-box pdf is
% the product of univariate smooth-box pdfs in each dimension.
%
% For each dimension i, the univariate smooth-box pdf is defined as a
% uniform distribution between pivots A(i), B(i) and Gaussian tails that
% fall starting from p(A(i)) to the left (resp., p(B(i)) to the right)
% with standard deviation SIGMA(i).
%
% X can be a matrix, where each row is a separate point and each column
% is a different dimension. Similarly, A, B, and SIGMA can also be
% matrices of the same size as X.
%
% The log pdf is typically preferred in numerical computations involving
% probabilities, as it is more stable.
%
% See also MSMOOTHBOXPDF, MSMOOTHBOXRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if any(sigma(:) <= 0)
error('msmoothboxpdf:NonPositiveSigma', ...
'All elements of SIGMA should be positive.');
end

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
if isscalar(sigma); sigma = sigma*ones(1,D); end
end

if size(a,2) ~= D || size(b,2) ~= D || size(sigma,2) ~= D
error('msmoothboxpdf:SizeError', ...
'A, B, SIGMA should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end
if size(sigma,1) == 1; sigma = repmat(sigma,[N,1]); end

if any(a(:) >= b(:))
error('msmoothboxpdf:OrderError', ...
'For all elements of A and B, the order A < B should hold.');
end

y = -inf(size(x));
lnf = log(1/sqrt(2*pi)./sigma) - log1p(1/sqrt(2*pi)./sigma.*(b - a));

for ii = 1:D
idx = x(:,ii) < a(:,ii);
y(idx,ii) = lnf(idx,ii) - 0.5*((x(idx,ii) - a(idx,ii))./sigma(idx,ii)).^2;

idx = x(:,ii) >= a(:,ii) & x(:,ii) <= b(:,ii);
y(idx,ii) = lnf(idx,ii);

idx = x(:,ii) > b(:,ii);
y(idx,ii) = lnf(idx,ii) - 0.5*((x(idx,ii) - b(idx,ii))./sigma(idx,ii)).^2;
end

y = sum(y,2);
46 changes: 2 additions & 44 deletions shared/msmoothboxpdf.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,8 @@
% is a different dimension. Similarly, A, B, and SIGMA can also be
% matrices of the same size as X.
%
% See also MSMOOTHBOXRND.
% See also MSMOOTHBOXLOGPDF, MSMOOTHBOXRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if any(sigma(:) <= 0)
error('msmoothboxpdf:NonPositiveSigma', ...
'All elements of SIGMA should be positive.');
end

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
if isscalar(sigma); sigma = sigma*ones(1,D); end
end

if size(a,2) ~= D || size(b,2) ~= D || size(sigma,2) ~= D
error('msmoothboxpdf:SizeError', ...
'A, B, SIGMA should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end
if size(sigma,1) == 1; sigma = repmat(sigma,[N,1]); end

if any(a(:) >= b(:))
error('msmoothboxpdf:OrderError', ...
'For all elements of A and B, the order A < B should hold.');
end

y = zeros(size(x));
nf = 1 + 1/sqrt(2*pi)./sigma.*(b - a);

for ii = 1:D
idx = x(:,ii) < a(:,ii);
y(idx,ii) = 1/sqrt(2*pi)./sigma(idx,ii).* exp(-0.5*((x(idx,ii) - a(idx,ii))./sigma(idx,ii)).^2) ./ nf(idx,ii);

idx = x(:,ii) >= a(:,ii) & x(:,ii) <= b(:,ii);
y(idx,ii) = 1/sqrt(2*pi)./sigma(idx,ii) ./ nf(idx,ii);

idx = x(:,ii) > b(:,ii);
y(idx,ii) = 1/sqrt(2*pi)./sigma(idx,ii).* exp(-0.5*((x(idx,ii) - b(idx,ii))./sigma(idx,ii)).^2) ./ nf(idx,ii);

end

y = prod(y,2);
y = exp(msmoothboxlogpdf(x,a,b,sigma));
71 changes: 71 additions & 0 deletions shared/msplinetrapezlogpdf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function y = msplinetrapezlogpdf(x,a,b,c,d)
%MSPLINETRAPEZLOGPDF Multivariate spline-trapezoidal log pdf.
% Y = MSPLINETRAPEZLOGPDF(X,A,B,C,D) returns the logarithm of the pdf of
% the multivariate spline-trapezoidal distribution with external bounds
% A and D and internal points B and C, evaluated at the values in X. The
% multivariate pdf is the product of univariate spline-trapezoidal pdfs
% in each dimension.
%
% For each dimension i, the univariate spline-trapezoidal pdf is defined
% as a trapezoidal pdf whose points A, B and C, D are connected by cubic
% splines such that the pdf is continuous and its derivatives at A, B, C,
% and D are zero (so the derivatives are also continuous):
%
% | __________
% | /| |\
% p(X(i)) | / | | \
% | / | | \
% |___/___|________|___\____
% A(i) B(i) C(i) D(i)
% X(i)
%
% X can be a matrix, where each row is a separate point and each column
% is a different dimension. Similarly, A, B, C, and D can also be
% matrices of the same size as X.
%
% The log pdf is typically preferred in numerical computations involving
% probabilities, as it is more stable.
%
% See also MSPLINETRAPEZPDF, MSPLINETRAPEZRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
if isscalar(c); c = c*ones(1,D); end
if isscalar(d); d = d*ones(1,D); end
end

if size(a,2) ~= D || size(b,2) ~= D || size(c,2) ~= D || size(d,2) ~= D
error('msplinetrapezlogpdf:SizeError', ...
'A, B, C, D should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end
if size(c,1) == 1; c = repmat(c,[N,1]); end
if size(d,1) == 1; d = repmat(d,[N,1]); end

y = -inf(size(x));
% Normalization factor
% nf = c - b + 0.5*(d - c + b - a);
lnf = log(0.5*(c - b + d - a));

for ii = 1:D
idx = x(:,ii) >= a(:,ii) & x(:,ii) < b(:,ii);
z = (x(idx,ii) - a(idx,ii))./(b(idx,ii) - a(idx,ii));
y(idx,ii) = log(-2*z.^3 + 3*z.^2) - lnf(idx,ii);

idx = x(:,ii) >= b(:,ii) & x(:,ii) < c(:,ii);
y(idx,ii) = -lnf(idx,ii);

idx = x(:,ii) >= c(:,ii) & x(:,ii) < d(:,ii);
z = 1 - (x(idx,ii) - c(idx,ii)) ./ (d(idx,ii) - c(idx,ii));
y(idx,ii) = log(-2*z.^3 + 3*z.^2) - lnf(idx,ii);
end

y = sum(y,2);

42 changes: 2 additions & 40 deletions shared/msplinetrapezpdf.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,8 @@
% is a different dimension. Similarly, A, B, C, and D can also be
% matrices of the same size as X.
%
% See also MSPLINETRAPEZRND.
% See also MSPLINETRAPEZLOGPDF, MSPLINETRAPEZRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
if isscalar(c); c = c*ones(1,D); end
if isscalar(d); d = d*ones(1,D); end
end

if size(a,2) ~= D || size(b,2) ~= D || size(c,2) ~= D || size(d,2) ~= D
error('msplinetrapezpdf:SizeError', ...
'A, B, C, D should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end
if size(c,1) == 1; c = repmat(c,[N,1]); end
if size(d,1) == 1; d = repmat(d,[N,1]); end

y = zeros(size(x));
% Normalization factor
% nf = c - b + 0.5*(d - c + b - a);
nf = 0.5*(c - b + d - a);

for ii = 1:D
idx = x(:,ii) >= a(:,ii) & x(:,ii) < b(:,ii);
z = (x(idx,ii) - a(idx,ii))./(b(idx,ii) - a(idx,ii));
y(idx,ii) = (-2*z.^3 + 3*z.^2) ./ nf(idx,ii);

idx = x(:,ii) >= b(:,ii) & x(:,ii) < c(:,ii);
y(idx,ii) = 1 ./ nf(idx,ii);

idx = x(:,ii) >= c(:,ii) & x(:,ii) < d(:,ii);
z = 1 - (x(idx,ii) - c(idx,ii)) ./ (d(idx,ii) - c(idx,ii));
y(idx,ii) = (-2*z.^3 + 3*z.^2) ./ nf(idx,ii);
end

y = prod(y,2);

y = exp(msplinetrapezlogpdf(x,a,b,c,d));
63 changes: 63 additions & 0 deletions shared/mtrapezlogpdf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function y = mtrapezlogpdf(x,a,u,v,b)
%MTRAPEZLOGPDF Multivariate trapezoidal probability log pdf.
% Y = MTRAPEZLOGPDF(X,A,U,V,B) returns the logarithm of the pdf of the
% multivariate trapezoidal distribution with external bounds A and B and
% internal points U and V, evaluated at the values in X. The multivariate
% trapezoidal pdf is the product of univariate trapezoidal pdfs in each
% dimension.
%
% For each dimension i, the univariate trapezoidal pdf is defined as:
%
% | __________
% | /| |\
% p(X(i)) | / | | \
% | / | | \
% |___/___|________|___\____
% A(i) U(i) V(i) B(i)
% X(i)
%
% X can be a matrix, where each row is a separate point and each column
% is a different dimension. Similarly, A, B, C, and D can also be
% matrices of the same size as X.
%
% The log pdf is typically preferred in numerical computations involving
% probabilities, as it is more stable.
%
% See also MTRAPEZPDF, MTRAPEZRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(u); u = u*ones(1,D); end
if isscalar(v); v = v*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
end

if size(a,2) ~= D || size(u,2) ~= D || size(v,2) ~= D || size(b,2) ~= D
error('mtrapezpdf:SizeError', ...
'A, B, C, D should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(u,1) == 1; u = repmat(u,[N,1]); end
if size(v,1) == 1; v = repmat(v,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end

y = -inf(size(x));
lnf = log(0.5) + log(b - a + v - u) + log(u - a);

for ii = 1:D
idx = x(:,ii) >= a(:,ii) & x(:,ii) < u(:,ii);
y(idx,ii) = log(x(idx,ii) - a(idx,ii)) - lnf(idx,ii);

idx = x(:,ii) >= u(:,ii) & x(:,ii) < v(:,ii);
y(idx,ii) = log(u(idx,ii)-a(idx,ii)) - lnf(idx,ii);

idx = x(:,ii) >= v(:,ii) & x(:,ii) < b(:,ii);
y(idx,ii) = log(b(idx,ii) - x(idx,ii)) - log(b(idx,ii) - v(idx,ii)) + log(u(idx,ii)-a(idx,ii)) - lnf(idx,ii);
end

y = sum(y,2);
37 changes: 2 additions & 35 deletions shared/mtrapezpdf.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,8 @@
% is a different dimension. Similarly, A, B, C, and D can also be
% matrices of the same size as X.
%
% See also MTRAPEZRND.
% See also MTRAPEZLOGPDF, MTRAPEZRND.

% Luigi Acerbi 2022

[N,D] = size(x);

if D > 1
if isscalar(a); a = a*ones(1,D); end
if isscalar(u); u = u*ones(1,D); end
if isscalar(v); v = v*ones(1,D); end
if isscalar(b); b = b*ones(1,D); end
end

if size(a,2) ~= D || size(u,2) ~= D || size(v,2) ~= D || size(b,2) ~= D
error('mtrapezpdf:SizeError', ...
'A, B, C, D should be scalars or have the same number of columns as X.');
end

if size(a,1) == 1; a = repmat(a,[N,1]); end
if size(u,1) == 1; u = repmat(u,[N,1]); end
if size(v,1) == 1; v = repmat(v,[N,1]); end
if size(b,1) == 1; b = repmat(b,[N,1]); end

y = zeros(size(x));
nf = 0.5 * (b - a + v - u) .* (u - a);

for ii = 1:D
idx = x(:,ii) >= a(:,ii) & x(:,ii) < u(:,ii);
y(idx,ii) = (x(idx,ii) - a(idx,ii)) ./ nf(idx,ii);

idx = x(:,ii) >= u(:,ii) & x(:,ii) < v(:,ii);
y(idx,ii) = (u(idx,ii)-a(idx,ii)) ./ nf(idx,ii);

idx = x(:,ii) >= v(:,ii) & x(:,ii) < b(:,ii);
y(idx,ii) = (b(idx,ii) - x(idx,ii))./(b(idx,ii) - v(idx,ii)) .* (u(idx,ii)-a(idx,ii)) ./ nf(idx,ii);
end

y = prod(y,2);
y = exp(mtrapezlogpdf(x,a,u,v,b));
Loading

0 comments on commit f3c05d6

Please sign in to comment.