inverse_gamma_model.py 2.64 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
33
class InverseGammaModel(Operator):
    def __init__(self, domain, alpha, q):
34
        self._domain = self._target = DomainTuple.make(domain)
35
36
37
        self._alpha = alpha
        self._q = q

Martin Reinecke's avatar
Martin Reinecke committed
38
    def apply(self, x):
39
        self._check_input(x)
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
        lin = isinstance(x, Linearization)
        val = x.val.local_data if lin else x.local_data
        # MR FIXME?!
        points = np.clip(val, None, 8.2)
Martin Reinecke's avatar
Martin Reinecke committed
44
        points = invgamma.ppf(norm.cdf(points), self._alpha, scale=self._q)
Martin Reinecke's avatar
Martin Reinecke committed
45
46
47
48
49
        points = Field.from_local_data(self._domain, points)
        if not lin:
            return points
        inner = norm.pdf(val)
        outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(val),
50
51
52
                                              self._alpha,
                                              scale=self._q),
                                 self._alpha, scale=self._q)
53
54
        # FIXME
        outer_inv = np.clip(outer_inv, 1e-20, None)
55
        outer = 1/outer_inv
Martin Reinecke's avatar
Martin Reinecke committed
56
57
        jac = makeOp(Field.from_local_data(self._domain, inner*outer))
        jac = jac(x.jac)
58
        return x.new(points, jac)
59

Martin Reinecke's avatar
Martin Reinecke committed
60
    @staticmethod
Philipp Arras's avatar
Philipp Arras committed
61
    def IG(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
62
63
        foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
        return Field.from_local_data(field.domain, foo)
64

Martin Reinecke's avatar
Martin Reinecke committed
65
    @staticmethod
Philipp Arras's avatar
Philipp Arras committed
66
    def inverseIG(u, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
67
68
        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
69
70
71
72
73
74
75
76

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

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