There is a maintenance of MPCDF Gitlab on Thursday, April 22st 2020, 9:00 am CEST - Expect some service interruptions during this time

poissonian_energy.py 1.99 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
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

Philipp Arras's avatar
Philipp Arras committed
21
from numpy import inf, isnan
Philipp Arras's avatar
Philipp Arras committed
22 23

from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
24 25
from ..minimization.energy import Energy
from ..operators.sandwich_operator import SandwichOperator
Philipp Arras's avatar
Philipp Arras committed
26 27 28
from ..sugar import log, makeOp


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

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

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

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

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

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

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