point_sources.py 2.47 KB
Newer Older
1 2
from __future__ import absolute_import, division, print_function
from ..compat import *
3 4 5 6
import numpy as np
from scipy.stats import invgamma, norm
from ..field import Field
from ..sugar import makeOp
Martin Reinecke's avatar
Martin Reinecke committed
7 8
from ..multi.multi_field import MultiField
from ..models.model import Model
9

Martin Reinecke's avatar
Martin Reinecke committed
10
from ..operators.selection_operator import SelectionOperator
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
from ..utilities import memo


class PointSources(Model):
    def __init__(self, position, alpha, q):
        super(PointSources, self).__init__(position)
        self._alpha = alpha
        self._q = q

    def at(self, position):
        return self.__class__(position, self._alpha, self._q)

    @property
    @memo
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
26
        points = self.position['points'].local_data
Martin Reinecke's avatar
Martin Reinecke committed
27
        # MR FIXME?!
28
        points = np.clip(points, None, 8.2)
Martin Reinecke's avatar
Martin Reinecke committed
29
        points = Field.from_local_data(self.position['points'].domain, points)
30 31 32 33
        return self.IG(points, self._alpha, self._q)

    @property
    @memo
Martin Reinecke's avatar
fixes  
Martin Reinecke committed
34
    def jacobian(self):
Martin Reinecke's avatar
Martin Reinecke committed
35 36 37
        u = self.position['points'].local_data
        inner = norm.pdf(u)
        outer_inv = invgamma.pdf(invgamma.ppf(norm.cdf(u),
38 39 40 41 42 43
                                              self._alpha,
                                              scale=self._q),
                                 self._alpha, scale=self._q)
        # FIXME
        outer_inv = np.clip(outer_inv, 1e-20, None)
        outer = 1/outer_inv
Martin Reinecke's avatar
bug fix  
Martin Reinecke committed
44 45
        grad = Field.from_local_data(self.position['points'].domain,
                                     inner*outer)
Martin Reinecke's avatar
Martin Reinecke committed
46 47
        grad = makeOp(MultiField.from_dict({"points": grad},
                                           self.position._domain))
48 49 50 51
        return SelectionOperator(grad.target, 'points')*grad

    @staticmethod
    def IG(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
52 53
        foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
        return Field.from_local_data(field.domain, foo)
54

Martin Reinecke's avatar
Martin Reinecke committed
55
    # MR FIXME: is this function needed?
56 57
    @staticmethod
    def IG_prime(field, alpha, q):
Martin Reinecke's avatar
Martin Reinecke committed
58
        inner = norm.pdf(field.local_data)
Martin Reinecke's avatar
Martin Reinecke committed
59 60
        outer = invgamma.pdf(invgamma.ppf(norm.cdf(field.local_data), alpha,
                                          scale=q), alpha, scale=q)
61 62
        # # FIXME
        # outer = np.clip(outer, 1e-20, None)
Martin Reinecke's avatar
Martin Reinecke committed
63
        return Field.from_local_data(field.domain, inner/outer)
64

Martin Reinecke's avatar
Martin Reinecke committed
65
    # MR FIXME: why does this take an np.ndarray instead of a Field?
66 67 68 69 70 71
    @staticmethod
    def inverseIG(u, alpha, q):
        res = norm.ppf(invgamma.cdf(u, alpha, scale=q))
        # # FIXME
        # res = np.clip(res, 0, None)
        return res