poissonian_energy.py 1.9 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.

Philipp Arras's avatar
Philipp Arras committed
19 20
from numpy import inf, isnan

Martin Reinecke's avatar
Martin Reinecke committed
21 22
from ..minimization.energy import Energy
from ..operators.sandwich_operator import SandwichOperator
Philipp Arras's avatar
Philipp Arras committed
23 24 25
from ..sugar import log, makeOp


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

        value = 0.5 * s.vdot(s), i.e. a log-Gauss distribution with unit
        covariance
        """
34
        super(PoissonianEnergy, self).__init__(lamb.position)
35
        self._lamb = lamb
Philipp Arras's avatar
Philipp Arras committed
36 37 38 39 40 41 42
        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
43
        self._gradient = self._lamb.jacobian.adjoint_times(1 - d/lamb_val)
Philipp Arras's avatar
Philipp Arras committed
44 45

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

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

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

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

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