kl.py 1.08 KB
Newer Older
1
from builtins import *
Philipp Arras's avatar
Philipp Arras committed
2
from ..minimization.energy import Energy
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
3
from ..utilities import memo, my_sum
4
5
6


class SampledKullbachLeiblerDivergence(Energy):
7
    def __init__(self, h, res_samples):
8
9
10
11
12
13
14
15
        """
        h: Hamiltonian
        N: Number of samples to be used
        """
        super(SampledKullbachLeiblerDivergence, self).__init__(h.position)
        self._h = h
        self._res_samples = res_samples

16
17
        self._energy_list = tuple(h.at(self.position+ss)
                                  for ss in res_samples)
18
19

    def at(self, position):
20
        return self.__class__(self._h.at(position), self._res_samples)
21
22
23
24

    @property
    @memo
    def value(self):
25
26
        return (my_sum(map(lambda v: v.value, self._energy_list)) /
                len(self._energy_list))
27
28
29
30

    @property
    @memo
    def gradient(self):
31
32
        return (my_sum(map(lambda v: v.gradient, self._energy_list)) /
                len(self._energy_list))
33
34
35
36

    @property
    @memo
    def curvature(self):
Martin Reinecke's avatar
Martin Reinecke committed
37
38
        return (my_sum(map(lambda v: v.curvature, self._energy_list)) *
                (1./len(self._energy_list)))