Esempio n. 1
0
 def gen_LDS(self):
     self.log.info('forming LDS model')
     # form the state space representation
     A = np.hstack([
         self.Phi_inv * outer(self.phi, self.phi, F) for F in self.F
     ])
     # this is [A_1 A_2 .. A_p; I 0]
     I = np.eye(len(self.phi)*(self.p-1))
     O = np.zeros((len(self.phi)*(self.p-1), len(self.phi)))
     A = np.vstack([A, np.hstack([I, O])])
     if self.G:
         B = np.hstack([
             self.Phi_inv * outer(self.phi, self.phi, G) 
             for G in self.G
         ])
         # this is [B_1 B_2 .. B_q; I 0]
         O = np.zeros((len(self.phi)*(self.p-1), self.q*len(self.phi)))
         B = np.vstack([B, O])
     else:
         B = None
     # C = <H,phi>(s1 ... sn)
     # <H,phi> = a^T <psi, phi>
     Cop = np.array([inner(self.H, phi_i) for phi_i in self.phi])
     # So we need to go through each basis function in Cop (C-operator) and
     # evalutate it at each observation location. This begs the question of
     # where the observation locations should come into the program. For 
     # the moment, though, we'll assume them fixed and store them in the 
     # HIDEX object
     C = np.zeros((len(self.obs_locns),len(self.phi)*(self.p)))
     for i,s in enumerate(self.obs_locns):
         for j,basis_function in enumerate(Cop):
             C[i,j] = basis_function(s)
     # make the covariance matrices
     Sw = np.zeros(
         (len(self.phi)*(self.p), len(self.phi)*(self.p))
         ,dtype=float
     )
     for i, basis_i in enumerate(self.phi):
         for j, basis_j in enumerate(self.phi):
             Sw[i,j] = inner(basis_i, basis_j, self.Q)
     # form the initial state
     x0 = np.hstack([self.f0.weights,np.zeros((len(self.phi)*(self.p-1)))])
     # turn everything into matrices for the LDS model
     A = np.matrix(A)
     B = np.matrix(B)
     C = np.matrix(C)
     Sw = np.matrix(Sw)
     Sv = np.matrix(self.R)
     x0 = np.matrix(x0).T
     Pi0 = np.matrix(self.Pi0)
     # make a state space model
     return LDS.LDS(A, B, C, Sw, Sv, x0, Pi0)
Esempio n. 2
0
region = 1

# change the type of img to list
lop = []  # list of points [ra,dec,v]
for i in range(s[2]):
    for j in range(s[1]):
        for k in range(s[0]):
            if img[k, j, i] > I_th:
                lop.append([i, j, k])

#''' create the range of indices i,j,k in the given region
ind = []
for i in range(-int(r), int(r) + 1):
    for j in range(-int(r), int(r) + 1):
        for k in range(-int(r * dv), int(r * dv) + 1):
            if inner.inner(0, 0, 0, i, j, k, r, dv, 1):
                ind.append([i, j, k])
ind = np.asarray(ind)
#'''

# ================================== fof method: find out the continuums ===================================

iso = []
grp = []
L = len(lop)
while not lop == []:
    next = []
    next.append(lop[0])
    lop.pop(0)
    p = 0
    while p < len(next):
Esempio n. 3
0
 def estimate_kernels(self, f, g, Y):
     """
     Parameters
     ==========
     f : list
             list of hidden fields
     g : list
             list of input fields
     Y : array
             data
     """
     self.log.info('estimating kernels')
     # treat psi as a kernel with unit weights
     psi = Kernel(bases=self.psi, weights=np.ones(len(self.psi)))
     # then define an operator P_op based on that kernel
     P_op = lambda f: inner(psi, f)
     # initialise gammas
     gamma_a = np.empty(self.p)
     gamma_b = np.empty(self.q)
     Gamma_a = np.empty((self.p, self.p))
     Gamma_b = np.empty((self.q, self.q))
     Gamma_ab = np.empty((self.p, self.q))
     # form the time sequence
     time = range(len(f))
     # form gammas
     for t in time[1:]:
         for i in range(self.p):
             gamma_a[i] += inner(
                 f[t], 
                 P_op(f[t-i]), 
                 Qinv
             )
     
     for t in time[1:]:
         for j in range(self.q):
             gamma_b[j] += inner(
                 f[t], 
                 P_op(f[t-i]), 
                 Qinv
             )
     
     for t in time[self.p:]:
         gamma_c = inner(y[t], P_op(f[t]), Rinv)
         for i in range(self.p):
             for idash in range(self.p):
                 Gamma_a[i, idash] += inner(
                     P_op(f[t-i]), 
                     P_op(f[t-idash]), 
                     Qinv
                 )
         
     for t in time[self.q:]:
         for j in range(self.q):
             for jdash in range(self.q):
                 Gamma_b[j, jdash] += inner(
                     P_op(g[t-j]), 
                     P_op(g[t-jdash]), 
                     Qinv
                 )
         
     for t in time[max(self.q,self.p):]:
         for i in range(self.p):
             for j in range(self.q):
                 Gamma_ab[i, j] += inner(
                     P_op(f[t-i]), 
                     P_op(f[t-j]), 
                     Qinv
                 )
         
     for t in time:
         Gamma_c += inner(
             P_op(f[t]), 
             P_op(f[t]), 
             Rinv
         )
     
     # turn these into the large matrices
     ga = np.hstack(gamma_a)
     gb = np.hstack(gamma_b)
     # quick function to horizontally then vertically stack arrays
     form_block_matrix = lambda A: np.vstack([np.hstack(a) for a in A])
     Ga = form_block_matrix(Gamma_a)
     Gb = form_block_matrix(Gamma_b)
     Gab = form_block_matrix(Gamma_ab)
     # prepare some pretty syntax for the zeros matrix
     O = lambda m, n: zeros((m, n))
     na = self.p*len(self.psi)
     nb = self.q*len(self.psi)
     nc = len(gamma_c)
     # form the final large matrices
     gamma = np.hstack([ga, gb, gamma_c])
     Gamma = form_block_matrix([
         [Ga,        Gab,        O(na, nc)],
         [Gab.T,     Gb,         O(nb, nc)],
         [O(nc, na), O(nc, nb),  Gamma_c]
     ])
     # perform the maximisation
     theta = 0.5 * Gamma.I * gamma
     # unpick the parameters
     theta_l = array(theta).flatten().tolist()
     nx = len(self.psi)
     # watch carefully!
     # a is composed of the first p*nx elements of the list theta_l
     # so we pop these from the list. Then reshape them into an array
     # where each column of this array corresponds to a_i
     a = pb.array([
         theta_l.pop(0) for i in range(self.p*nx)
     ]).reshape(nx, self.p)
     # similarly, b is composed of the first q*nx of the now shortened 
     # list! so we just do the same thing, pop the first q*nx elements and 
     # reshape
     b = pb.array([
         theta_l.pop(0) for i in range(self.q*nx)
     ]).reshape(nx, self.q)
     # finally, c is all that's left. So just make it into an array!
     c = pb.array(theta_l)
     # form the new kernels. We need to transpose a and b because they're
     # stored columnwise, i.e. a_i = a[:, i] and the "for" works row-wise
     for Fi, ai in zip(self.F, a.T):
         Fi.bases = ai
     for Gi, bi in zip(self.G, b.T):
         Gi.bases = bi
     self.H.bases = c