Exemplo n.º 1
0
    def __init__(self, pca, eparams):
        '''
        Provide the emulation products.

        :param pca: object storing the principal components, the eigenpsectra
        :type pca: PCAGrid
        :param eparams: Optimized GP hyperparameters.
        :type eparams: 1D np.array
        '''

        self.pca = pca
        self.lambda_xi = eparams[0]
        self.h2params = eparams[1:].reshape(self.pca.m, -1) ** 2

        # Determine the minimum and maximum bounds of the grid
        self.min_params = np.min(self.pca.gparams, axis=0)
        self.max_params = np.max(self.pca.gparams, axis=0)

        # self.eigenspectra = self.PCAGrid.eigenspectra
        self.dv = self.pca.dv
        self.wl = self.pca.wl

        self.iPhiPhi = (1. / self.lambda_xi) * np.linalg.inv(skinny_kron(self.pca.eigenspectra, self.pca.M))

        self.V11 = self.iPhiPhi + Sigma(self.pca.gparams, self.h2params)

        self._params = None  # Where we want to interpolate

        self.V12 = None
        self.V22 = None
        self.mu = None
        self.sig = None
Exemplo n.º 2
0
    def lnprob(p, fmin=False):
        '''
        :param p: Gaussian Processes hyper-parameters
        :type p: 1D np.array

        Calculate the lnprob using Habib's posterior formula for the emulator.
        '''

        # We don't allow negative parameters.
        if np.any(p < 0.):
            if fmin:
                return 1e99
            else:
                return -np.inf

        lambda_xi = p[0]
        hparams = p[1:].reshape((my_pca.m, -1))

        # Calculate the prior for parname variables
        # We have two separate sums here, since hparams is a 2D array
        # hparams[:, 0] are the amplitudes, so we index i+1 here
        lnpriors = 0.0
        for i in range(0, len(Starfish.parname)):
            lnpriors += np.sum(Glnprior(hparams[:, i + 1], *priors[i]))

        h2params = hparams**2
        #Fold hparams into the new shape
        Sig_w = Sigma(my_pca.gparams, h2params)

        C = (1. / lambda_xi) * PhiPhi + Sig_w

        sign, pref = np.linalg.slogdet(C)

        central = my_pca.w_hat.T.dot(np.linalg.solve(C, my_pca.w_hat))

        lnp = -0.5 * (pref + central +
                      my_pca.M * my_pca.m * np.log(2. * np.pi)) + lnpriors

        # Negate this when using the fmin algorithm
        if fmin:
            print("lambda_xi", lambda_xi)
            for row in hparams:
                print(row)
            print()
            print(lnp)

            return -lnp
        else:
            return lnp