inverse_gamma_model.py 3.05 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 ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
26
from ..models.model import Model
Martin Reinecke's avatar
Martin Reinecke committed
27
from ..multi_field import MultiField
Martin Reinecke's avatar
Martin Reinecke committed
28
from ..operators.selection_operator import SelectionOperator
Philipp Arras's avatar
Philipp Arras committed
29
from ..sugar import makeOp
30 31 32
from ..utilities import memo


33 34 35
class InverseGammaModel(Model):
    def __init__(self, position, alpha, q, key):
        super(InverseGammaModel, self).__init__(position)
36 37
        self._alpha = alpha
        self._q = q
38
        self._key = key
39

40 41 42 43 44
    @classmethod
    def make(cls, actual_position, alpha, q, key):
        pos = cls.inverseIG(actual_position, alpha, q)
        mf = MultiField.from_dict({key: pos})
        return cls(mf, alpha, q, key)
45 46

    def at(self, position):
47
        return self.__class__(position, self._alpha, self._q, self._key)
48 49 50 51

    @property
    @memo
    def value(self):
52
        points = self.position[self._key].local_data
53 54
        # MR FIXME?!
        points = np.clip(points, None, 8.2)
55
        points = Field.from_local_data(self.position[self._key].domain, points)
56 57 58 59
        return self.IG(points, self._alpha, self._q)

    @property
    @memo
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
60
    def jacobian(self):
61
        u = self.position[self._key].local_data
Martin Reinecke's avatar
Martin Reinecke committed
62 63
        inner = norm.pdf(u)
        outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(u),
64 65 66
                                              self._alpha,
                                              scale=self._q),
                                 self._alpha, scale=self._q)
67 68
        # FIXME
        outer_inv = np.clip(outer_inv, 1e-20, None)
69
        outer = 1/outer_inv
70
        grad = Field.from_local_data(self.position[self._key].domain,
Martin Reinecke's avatar
bug fix  
Martin Reinecke committed
71
                                     inner*outer)
72
        grad = makeOp(MultiField.from_dict({self._key: grad},
Martin Reinecke's avatar
Martin Reinecke committed
73
                                           self.position._domain))
74
        return SelectionOperator(grad.target, self._key)*grad
75 76 77

    @staticmethod
    def IG(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
78 79
        foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
        return Field.from_local_data(field.domain, foo)
80 81 82

    @staticmethod
    def inverseIG(u, alpha, q):
83 84
        res = norm.ppf(invgamma.cdf(u.local_data, alpha, scale=q))
        return Field.from_local_data(u.domain, res)