<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Oct 10 17:27:49 2016

@author: camacho
"""
import Kernel;reload(Kernel);kl = Kernel

import numpy as np
import inspect
from time import time   

##### INITIAL MATRIX ########################################################## 
def build_matrix(kernel, x, y, yerr): #covariance matrix calculations
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x)) 
    return K


##### LIKELIHOOD ##############################################################
def likelihood(kernel, x, y, yerr): #covariance matrix calculations   
    from scipy.linalg import cho_factor, cho_solve    
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x))    
    L1 = cho_factor(K) # tuple (L, lower)
    sol = cho_solve(L1, y) # this is K^-1*(r)
    n = y.size
    logLike = -0.5*np.dot(y, sol) \
              - np.sum(np.log(np.diag(L1[0]))) \
              - n*0.5*np.log(2*np.pi)        
    return logLike    

def lnlike(K, y): #log-likelihood calculations
    from scipy.linalg import cho_factor, cho_solve
    L1 = cho_factor(K) # tuple (L, lower)
    sol = cho_solve(L1, y) # this is K^-1*(r)
    n = y.size
    logLike = -0.5*np.dot(y, sol) \
              - np.sum(np.log(np.diag(L1[0]))) \
              - n*0.5*np.log(2*np.pi)        
    return logLike


##### MEAN AND VARIANCE #######################################################
def compute_kernel(kernel, x, xcalc, y, yerr):    
    from scipy.linalg import cho_factor, cho_solve    
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x))             #covariance matrix
    L1 = cho_factor(K) # tuple (L, lower)
    sol = cho_solve(L1, y) # this is K^-1*(r)

    K_final=K
    
    new_r = xcalc[:, None] - x[None, :]    
    new_lines = kernel(new_r)
    K_final=np.vstack([K_final,new_lines])

    new_r = xcalc[:,None] - xcalc[None,:]
    new_columns = kernel(new_r)        
    K_columns = np.vstack([new_lines.T,new_columns])
    K_final=np.hstack([K_final,K_columns])

    y_mean=[] #mean = K*.K-1.y  
    for i in range(len(xcalc)):
        y_mean.append(np.dot(new_lines[i,:], sol))  #mean
    
    y_var=[] #var=  K** - K*.K-1.K*.T
    diag=np.diagonal(new_columns)
    for i in range(len(xcalc)):
        #k**=diag[i]; k*=new_lines[i]      
        a=diag[i]
        newsol = cho_solve(L1, new_lines[i]) # this is K-1.(K*)
        d=np.dot(new_lines[i,:],newsol)
        result=a-d      
        y_var.append(result)                        #variance

    y_std = np.sqrt(y_var)                          #standard deviation
    return [y_mean,y_std]


##### LIKELIHOOD GRADIENT #####################################################
def likelihood_aux(kernel, x, y, yerr): #covariance matrix calculations   
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x))    
    log_p_correct = lnlike(K, y)   
    return K

def grad_logp(kernel,x,y,yerr,cov_matrix): #covariance matrix of derivatives
    r = x[:, None] - x[None, :]
    K_grad = kernel(r)
    K_inv = np.linalg.inv(cov_matrix)    
    alpha = np.dot(K_inv,y)
    A = np.outer(alpha, alpha) - K_inv #this comes from george
    grad = 0.5 * np.einsum('ij,ij', K_grad, A) #this comes from george
    return grad 
<commit_msg>problem with the whitenoise fixed<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Oct 10 17:27:49 2016

@author: camacho
"""
import Kernel;reload(Kernel);kl = Kernel

import numpy as np
import inspect
from time import time   

##### INITIAL MATRIX ########################################################## 
def build_matrix(kernel, x, y, yerr): #covariance matrix calculations
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x)) 
    return K


##### LIKELIHOOD ##############################################################
def likelihood(kernel, x, y, yerr): #covariance matrix calculations   
    from scipy.linalg import cho_factor, cho_solve    
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x))    
    L1 = cho_factor(K) # tuple (L, lower)
    sol = cho_solve(L1, y) # this is K^-1*(r)
    n = y.size
    logLike = -0.5*np.dot(y, sol) \
              - np.sum(np.log(np.diag(L1[0]))) \
              - n*0.5*np.log(2*np.pi)        
    return logLike    

def lnlike(K, y): #log-likelihood calculations
    from scipy.linalg import cho_factor, cho_solve
    L1 = cho_factor(K) # tuple (L, lower)
    sol = cho_solve(L1, y) # this is K^-1*(r)
    n = y.size
    logLike = -0.5*np.dot(y, sol) \
              - np.sum(np.log(np.diag(L1[0]))) \
              - n*0.5*np.log(2*np.pi)        
    return logLike


