def __init__(self, d, l): self.d = d self.l = l [self.smolyak_points, self.smolyak_indices] = smolyak_grids(d, l) self.u_grid = self.smolyak_points.T self.isup = max(max(self.smolyak_indices)) self.n_points = len(self.smolyak_points) # self.grid = self.real_gri Ts = chebychev(self.smolyak_points.T, self.n_points - 1) ket = [] for comb in self.smolyak_indices: p = reduce(mul, [Ts[comb[i], i, :] for i in range(self.d)]) ket.append(p) ket = np.row_stack(ket) self.__ket__ = ket self.__Ts__ = Ts self.bounds = np.row_stack([(0, 1)] * d)
def __init__(self,d,l): self.d = d self.l = l [self.smolyak_points, self.smolyak_indices] = smolyak_grids(d,l) self.u_grid = self.smolyak_points.T self.isup = max(max(self.smolyak_indices)) self.n_points = len(self.smolyak_points) #self.grid = self.real_gri Ts = chebychev( self.smolyak_points.T, self.n_points - 1 ) ket = [] for comb in self.smolyak_indices: p = reduce( mul, [Ts[comb[i],i,:] for i in range(self.d)] ) ket.append(p) ket = np.row_stack( ket ) self.__ket__ = ket self.__Ts__ = Ts self.bounds = np.row_stack([(0,1)]*d)
def interpolate(self, x, with_derivative=True, with_theta_deriv=False): # points in x must be stacked horizontally theta = self.theta [n_v, n_t] = theta.shape assert n_t == self.n_points n = theta.shape[1] - 1 [n_d, n_p] = x.shape n_obs = n_p # by def assert n_d == self.d s = x Ts = chebychev(s, self.n_points - 1) ket = [] for comb in self.smolyak_indices: p = reduce(mul, [Ts[comb[i], i, :] for i in range(self.d)]) ket.append(p) ket = np.row_stack(ket) val = np.dot(theta, ket) # if with_derivative: # bounds = self.bounds # bounds_delta = bounds[1,:] - bounds[0,:] # # derivative w.r.t. to theta # l = [] # for i in range(n_v): # block = np.zeros( (n_v,n_t,n_obs) ) # block[i,:,:] = ket # l.append(block) # dval = np.concatenate( l, axis = 1 ) # # derivative w.r.t. arguments Us = chebychev2(s, self.n_points - 2) Us = np.concatenate([np.zeros((1, n_d, n_obs)), Us], axis=0) for i in range(Us.shape[0]): Us[i, :, :] = Us[i, :, :] * i der_s = np.zeros((n_t, n_d, n_obs)) for i in range(n_d): # BB = Ts.copy() # BB[:,i,:] = Us[:,i,:] el = [] for comb in self.smolyak_indices: # p = reduce( mul, [BB[comb[j],j,:] for j in range(self.d)] ) p = reduce(mul, [(Ts[comb[j], j, :] if i != j else Us[comb[j], j, :]) for j in range(self.d)]) el.append(p) el = np.row_stack(el) der_s[:, i, :] = el dder = np.tensordot(theta, der_s, (1, 0)) if with_theta_deriv: # derivative w.r.t. to theta l = [] for i in range(n_v): block = np.zeros((n_v, n_t, n_obs)) block[i, :, :] = ket l.append(block) dval = np.concatenate(l, axis=1) return [val, dder, dval] else: return [val, dder] else: return val
def interpolate(self, x, with_derivative=True, with_theta_deriv=False): # points in x must be stacked horizontally theta = self.theta [n_v, n_t] = theta.shape assert( n_t == self.n_points ) n = theta.shape[1] - 1 [n_d, n_p] = x.shape n_obs = n_p # by def assert( n_d == self.d ) s = x Ts = chebychev( s, self.n_points - 1 ) ket = [] for comb in self.smolyak_indices: p = reduce( mul, [Ts[comb[i],i,:] for i in range(self.d)] ) ket.append(p) ket = np.row_stack( ket ) val = np.dot(theta,ket) # if with_derivative: # bounds = self.bounds # bounds_delta = bounds[1,:] - bounds[0,:] # # derivative w.r.t. to theta # l = [] # for i in range(n_v): # block = np.zeros( (n_v,n_t,n_obs) ) # block[i,:,:] = ket # l.append(block) # dval = np.concatenate( l, axis = 1 ) # # derivative w.r.t. arguments Us = chebychev2( s, self.n_points - 2 ) Us = np.concatenate([np.zeros( (1,n_d,n_obs) ), Us],axis=0) for i in range(Us.shape[0]): Us[i,:,:] = Us[i,:,:] * i der_s = np.zeros( ( n_t, n_d, n_obs ) ) for i in range(n_d): #BB = Ts.copy() #BB[:,i,:] = Us[:,i,:] el = [] for comb in self.smolyak_indices: #p = reduce( mul, [BB[comb[j],j,:] for j in range(self.d)] ) p = reduce( mul, [ (Ts[comb[j],j,:] if i!=j else Us[comb[j],j,:]) for j in range(self.d)] ) el.append(p) el = np.row_stack(el) der_s[:,i,:] = el dder = np.tensordot( theta, der_s, (1,0) ) if with_theta_deriv: # derivative w.r.t. to theta l = [] for i in range(n_v): block = np.zeros( (n_v,n_t,n_obs) ) block[i,:,:] = ket l.append(block) dval = np.concatenate( l, axis = 1 ) return [val,dder,dval] else: return [val,dder] else: return val