ensemble_likelihood.py 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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-2017 Max-Planck-Society
#
# IMAGINE is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

19 20
import numpy as np

21
from nifty import Field
22

23
from imagine.likelihoods.likelihood import Likelihood
24

25 26

class EnsembleLikelihood(Likelihood):
27
    def __init__(self, observable_name,  measured_data,
28
                 data_covariance, profile=None):
29
        self.observable_name = observable_name
30
        self.measured_data = self._strip_data(measured_data)
31 32 33
        if isinstance(data_covariance, Field):
            data_covariance = data_covariance.val.get_full_data()
        self.data_covariance = data_covariance
34
        self.use_determinant = True
35 36

    def __call__(self, observable):
37 38 39
        field = observable[self.observable_name]
        return self._process_simple_field(field,
                                          self.measured_data,
40
                                          self.data_covariance)
41

42
    def _process_simple_field(self, observable, measured_data,
43
                              data_covariance):
44 45 46
        # Theo's scheme of inversing matrices now modifined by Jiaxin
        # use numpy.linalg.solve(A,b) to get inv(A)*b
        # thus safely avoid any possible difficulties brought by inversing matrices
Theo Steininger's avatar
Theo Steininger committed
47
        data_covariance = data_covariance.copy()
48
        k = observable.shape[0]
49
        n = observable.shape[1]
50 51

        obs_val = observable.val.get_full_data()
52
        obs_mean = observable.ensemble_mean().val.get_full_data()
53

54
        U = obs_val - obs_mean
55 56 57 58 59 60 61 62 63 64 65
        # OAS calculation modifined by Jiaxin
        # emperical S
        S = np.vdot(U,U)/k
        # trace of S
        TrS = np.trace(S)
        # trace of S^2
        TrS2 = np.trace(np.dot(S,S))
        
        numerator = (1.-2./n)*TrS2 + TrS**2
        denominator = (k+1.-2./n)*(TrS2-(TrS**2)/n)
        
66 67 68
        if denominator == 0:
            rho = 1
        else:
69
            rho = np.min([1, numerator/denominator])
70
            
71 72
        self.logger.debug("rho: %f = %f / %f" % (rho, numerator, denominator))

73 74 75 76
        # total covariance
        AC = data_covariance + (1-rho)*S + np.eye(n)*rho*TrS/n
        # obs - mean(sim)
        dc = measured_data - obs_mean
77 78
        # If the data was incomplete, i.e. contains np.NANs, set those values
        # to zero.
79 80 81 82 83
        dc = np.nan_to_num(dc)
        # calculate likelihood
        (sign_AC, logdet_AC) = np.linalg.slogdet(AC*2.*np.pi)
        result = -0.5*(dc*np.linalg.solve(AC,dc)) - 0.5*sing_AC*logdet_AC
        
84
        return result