##### MEANS AND VARIANCES #####################################################
def compute_kernel(kernel, x, xcalc, y, yerr):    
    from scipy.linalg import cho_factor, cho_solve    
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x)) #covariance matrix
    L1 = cho_factor(K) #tuple (L, lower)
    sol = cho_solve(L1, y) #this is K^-1*(r)

    K_final=K
    
    #Exceptions because of the White Noise diagonal problem
    if isinstance(kernel,kl.Sum) or isinstance(kernel,kl.Product):
        if  isinstance(kernel.k1,kl.WhiteNoise):
            print kernel.k1
            oldKernel=kernel
            kernel=kernel.k2
            new_r = xcalc[:, None] - x[None, :]   
            new_lines = kernel(new_r)
            K_final=np.vstack([K_final,new_lines])
            
            new_r = xcalc[:,None] - xcalc[None,:]
            new_columns = oldKernel(new_r)        
            K_columns = np.vstack([new_lines.T,new_columns])
            K_final=np.hstack([K_final,K_columns])
        
            y_mean=[] #mean = K*.K-1.y  
            for i in range(len(xcalc)):
                y_mean.append(np.dot(new_lines[i,:], sol))  #mean
            
            y_var=[] #var=  K** - K*.K-1.K*.T
            diag=np.diagonal(new_columns)
            for i in range(len(xcalc)):
                #k**=diag[i]; k*=new_lines[i]      
                a=diag[i]
                newsol = cho_solve(L1, new_lines[i]) #this is K-1.(K*)
                d=np.dot(new_lines[i,:],newsol)
                result=a-d      
                y_var.append(result) #variance  
        if isinstance(kernel.k2,kl.WhiteNoise):
            oldKernel=kernel
            kernel=kernel.k1
            new_r = xcalc[:, None] - x[None, :]   
            new_lines = kernel(new_r)
            K_final=np.vstack([K_final,new_lines])
            
            new_r = xcalc[:,None] - xcalc[None,:]
            new_columns = oldKernel(new_r)        
            K_columns = np.vstack([new_lines.T,new_columns])
            K_final=np.hstack([K_final,K_columns])
        
            y_mean=[] #mean = K*.K-1.y  
            for i in range(len(xcalc)):
                y_mean.append(np.dot(new_lines[i,:], sol)) #mean
            
            y_var=[] #var=  K** - K*.K-1.K*.T
            diag=np.diagonal(new_columns)
            for i in range(len(xcalc)):
                #k**=diag[i]; k*=new_lines[i]      
                a=diag[i]
                newsol = cho_solve(L1, new_lines[i]) # this is K-1.(K*)
                d=np.dot(new_lines[i,:],newsol)
                result=a-d      
                y_var.append(result) #variance
        else:
            new_r = xcalc[:, None] - x[None, :]   
            new_lines = kernel(new_r)
            K_final=np.vstack([K_final,new_lines])
            
            new_r = xcalc[:,None] - xcalc[None,:]
            new_columns = kernel(new_r)        
            K_columns = np.vstack([new_lines.T,new_columns])
            K_final=np.hstack([K_final,K_columns])
        
            y_mean=[] #mean = K*.K-1.y  
            for i in range(len(xcalc)):
                y_mean.append(np.dot(new_lines[i,:], sol))  #mean
            
            y_var=[] #var=  K** - K*.K-1.K*.T
            diag=np.diagonal(new_columns)
            for i in range(len(xcalc)):
                #k**=diag[i]; k*=new_lines[i]      
                a=diag[i]
                newsol = cho_solve(L1, new_lines[i]) #this is K-1.(K*)
                d=np.dot(new_lines[i,:],newsol)
                result=a-d      
                y_var.append(result) #variance
    
    #If there's no White Noise things are ok
    else:    
        new_r = xcalc[:, None] - x[None, :]   
        new_lines = kernel(new_r)
        K_final=np.vstack([K_final,new_lines])
        
        new_r = xcalc[:,None] - xcalc[None,:]
        new_columns = kernel(new_r)        
        K_columns = np.vstack([new_lines.T,new_columns])
        K_final=np.hstack([K_final,K_columns])
    
        y_mean=[] #mean = K*.K-1.y  
        for i in range(len(xcalc)):
            y_mean.append(np.dot(new_lines[i,:], sol)) #mean
        
        y_var=[] #var=  K** - K*.K-1.K*.T
        diag=np.diagonal(new_columns)
        for i in range(len(xcalc)):
            #k**=diag[i]; k*=new_lines[i]      
            a=diag[i]
            newsol = cho_solve(L1, new_lines[i]) #this is K-1.(K*)
            d=np.dot(new_lines[i,:],newsol)
            result=a-d      
            y_var.append(result) #variance

    y_std = np.sqrt(y_var) #standard deviation
    return [y_mean,y_std]


##### LIKELIHOOD GRADIENT #####################################################
def likelihood_aux(kernel, x, y, yerr): #covariance matrix calculations   
    r = x[:, None] - x[None, :]
    K = kernel(r)
    K = K + yerr**2*np.identity(len(x))    
    log_p_correct = lnlike(K, y)   
    return K

def grad_logp(kernel,x,y,yerr,cov_matrix): #covariance matrix of derivatives
    r = x[:, None] - x[None, :]
    K_grad = kernel(r)
    K_inv = np.linalg.inv(cov_matrix)    
    alpha = np.dot(K_inv,y)
    A = np.outer(alpha, alpha) - K_inv #this comes from george
    grad = 0.5 * np.einsum('ij,ij', K_grad, A) #this comes from george
    return grad 

{'fraction_non_alphanumeric': 0.19011826017237923, 'fraction_numerical': 0.010924032872319103, 'mean_word_length': 2.0923458320421444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6159228', 'n_tokens_mistral': 3730, 'n_tokens_neox': 3630, 'n_words': 713}