about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/potentials/@cpot/cpot_to_mpot.m
blob: 73739d7e2405910c43283cf67566a9b0f0bcfdf0 (plain)
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
function mom = cpot_to_mpot(can)
% CPOT_TO_MPOT Convert a canonical potential to moment form.
% mom = cpot_to_mpot(can)

[logp, mu, Sigma] = canonical_to_moment(can.g, can.h, can.K);
mom = mpot(can.domain, can.sizes, logp, mu, Sigma);

%%%%%%%

function [logp, mu, Sigma] = canonical_to_moment(g, h, K)
% CANONICAL_TO_MOMENT Convert canonical characteristics to moment form.
% [logp, mu, Sigma] = canonical_to_moment(g, h, K)

n = length(K);
if isempty(K)
  logp = g - 0.5*(log(det(K)) - n*log(2*pi));
  Sigma = [];
  mu = [];
else
  if det(K)==0
    Sigma = inf*ones(n,n);
    mu = zeros(n,1); % if the precision is zero, the mean is arbitrary
    logp = g; % the scaling factor for the uniform distribution is 1
  else
    Sigma = inv(K);
    mu = Sigma*h;
    logp = g - 0.5*(log(det(K)) - n*log(2*pi) - mu'*K*mu);
  end
end