inverse_gamma_operator.py 3.72 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# This program is free software: you can redistribute it and/or modify
# 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.
#
# This program is distributed in the hope that it will be useful,
# 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
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
17

18
19
import numpy as np
from scipy.stats import invgamma, norm
Philipp Arras's avatar
Philipp Arras committed
20

21
from ..domain_tuple import DomainTuple
22
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
23
24
from ..linearization import Linearization
from ..operators.operator import Operator
Philipp Arras's avatar
Philipp Arras committed
25
from ..sugar import makeOp
26
27


Philipp Arras's avatar
Philipp Arras committed
28
class InverseGammaOperator(Operator):
Martin Reinecke's avatar
Martin Reinecke committed
29
    def __init__(self, domain, alpha, q, delta=0.001):
Philipp Arras's avatar
Philipp Arras committed
30
        """Operator which transforms a Gaussian into an inverse gamma distribution.
31
32
33
34
35
36
37
38
39
40

        The pdf of the inverse gamma distribution is defined as follows:

        .. math::
            \frac {\beta ^{\alpha }}{\Gamma (\alpha )}}x^{-\alpha -1}\exp \left(-{\frac {\beta }{x}}\right)

        That means that for large x the pdf falls off like x^(-alpha -1).
        The mean of the pdf is at q / (alpha - 1) if alpha > 1.
        The mode is q / (alpha + 1).

41
42
43
        This transformation is implemented as a linear interpolation which
        maps a Gaussian onto a inverse gamma distribution.

44
45
46
47
48
49
50
51
52
        Parameters
        ----------
        domain : Domain, tuple of Domain or DomainTuple
            The domain on which the field shall be defined. This is at the same
            time the domain and the target of the operator.
        alpha : float
            The alpha-parameter of the inverse-gamma distribution.
        q : float
            The q-parameter of the inverse-gamma distribution.
53
54
        delta : float
            distance between sampling points for linear interpolation.
55
        """
56
        self._domain = self._target = DomainTuple.make(domain)
Philipp Arras's avatar
Philipp Arras committed
57
        self._alpha, self._q, self._delta = float(alpha), float(q), float(delta)
Martin Reinecke's avatar
Martin Reinecke committed
58
        self._xmin, self._xmax = -8.2, 8.2
Martin Reinecke's avatar
Martin Reinecke committed
59
        # Precompute
Martin Reinecke's avatar
Martin Reinecke committed
60
61
        xs = np.arange(self._xmin, self._xmax+2*delta, delta)
        self._table = np.log(invgamma.ppf(norm.cdf(xs), self._alpha,
Martin Reinecke's avatar
Martin Reinecke committed
62
63
                                          scale=self._q))
        self._deriv = (self._table[1:]-self._table[:-1]) / delta
64

Martin Reinecke's avatar
Martin Reinecke committed
65
    def apply(self, x):
66
        self._check_input(x)
Martin Reinecke's avatar
Martin Reinecke committed
67
68
        lin = isinstance(x, Linearization)
        val = x.val.local_data if lin else x.local_data
69

Martin Reinecke's avatar
Martin Reinecke committed
70
        val = (np.clip(val, self._xmin, self._xmax) - self._xmin) / self._delta
71
72

        # Operator
Martin Reinecke's avatar
Martin Reinecke committed
73
74
        fi = np.floor(val).astype(int)
        w = val - fi
Martin Reinecke's avatar
Martin Reinecke committed
75
        res = np.exp((1 - w)*self._table[fi] + w*self._table[fi + 1])
76
77

        points = Field.from_local_data(self._domain, res)
Martin Reinecke's avatar
Martin Reinecke committed
78
79
        if not lin:
            return points
80
81

        # Derivative of linear interpolation
Martin Reinecke's avatar
Martin Reinecke committed
82
        der = self._deriv[fi]*res
83
84

        jac = makeOp(Field.from_local_data(self._domain, der))
Martin Reinecke's avatar
Martin Reinecke committed
85
        jac = jac(x.jac)
86
        return x.new(points, jac)
87

Martin Reinecke's avatar
Martin Reinecke committed
88
    @staticmethod
Philipp Arras's avatar
Philipp Arras committed
89
    def IG(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
90
91
        foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
        return Field.from_local_data(field.domain, foo)
92

Martin Reinecke's avatar
Martin Reinecke committed
93
    @staticmethod
Philipp Arras's avatar
Philipp Arras committed
94
    def inverseIG(u, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
95
96
        res = norm.ppf(invgamma.cdf(u.local_data, alpha, scale=q))
        return Field.from_local_data(u.domain, res)
Philipp Arras's avatar
Philipp Arras committed
97
98
99
100
101
102
103
104

    @property
    def alpha(self):
        return self._alpha

    @property
    def q(self):
        return self._q