noise_energy.py 2.33 KB
Newer Older
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.

Martin Reinecke's avatar
Martin Reinecke committed
19
from ..field import Field, exp
Philipp Arras's avatar
Philipp Arras committed
20
from ..minimization.energy import Energy
Philipp Arras's avatar
Philipp Arras committed
21
from ..operators.diagonal_operator import DiagonalOperator
Martin Reinecke's avatar
Martin Reinecke committed
22
import numpy as np
23

24
25

class NoiseEnergy(Energy):
Philipp Arras's avatar
Philipp Arras committed
26
    def __init__(self, position, alpha, q, res_sample_list):
Martin Reinecke's avatar
Martin Reinecke committed
27
        super(NoiseEnergy, self).__init__(position)
28

Martin Reinecke's avatar
Martin Reinecke committed
29
        self.N = DiagonalOperator(exp(position))
30
31
        self.alpha = alpha
        self.q = q
Philipp Arras's avatar
Philipp Arras committed
32
        self.res_sample_list = res_sample_list
Philipp Arras's avatar
Philipp Arras committed
33
34
35

        self._gradient = None

Martin Reinecke's avatar
Martin Reinecke committed
36
        for s in res_sample_list:
Philipp Arras's avatar
Philipp Arras committed
37
38
            lh = .5 * s.vdot(self.N.inverse_times(s))
            grad = -.5 * self.N.inverse_times(s.conjugate()*s)
Philipp Arras's avatar
Philipp Arras committed
39
40
            if self._gradient is None:
                self._value = lh
Martin Reinecke's avatar
Martin Reinecke committed
41
                self._gradient = grad.copy()
Philipp Arras's avatar
Philipp Arras committed
42
43
44
45
            else:
                self._value += lh
                self._gradient += grad

Martin Reinecke's avatar
Martin Reinecke committed
46
47
        expmpos = exp(-position)
        self._value *= 1./len(self.res_sample_list)
Martin Reinecke's avatar
Martin Reinecke committed
48
49
50
51
52
        possum = position.sum()
        s1 = (alpha-1.)*possum if np.isscalar(alpha) \
            else (alpha-1.).vdot(position)
        s2 = q*expmpos.sum() if np.isscalar(q) else q.vdot(expmpos)
        self._value += .5*possum + s1 + s2
Philipp Arras's avatar
Philipp Arras committed
53

Martin Reinecke's avatar
Martin Reinecke committed
54
55
        self._gradient *= 1./len(res_sample_list)
        self._gradient += (alpha-0.5) - q*expmpos
Martin Reinecke's avatar
Martin Reinecke committed
56
        self._gradient.lock()
57
58

    def at(self, position):
Martin Reinecke's avatar
Martin Reinecke committed
59
60
        return self.__class__(position, self.alpha, self.q,
                              self.res_sample_list)
61
62

    @property
Martin Reinecke's avatar
Martin Reinecke committed
63
64
    def value(self):
        return self._value
65
66

    @property
Martin Reinecke's avatar
Martin Reinecke committed
67
68
    def gradient(self):
        return self._gradient