def create_Lambda(self,f,sigma): N = self.N m = self.m sample_points, weights = np.polynomial.hermite.hermgauss(self.n_gausshermite_sample_points) ''' Diagonal matrix ''' Lambda_diagonal = [0]*N constant = 1/(m*(sigma**2)) for i in range(N): if not self.is_pseudobs(i): Lambda_diagonal[i] = -constant*self.sum_Phi(i,2,f,sigma) #Note that sum_Phi used so minus sign needed! else: latest_x_index = np.max([ind for ind in self.obs_indices if ind < i]) Delta_i = (f[i]-f[latest_x_index])/sigma Lambda_diagonal[i] = 0.5*constant*Delta_i*var2_normal_pdf(Delta_i) Lambda_diagonal = np.diag(Lambda_diagonal) ''' Upper triangular off-diagonal matrix''' Lambda_uppertri = np.zeros((N,N)) for row_ind in range(N): if not self.is_pseudobs(row_ind): for col_ind in range(row_ind+1,row_ind+m+1): if self.is_pseudobs(col_ind): Delta = (f[col_ind]-f[row_ind])/sigma Lambda_uppertri[row_ind,col_ind] = -0.5*constant*Delta*var2_normal_pdf(Delta) ''' Final Lambda is sum of diagonal + upper_tringular + lower_tringular ''' Lambda = Lambda_diagonal + Lambda_uppertri + Lambda_uppertri.T return Lambda
def sum_Phi(self,i,order_of_derivative,f,sigma,sample_points=None, weights=None): ''' Auxiliary function that computes summation of Phi-function values i = index of x observation, i.e. some element of list 'obs_indices' order_of_derivative = how many this the c.d.f of the standard normal (Phi) is differentiated? f = vector of f-values ''' m = self.m #Delta_i_j = (f[i+j+1]-f[i])/(sigma_) Delta = f[i+1:i+m+1] Delta = (Delta - f[i])/sigma sum_=0 if order_of_derivative==0: for j in range(0,m): #sum_ = sum_ + integrate(lambda x: std_normal_cdf(Delta[j]+x)*std_normal_pdf(x), -np.inf, np.inf)[0] #Do integration by using Gaussian-Hermite quadrature: sum_ = sum_ + (1/np.sqrt(np.pi))*np.dot(weights,std_normal_cdf(Delta[j]-np.sqrt(2)*sample_points)) #Do to change of variables to get integteragl into the form int exp(x^2)*....dx. This is why np.sqrt(2) return(sum_) if order_of_derivative==1: for j in range(0,m): sum_ = sum_ + float(var2_normal_pdf(Delta[j])) return(sum_) if order_of_derivative==2: for j in range(0,m): sum_ = sum_ - 0.5*Delta[j]*float(var2_normal_pdf(Delta[j])) return(sum_) else: print("The derivatives of an order higher than 2 are not needed!") return None
def sum_Phi(self, i, order_of_derivative, f, sigma, sample_points=None, weights=None): ''' Auxiliary function that computes summation of Phi-function values i = index of x observation, i.e. some element of list 'obs_indices' dimensionality of the output depends on order_of_derivative order_of_derivative = how many this the c.d.f of the standard normal (Phi) is differentiated? f = vector of f-values ''' m = self.m phi_X = self.phi_X #Delta_i_j = (f[i+j+1]-f[i])/(sigma_) Delta = f[i + 1:i + m + 1] Delta = (Delta - f[i]) / sigma if order_of_derivative == 0: sum_ = 0 for j in range(0, m): sum_ = sum_ + (1 / np.sqrt(np.pi)) * np.dot( weights, std_normal_cdf(Delta[j] - np.sqrt(2) * sample_points) ) #Do to change of variables to get integteragl into the form int exp(x^2)*....dx. This is why np.sqrt(2) return (sum_) if order_of_derivative == 1: sum_ = np.zeros(self.nFeatures) for j in range(0, m): sum_ = sum_ + (phi_X[:, i + 1 + j] - phi_X[:, i]) * float( var2_normal_pdf(Delta[j])) return (sum_) if order_of_derivative == 2: sum_ = np.zeros(self.nFeatures) for j in range(0, m): sum_ = sum_ - ( (phi_X[:, i + 1 + j] - phi_X[:, i])** 2) * 0.5 * Delta[j] * float(var2_normal_pdf(Delta[j])) return (sum_) else: print("The derivatives of an order higher than 2 are not needed!") return None
def T_grad(self,f,theta,Sigma_inv_=None): if Sigma_inv_ is None: Sigma_inv_=self.Sigma_inv N = self.N m = self.m latest_obs_indices = self.latest_obs_indices beta = np.zeros((N,1)).reshape(N,) Phi_der_vec_ = np.array([float(var2_normal_pdf((f[j]-f[latest_obs_indices[j]])/theta[0])) for j in self.pseudobs_indices]) sum_Phi_der_vec_ = self.sum_Phi_vec(1,f,theta[0]) beta[self.obs_indices] = sum_Phi_der_vec_/(theta[0]*m) beta[self.pseudobs_indices] = -Phi_der_vec_/(theta[0]*m) T_grad = -Sigma_inv_.dot(f).reshape(N,) + beta.reshape(N,) return T_grad