Esempio n. 1
0
    def _moments_match_analytical(self,data_i,tau_i,v_i):
        """
        Moments match of the marginal approximation in EP algorithm

        :param i: number of observation (int)
        :param tau_i: precision of the cavity distribution (float)
        :param v_i: mean/variance of the cavity distribution (float)
        """
        if data_i == 1:
            sign = 1.
        elif data_i == 0:
            sign = -1
        else:
            raise ValueError("bad value for Bernouilli observation (0,1)")
        if isinstance(self.gp_link,gp_transformations.Probit):
            z = sign*v_i/np.sqrt(tau_i**2 + tau_i)
            Z_hat = std_norm_cdf(z)
            phi = std_norm_pdf(z)
            mu_hat = v_i/tau_i + sign*phi/(Z_hat*np.sqrt(tau_i**2 + tau_i))
            sigma2_hat = 1./tau_i - (phi/((tau_i**2+tau_i)*Z_hat))*(z+phi/Z_hat)

        elif isinstance(self.gp_link,gp_transformations.Heaviside):
            a = sign*v_i/np.sqrt(tau_i)
            Z_hat = std_norm_cdf(a)
            N = std_norm_pdf(a)
            mu_hat = v_i/tau_i + sign*N/Z_hat/np.sqrt(tau_i)
            sigma2_hat = (1. - a*N/Z_hat - np.square(N/Z_hat))/tau_i
            if np.any(np.isnan([Z_hat, mu_hat, sigma2_hat])):
                stop
        else:
            raise ValueError("Exact moment matching not available for link {}".format(self.gp_link.gp_transformations.__name__))

        return Z_hat, mu_hat, sigma2_hat
Esempio n. 2
0
 def dtransf_df(self, z):
     if type(z) == type(np.ones(1)):
         z_shape = np.shape(z)
         z = np.reshape(z, (1, -1))[0]
         output = np.array(map(lambda z_in: std_norm_pdf(self.copula(z_in)) * self.dcopula_dz(z_in), z))
         output = np.reshape(output, z_shape)
     else:
         output = std_norm_pdf(self.copula(z)) * self.dcopula_dz(z)
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 3
0
 def dtransf_df(self, z):
     if type(z) == type(np.ones(1)):
         z_shape = np.shape(z)
         z = np.reshape(z, (1, -1))[0]
         output = np.array(
             map(
                 lambda z_in: std_norm_pdf(self.copula(z_in)) * self.
                 dcopula_dz(z_in), z))
         output = np.reshape(output, z_shape)
     else:
         output = std_norm_pdf(self.copula(z)) * self.dcopula_dz(z)
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 4
0
 def dcopula_dk(self, z):
     output = self.marginal.dquantile_dcdf(std_norm_cdf(
         z / np.sqrt(self.k))) * (std_norm_pdf(
             z / np.sqrt(self.k))) * -0.5 * z * (self.k**(-1.5))
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 5
0
 def pdf(self, f):
     input = (f - self.mu_vec) / (self.sigma_vec)
     output = np.dot(
         self.k_vec,
         np.reshape(std_norm_pdf(input) / self.sigma_vec, (1, -1))[0])
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 6
0
 def dcdf_dtheta(self, f):
     # FIXME: Limit to only mu_vec at the moment.
     input = (f - self.mu_vec) / (self.sigma_vec)
     output = -1. * std_norm_pdf(input) / (self.sigma_vec * self.k_vec)
     output = np.reshape(output, (1, -1))[0]
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 7
0
 def dcdf_dtheta(self, f):
     # FIXME: Limit to only mu_vec at the moment.
     input = (f - self.mu_vec)/(self.sigma_vec)
     output = -1. * std_norm_pdf(input)/(self.sigma_vec * self.k_vec)
     output = np.reshape(output, (1, -1))[0]
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 8
0
 def dcopula_dz(self, z):
     output = self.marginal.dquantile_dcdf(std_norm_cdf(z)) * std_norm_pdf(z)
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 9
0
 def d3transf_df3(self,f):
     shiftedf = (f-self.offset)*self.scale    
     return (safe_square(shiftedf)-1.)*std_norm_pdf(shiftedf)
Esempio n. 10
0
 def d2transf_df2(self,shiftedf):
     shiftedf = (f-self.offset)*self.scale
     return -f * std_norm_pdf(shiftedf)
Esempio n. 11
0
 def dtransf_df(self,f):
     shiftedf = (f-self.offset)*self.scale
     return std_norm_pdf(shiftedf)
Esempio n. 12
0
 def d2transf_df2(self,f):
     #FIXME
     return -f * std_norm_pdf(f)
Esempio n. 13
0
 def dtransf_df(self,f):
     return std_norm_pdf(f)
Esempio n. 14
0
 def dtransf_dk(self, z):
     ''' f being z'''
     output = std_norm_pdf(self.copula(z)) * self.dcopula_dk(z)
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 15
0
 def pdf(self, f):
     output = std_norm_pdf((f - self.mu) / self.sigma) / self.sigma
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 16
0
 def dcopula_dk(self, z):
     output = self.marginal.dquantile_dcdf(std_norm_cdf(z/np.sqrt(self.k))) * (std_norm_pdf(z/np.sqrt(self.k))) * -0.5 * z * (self.k ** (-1.5))
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 17
0
 def pdf(self, f):
     output = std_norm_pdf((f - self.mu)/self.sigma)/self.sigma
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 18
0
 def pdf(self, f):
     input = (f - self.mu_vec)/(self.sigma_vec)
     output = np.dot(self.k_vec, np.reshape(std_norm_pdf(input)/self.sigma_vec, (1, -1))[0])
     assert not(np.isnan(output).any()), 'function returns NaN'
     assert not(np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 19
0
 def dtransf_dk(self, z):
     ''' f being z'''
     output = std_norm_pdf(self.copula(z)) * self.dcopula_dk(z)
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output
Esempio n. 20
0
 def dcopula_dz(self, z):
     output = self.marginal.dquantile_dcdf(
         std_norm_cdf(z)) * std_norm_pdf(z)
     assert not (np.isnan(output).any()), 'function returns NaN'
     assert not (np.isinf(output).any()), 'function returns inf'
     return output