logprior.py 4.27 KB
Newer Older
Jean-Baptiste Delisle's avatar
Jean-Baptiste Delisle committed
1
2
3
4
# -*- coding: utf-8 -*-

# Copyright 2021 Jean-Baptiste Delisle
#
5
# This file is part of samsam.
Jean-Baptiste Delisle's avatar
Jean-Baptiste Delisle committed
6
#
7
# samsam is free software: you can redistribute it and/or modify
Jean-Baptiste Delisle's avatar
Jean-Baptiste Delisle committed
8
9
10
11
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
12
# samsam is distributed in the hope that it will be useful,
Jean-Baptiste Delisle's avatar
Jean-Baptiste Delisle committed
13
14
15
16
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
18
# along with samsam.  If not, see <http://www.gnu.org/licenses/>.
Jean-Baptiste Delisle's avatar
Jean-Baptiste Delisle committed
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

import numpy as np
from scipy.special import beta as sp_beta

log2pi = np.log(2.0 * np.pi)
halflog2pi = log2pi / 2.0


def uniform(x, a=0.0, b=1.0):
  r"""
  Uniform distribution over an interval :math:`[a,b[`.

  Parameters
  ----------
  x : float
    Value of the variable.
  a : float
    Interval lower bound.
  b : float
    Interval upper bound.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  if x < a or x >= b:
    raise Exception('uniform: inf')
  return (-np.log(b - a))


def uniform_periodic(x, period):
  r"""
  Uniform distribution over a periodic interval.

  Parameters
  ----------
  x : float
    Value of the variable.
  period : float
    Periodicity.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  return (-np.log(period))


def loguniform(x, loga=0.0, logb=1.0):
  r"""
  Log-uniform distribution over an interval :math:`[a,b[`.

  Parameters
  ----------
  x : float
    Value of the variable.
  loga : float
    Logarithm of the lower bound.
  logb : float
    Logarithm of the upper bound.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  if x <= 0:
    raise Exception('loguniform: inf')
  logx = np.log(x)
  return (-logx + uniform(logx, loga, logb))


def moduniform(x, y, a=0.0, b=1.0):
  r"""
  Uniform distribution for the module :math:`\sqrt{x^2+y^2}`
  over an interval :math:`[a,b[`.

  Parameters
  ----------
  x : float
    Value of the abscissa.
  y : float
    Value of the ordinate.
  a : float
    Interval lower bound.
  b : float
    Interval upper bound.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  r = np.sqrt(x**2 + y**2)
  if r <= a or r >= b:
    raise Exception('moduniform: inf')
  return (-(log2pi + np.log(r * (b - a))))


def normal(x, mu=0.0, sig=1.0):
  r"""
  Normal distribution with parameters (mu,sig).

  Parameters
  ----------
  x : float
    Value of the variable.
  mu : float
    Mean.
  sig : float
    Standard-deviation.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  return (-(halflog2pi + np.log(sig) + ((x - mu) / sig)**2 / 2.0))


def lognormal(x, mu=0.0, sig=1.0):
  r"""
  Log-normal distribution with parameters (mu,sig).

  Parameters
  ----------
  x : float
    Value of the variable.
  mu : float
    Mean of :math:`\log(x)`.
  sig : float
    Standard-deviation of :math:`\log(x)`.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  if x <= 0:
    raise Exception('lognormal: inf')
  logx = np.log(x)
  return (-logx + normal(logx, mu, sig))


logbeta_dic = {}


def _lazy_logbeta(a, b):
  r"""
  Lazy computation of log(beta(a,b))
  """
  if (a, b) not in logbeta_dic:
    logbeta_dic[(a, b)] = np.log(sp_beta(a, b))
  return (logbeta_dic[(a, b)])


def beta(x, a, b):
  r"""
  Beta distribution with parameters (a, b).

  Parameters
  ----------
  x : float
    Value of the variable.
  a, b : float
    Shape parameters.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  if x <= 0 or x >= 1:
    raise Exception('beta: inf')
  return ((a - 1.0) * np.log(x) + (b - 1.0) * np.log(1.0 - x) -
    _lazy_logbeta(a, b))


def modbeta(x, y, a, b):
  r"""
  Beta distribution with parameters (a, b)
  for the module :math:`\sqrt{x^2+y^2}`.

  Parameters
  ----------
  x : float
    Value of the abscissa.
  y : float
    Value of the ordinate.
  a, b : float
    Shape parameters.

  Returns
  -------
  lp : float
    Log-probability of x.
  """

  r = np.sqrt(x**2 + y**2)
  if r == 0 or r >= 1:
    raise Exception('modbeta: inf')
  return ((a - 2.0) * np.log(r) + (b - 1.0) * np.log(1.0 - r) -
    _lazy_logbeta(a, b) - log2pi)