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
25


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

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

    @property
    @memo
    def curvature(self):
38
39
       return (my_sum(map(lambda v: v.curvature, self._energy_list)) *
               (1./len(self._energy_list)))