poissonian_energy.py 1.98 KB
Newer Older
Philipp Arras's avatar
Philipp Arras 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 20
from __future__ import absolute_import, division, print_function
from ..compat import *
Philipp Arras's avatar
Philipp Arras committed
21
from numpy import inf, isnan
Martin Reinecke's avatar
Martin Reinecke committed
22 23
from ..minimization.energy import Energy
from ..operators.sandwich_operator import SandwichOperator
Philipp Arras's avatar
Philipp Arras committed
24 25 26
from ..sugar import log, makeOp


27
class PoissonianEnergy(Energy):
28
    def __init__(self, lamb, d):
Philipp Arras's avatar
Philipp Arras committed
29
        """
30
        lamb: Model object
Philipp Arras's avatar
Philipp Arras committed
31 32 33 34

        value = 0.5 * s.vdot(s), i.e. a log-Gauss distribution with unit
        covariance
        """
35
        super(PoissonianEnergy, self).__init__(lamb.position)
36
        self._lamb = lamb
Philipp Arras's avatar
Philipp Arras committed
37 38 39 40 41 42 43
        self._d = d

        lamb_val = self._lamb.value

        self._value = lamb_val.sum() - d.vdot(log(lamb_val))
        if isnan(self._value):
            self._value = inf
44
        self._gradient = self._lamb.jacobian.adjoint_times(1 - d/lamb_val)
Philipp Arras's avatar
Philipp Arras committed
45 46

        metric = makeOp(1./lamb_val)
Martin Reinecke's avatar
Martin Reinecke committed
47
        self._metric = SandwichOperator.make(self._lamb.jacobian, metric)
Philipp Arras's avatar
Philipp Arras committed
48 49

    def at(self, position):
50
        return self.__class__(self._lamb.at(position), self._d)
Philipp Arras's avatar
Philipp Arras committed
51 52 53 54 55 56 57 58 59 60

    @property
    def value(self):
        return self._value

    @property
    def gradient(self):
        return self._gradient

    @property
Martin Reinecke's avatar
Martin Reinecke committed
61 62
    def metric(self):
        return self._metric