inverse_gamma_model.py 3.78 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

21
22
import numpy as np
from scipy.stats import invgamma, norm
Philipp Arras's avatar
Philipp Arras committed
23
24

from ..compat import *
25
from ..domain_tuple import DomainTuple
26
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
27
28
from ..linearization import Linearization
from ..operators.operator import Operator
Philipp Arras's avatar
Philipp Arras committed
29
from ..sugar import makeOp
30
31


Martin Reinecke's avatar
Martin Reinecke committed
32
class InverseGammaModel(Operator):
Martin Reinecke's avatar
Martin Reinecke committed
33
    def __init__(self, domain, alpha, q, delta=0.001):
34
35
36
37
38
39
40
41
42
43
44
        """Model which transforms a Gaussian into an inverse gamma distribution.

        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).

45
46
47
        This transformation is implemented as a linear interpolation which
        maps a Gaussian onto a inverse gamma distribution.

48
49
50
51
52
53
54
55
56
        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.
57
58
        delta : float
            distance between sampling points for linear interpolation.
59
        """
60
        self._domain = self._target = DomainTuple.make(domain)
61
        self._alpha, self._q, self._delta = alpha, q, delta
Martin Reinecke's avatar
Martin Reinecke committed
62
63
64
65
66
        # Precompute
        xs = np.arange(0., 8.2+2*delta, delta)
        self._table = np.log(invgamma.ppf(norm.cdf(delta), self._alpha,
                                          scale=self._q))
        self._deriv = (self._table[1:]-self._table[:-1]) / delta
67

Martin Reinecke's avatar
Martin Reinecke committed
68
    def apply(self, x):
69
        self._check_input(x)
Martin Reinecke's avatar
Martin Reinecke committed
70
71
        lin = isinstance(x, Linearization)
        val = x.val.local_data if lin else x.local_data
72
73
74
75

        val = np.clip(val, None, 8.2)

        # Operator
Martin Reinecke's avatar
Martin Reinecke committed
76
77
78
79
        fr = val/self._delta
        fi = np.floor(fr).astype(int)
        w = fr - fi
        res = np.exp((1 - w)*self._table[fi] + w*self._table[fi + 1])
80
81

        points = Field.from_local_data(self._domain, res)
Martin Reinecke's avatar
Martin Reinecke committed
82
83
        if not lin:
            return points
84
85

        # Derivative of linear interpolation
Martin Reinecke's avatar
Martin Reinecke committed
86
        der = self._deriv[fi]*res
87
88

        jac = makeOp(Field.from_local_data(self._domain, der))
Martin Reinecke's avatar
Martin Reinecke committed
89
        jac = jac(x.jac)
90
        return x.new(points, jac)
91

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

Martin Reinecke's avatar
Martin Reinecke committed
97
    @staticmethod
Philipp Arras's avatar
Philipp Arras committed
98
    def inverseIG(u, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
99
100
        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
101
102
103
104
105
106
107
108

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

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