Ejemplo n.º 1
0
    def __init__(self, inpDim, hidDim):
        self.inpDim = inpDim  #number of input neurons (and output neurons)
        self.hidDim = hidDim  #number of hidden neurons

        self.inp = zeros((self.inpDim, 1))  #vector holding current input
        self.out = zeros((self.hidDim, 1))  #output neurons
        self.g = zeros((self.hidDim, 1))  #neural activity before non-linearity
        self.h = zeros((self.hidDim, 1))  #hidden neuron activation
        self.a = ones((self.hidDim, 1))  #slopes of activation functions
        self.b = -3 * ones((self.hidDim, 1))  #biases of activation functions
        scale = 0.025

        self.W = scale * (
            2 * rand((self.inpDim, self.hidDim)) - 0.5 * ones(
                (self.inpDim, self.hidDim))
        ) + scale  #shared network weights, i.e. used to compute hidden layer activations and estimated outputs
        print self.W.shape

        self.lrateRO = 0.01
        #learning rate for synaptic plasticity of read-out layer (RO)
        self.regRO = 0.0002
        #numerical regularization constant
        self.decayP = 0
        #decay factor for positive weights [0..1]
        self.decayN = 1
        #decay factor for negative weights [0..1]

        self.lrateIP = 0.001
        #learning rate for intrinsic plasticity (IP)
        self.meanIP = 0.2
Ejemplo n.º 2
0
    def neglnlikelihood(self):

        ye = copy.deepcopy(self.ye)

        # calculate mu_d
        dd = ye - self.rho * self.yc_xe
        # a = np.linalg.solve(self.UPsid_Xe.T,dd)
        # b = np.linalg.solve(self.UPsid_Xe,a)
        c = ones([self.ne,1]).T.dot(np.mat(self.Psid_Xe).I.dot(dd))

        # d = np.linalg.solve(self.UPsid_Xe.T, ones([self.ne,1]))
        # e = np.linalg.solve(self.UPsid_Xe,d)
        f = ones([self.ne,1]).T.dot(np.mat(self.Psid_Xe).I.dot(ones([self.ne,1])))

        self.mud = c/f

        # let's solve sigmaSqrd
        # notice that here yc is alist, it should be transfered to n*1 array

        d = ye - self.rho * self.yc_xe - ones([self.ne, 1]).dot(self.mud)
        # a = np.linalg.solve(self.UPsid_Xe.T, d)
        # b = np.linalg.solve(self.UPsid_Xe, a)

        self.sigmaSqrd = d.T.dot(np.mat(self.Psid_Xe).I.dot(d)) / self.ne
        # self.LnDetPsid = 2. * np.sum(np.log(np.abs(np.diag(self.UPsid_Xe))))

        self.LnDetPsid = np.log(np.abs(np.linalg.det(self.Psid_Xe)))

        self.NegLnLike = -1.*(-(self.ne/2.)*np.log(self.sigmaSqrd[0,0]) - 0.5*self.LnDetPsid)
Ejemplo n.º 3
0
 def inf(self, x, meanonly=False):
     x = np.asmatrix(x)
     if x.shape[1] != self.d:
         if x.shape[0] == self.d:
             x = x.T
         else:
             raise Exception('Invalid test-set dimension -- '
                             'expected d = ' + str(self.d) + '.')
     n = x.shape[0]
     # Handle empty test set
     if n == 0:
         return (np.zeros((0, 1)), np.zeros((0, 1)))
     ms = self.kernel.mean*np.ones((n, 1))
     Kbb = self.kernel(x, diag=True)
     # Handle empty training set
     if len(self) == 0:
         return (ms, np.asmatrix(Kbb))
     Kba = self.kernel(x, self.x)
     m = self.kernel.mean*np.ones((len(self), 1))
     fm = ms + Kba*scipy.linalg.cho_solve((self.L, True), self.y - m,
                                          overwrite_b=True)
     if meanonly:
         return fm
     else:
         W = scipy.linalg.cho_solve((self.L, True), Kba.T)
         fv = np.asmatrix(Kbb - np.sum(np.multiply(Kba.T, W), axis=0).T)
         # W = np.asmatrix(scipy.linalg.solve(self.L, Kba.T, lower=True))
         # fv = np.asmatrix(Kbb - np.sum(np.power(W, 2), axis=0).T)
         return (fm, fv)
Ejemplo n.º 4
0
def followup(A, b):
    """Thomas solver of A*x = b, where A is a tri-diagonal matrix

  Returns
  -------
  x : solution of A*x = b
  """
    n = A.shape[0]
    d = matlib.ones([n, 1])
    a = matlib.ones([n - 1, 1])
    c = matlib.ones([n - 1, 1])
    x = matlib.ones(n)
    for i in range(n - 1):
        a[i, 0] = A[i + 1, i]
        c[i, 0] = A[i, i + 1]
        d[i, 0] = A[i, i]
    d[n - 1, 0] = A[n - 1, n - 1]
    for i in range(1, n):
        ad = a[i - 1, 0] / d[i - 1, 0]
        d[i, 0] = d[i, 0] - ad * c[i - 1, 0]
        b[i, 0] = b[i, 0] - ad * b[i - 1, 0]
    x[0, n - 1] = b[n - 1, 0] / d[n - 1, 0]
    for i in range(n - 2, -1, -1):
        x[0, i] = (b[i, 0] - c[i, 0] * x[0, i + 1]) / d[i, 0]
    return x
Ejemplo n.º 5
0
def MakeWind(speedmean, speedstd, dirmean, dirstd, n):
  """
    Uses auto-regressive process to compute a set
    of wind x/y velocities. Returns a 2-item list where each
    item is a list of all the x/y velocities respectively.
  """
  N = 10
  phispeed = matlib.ones((1, N)) / N * 0.99
  phidir = matlib.ones((1, N)) / N * 0.99
  s0 = speedmean
  d0 = dirmean
  speeds = [s0]
  dirs = [d0]
  xs = []
  ys = []
  espeed = lambda: random.normal(speedmean, speedstd)
  edir = lambda: random.normal(dirmean, dirstd)
  for ii in range(1, n+1):
    Xspeed = matlib.zeros(phispeed.shape).T
    Xdir = matlib.zeros(phidir.shape).T
    for jj in range(N):
      idx = max(ii + jj - N, 0)
      Xspeed[jj, 0] = speeds[idx] - speedmean
      Xdir[jj, 0] = dirs[idx] - dirmean
    speeds.append(float(phispeed * Xspeed + espeed()))
    dirs.append(float(phidir * Xdir + edir()))
    xs.append(speeds[-1] * np.cos(dirs[-1]))
    ys.append(speeds[-1] * np.sin(dirs[-1]))

  return [xs, ys]
Ejemplo n.º 6
0
def echo_encode(original_audio: Audio,
                mark: bytes,
                alpha: float = 0.7,
                m: tuple = (150, 200)) -> Audio:
    """
    用回声隐藏进行隐写。

    优点:透明性强,写入的数据并非噪声,鲁棒性好,抗压缩性好。
    缺点:容量小,这里默认是8192个取样点写入一个比特。

    :param original_audio: 原音频,为一个Audio对象
    :param mark: 水印,必须为一个bytes对象
    :param alpha: 衰退率,理论上这个值越大水印鲁棒性越好,但生成的音频回音会更强
    :param m: 回音的延迟,分别为比特0和比特1的延迟
    :return: 隐写后的音频,为一个Audio对象
    """
    if not isinstance(mark, bytes):
        raise MarkFormatError("Mark must be a bytes object.")
    if alpha > 1 or alpha < 0:
        raise MarkFormatError("Alpha must be in [0,1].")
    bits = np.mat(get_all_bits(mark))
    bits_len = bits.shape[1]
    original_samples_reg = np.matrix(original_audio.get_reshaped_samples())
    samples_len = original_samples_reg.shape[1]
    channels = original_audio.channels
    if 8192 * bits_len > samples_len:
        raise MarkTooLargeError("Mark too large for echo encoding.")
    fragment_len = samples_len // bits_len
    encoded_len = bits_len * fragment_len
    kernel0 = np.concatenate((mt.zeros(
        (channels, m[0])), alpha * original_samples_reg), 1)
    kernel1 = np.concatenate((mt.zeros(
        (channels, m[1])), alpha * original_samples_reg),
                             1)  # 这里是两个回声核,0代表隐藏信息的0,1代表隐藏信息的1
    direct_sig = np.reshape(
        np.matrix(mt.ones((fragment_len, 1))) * bits, (encoded_len, 1), 'F')
    smooth_length = int(
        np.floor(fragment_len / 4) - np.floor(fragment_len / 4) % 4)
    tp = np.matrix(
        fftconvolve(np.array(direct_sig)[:, 0],
                    np.hanning(smooth_length)))  # 这里用的是汉宁窗口,其他窗口的效果类似
    window = tp[0, smooth_length // 2:tp.shape[1] - smooth_length // 2 +
                1] / np.max(np.abs(tp))  # window 用于平滑
    mixer = mt.ones((channels, 1)) * window  # 这里的 channels 很关键,需要写入所有的音轨
    encoded_samples_reg = original_samples_reg[:, :encoded_len] + \
                          np.multiply(kernel1[:, :encoded_len], mixer) + \
                          np.multiply(kernel0[:, :encoded_len], abs(mixer - 1)) # 这里是回音合成
    new_samples_reg = np.concatenate(
        (encoded_samples_reg, original_samples_reg[:, encoded_len:]), 1)
    new_audio = original_audio.spawn(new_samples_reg)
    new_audio.key = {
        'type': 'SINGLE_ECHO',
        'key': {
            'm': m,
            'fragment_len': fragment_len,
            'bits_len': bits_len
        }
    }
    return new_audio
Ejemplo n.º 7
0
def getOrthColumns(m):
    '''
    Constructs the orthogonally complementing columns of the input.

    Input of the form pxr is assumed to have r<=p,
    and have either full column rank r or rank 0 (scalar or matrix)
    Output is of the form px(p-r), except:
    a) if M square and full rank p, returns scalar 0
    b) if rank(M)=0 (zero matrix), returns I_p
    (Note you cannot pass scalar zero, because dimension info would be
    missing.)
    Return type is as input type.
    '''
    if type(m) == type(asarray(m)):
        m = mat(m)
        output = 'array'
    else: output = 'matrix'
    p, r = m.shape
    # first catch the stupid input case
    if p < r: raise ValueError, 'need at least as many rows as columns'
    # we use lstsq(M, ones) just to exploit its rank-finding algorithm,
    rk = lstsq(m, ones(p).T)[2]
    # first the square and full rank case:
    if rk == p: result = zeros((p,0))   # note the shape! hopefully octave-like
    # then the zero-matrix case (within machine precision):
    elif rk == 0: result = eye(p)
    # now the rank-deficient case:
    elif rk < r:
        raise ValueError, 'sorry, matrix does not have full column rank'
    # (what's left should be ok)
    else:
        # we have to watch out for zero rows in M,
        # if they are in the first p-r positions!
        # so the (probably inefficient) algorithm:
            # 1. check the rank of each row
            # 2. if zero, then also put a zero row in c
            # 3. if not, put the next unit vector in c-row
        idr = eye(r)
        idpr = eye(p-r)
        c = empty([0,r])    # starting point  
        co = empty([0, p-r]) # will hold orth-compl.
        idrcount = 0
        for row in range(p):
            # (must be ones() instead of 1 because of 2d-requirement
            if lstsq( m[row,:], ones(1) )[2] == 0 or idrcount >= r:
                c = r_[ c, zeros(r) ]
                co = r_[ co, idpr[row-idrcount, :] ]
            else:     # row is non-zero, and we haven't used all unit vecs 
                c = r_[ c, idr[idrcount, :] ] 
                co = r_[ co, zeros(p-r) ]
                idrcount += 1
        # earlier non-general (=bug) line: c = mat(r_[eye(r), zeros((p-r, r))])
        # and:  co = mat( r_[zeros((r, p-r)), eye(p-r)] )
        # old:
        # result = ( eye(p) - c * (M.T * c).I * M.T ) * co
        result = co - c * solve(m.T * c, m.T * co)
    if output == 'array': return result.A
    else: return result
Ejemplo n.º 8
0
Archivo: walk.py Proyecto: mklvcm/tsid
def AttachContact(name):
    i = contact_frames.index(name)
    contacts[i] = tsid.ContactPoint(name, robot, name, contactNormal, mu, fMin, fMax)
    contacts[i].setKp(kp_contact * matlib.ones(3).T)
    contacts[i].setKd(2.0 * np.sqrt(kp_contact) * matlib.ones(3).T)
    H_rf_ref = robot.framePosition(data, robot.model().getFrameId(name))
    contacts[i].setReference(H_rf_ref)
    contacts[i].useLocalFrame(False)
    invdyn.addRigidContact(contacts[i], w_forceRef, 1.0, 1)
Ejemplo n.º 9
0
    def neglnlikehood(self):
        a = np.linalg.solve(self.UPsicXc.T, np.matrix(self.yc).T)
        b = np.linalg.solve(self.UPsicXc, a)
        c = ones([self.nc, 1]).T * b

        d = np.linalg.solve(self.UPsicXc.T, ones([self.nc, 1]))
        e = np.linalg.solve(self.UPsicXc, d)
        f = ones([self.nc, 1]).T * e

        self.muc = c / f
        # This only works if yc is transposed, then its a scalar under two layers of arrays. Correct? Not sure

        print('y', self.yd.T)
        a = np.linalg.solve(self.UPsicXe.T, self.yd)
        print('a', a)
        b = np.linalg.solve(self.UPsicXe, a)
        print('b', b)
        c = ones([self.ne, 1]) * b
        print('c', c)

        d = np.linalg.solve(self.UPsicXe.T, ones([self.ne, 1], dtype=float))
        print(d)

        e = np.linalg.solve(self.UPsicXe, d)
        print(e)

        f = ones([self.ne, 1]).T * e
        print(f)

        self.mud = c / f

        a = np.linalg.solve(
            self.UPsicXc.T,
            (self.yc - ones([self.nc, 1]) * self.muc)) / self.nc
        b = np.linalg.solve(self.UPsicXc, a)
        self.SigmaSqrc = (self.yc - ones([self.nc, 1]) * self.muc).T * b

        print(self.ne)
        print(self.mud)
        print(self.UPsicXe.T)
        a = np.linalg.solve(
            self.UPsicXe.T,
            (self.yd - ones([self.ne, 1]) * self.mud)) / self.ne
        b = np.linalg.solve(self.UPsicXe, a)
        self.SigmaSqrd = (self.yd - ones([self.ne, 1]) * self.mud).T * b

        self.C = np.array([
            self.SigmaSqrc * self.PsicXc,
            self.rho * self.SigmaSqrc * self.PsicXcXe,
            self.rho * self.SigmaSqrc * self.PsicXeXc,
            np.power(self.rho, 2) * self.SigmaSqrc * self.PsicXe +
            self.SigmaSqrd * self.PsidXe
        ])
        np.reshape(c, [2, 2])

        self.UC = np.linalg.cholesky(self.C)
Ejemplo n.º 10
0
def applyTransformForPoints(points, moving_res, fixed_res, R, T, C = npy.asmatrix([0, 0, 0]).T):
    points[:, :3] *= moving_res[:3]
    points[:, :3] -= C.T
    if T.shape[1] == 1:
        TT = ml.ones((points.shape[0], 1)) * T.T
    else:
        TT = T
    temp = ml.mat(points[:, :3]) * R + TT + ml.ones((points.shape[0], 1)) * C.T
    points[:, :3] = temp
    points[:, :3] /= fixed_res[:3]
    return points
Ejemplo n.º 11
0
def svmfit(X, y):
    features, samples = np.shape(X)
    H = np.ones((features + 1, features + 1))
    H[0] = 0
    H[:, 0] = 0
    A = np.zeros((samples, features + 1))
    for i in range(samples):
        A[i] = np.hstack((y[i], y[i] * X[:, i]))
    w = mat.ones(features + 1)
    w = QuadProg(np.matrix(H), mat.zeros((features + 1, 1)), [], [], np.matrix(A), mat.ones((samples, 1)), np.matrix(w).transpose());
    return w
Ejemplo n.º 12
0
 def getMfeature(self):
     mata = npmatrix.empty((3, 4))  # random data
     mata = npmatrix.zeros((3, 4))  # zeros
     mata = npmatrix.ones((3, 4))  # one
     mata = npmatrix.eye(3)  # one along diagonal
     mata = npmatrix.eye(3, 5)  # one along diagonal
     mata = npmatrix.identity(3)  # identity square matrix
     mata = npmatrix.rand(3, 7)  # rand data
     mata = npmatrix.ones((3, 1))  # one
     print(mata)
     print(mata.shape)
     print(mata.dtype)
Ejemplo n.º 13
0
def knives(dim):  # returns a list of structured random row vectors
    if dim == 0:
        r = random.choice([-1, +1])
        return [mat.mat(r), mat.mat(-r)]
    n = int(2**dim)
    ss = [
        np.concatenate([mat.ones((1, n / 2)), -mat.ones((1, n / 2))], axis=1),
        np.concatenate([-mat.ones(
            (1, n / 2)), mat.ones((1, n / 2))], axis=1),
    ]
    for h in knives(dim - 1):
        ss.append(np.concatenate([h, h], axis=1))
    return ss
Ejemplo n.º 14
0
def svmfit(X, y):
    features, samples = np.shape(X)
    H = np.ones((features + 1, features + 1))
    H[0] = 0
    H[:, 0] = 0
    A = np.zeros((samples, features + 1))
    for i in range(samples):
        A[i] = np.hstack((y[i], y[i] * X[:, i]))
    w = mat.ones(features + 1)
    w = QuadProg(np.matrix(H), mat.zeros((features + 1, 1)), [], [],
                 np.matrix(A), mat.ones((samples, 1)),
                 np.matrix(w).transpose())
    return w
Ejemplo n.º 15
0
    def neglnlikehood(self):
        a = np.linalg.solve(self.UPsicXc.T, np.matrix(self.yc).T)
        b = np.linalg.solve( self.UPsicXc, a )
        c = ones([self.nc,1]).T * b

        d = np.linalg.solve(self.UPsicXc.T, ones([self.nc,1]))
        e = np.linalg.solve(self.UPsicXc, d)
        f = ones([self.nc,1]).T * e

        self.muc = c/f
        # This only works if yc is transposed, then its a scalar under two layers of arrays. Correct? Not sure

        print 'y',self.yd.T
        a = np.linalg.solve(self.UPsicXe.T, self.yd)
        print 'a',a
        b = np.linalg.solve(self.UPsicXe, a)
        print 'b', b
        c = ones([self.ne,1]) * b
        print 'c', c

        d = np.linalg.solve(self.UPsicXe.T, ones([self.ne,1], dtype=float))
        print d

        e = np.linalg.solve(self.UPsicXe, d)
        print e

        f = ones([self.ne,1]).T * e
        print f

        self.mud= c/f


        a = np.linalg.solve(self.UPsicXc.T,(self.yc-ones([self.nc,1])*self.muc))/self.nc
        b = np.linalg.solve(self.UPsicXc, a)
        self.SigmaSqrc=(self.yc-ones([self.nc,1])*self.muc).T* b



        print self.ne
        print self.mud
        print self.UPsicXe.T
        a = np.linalg.solve(self.UPsicXe.T,(self.yd-ones([self.ne,1])*self.mud))/self.ne
        b = np.linalg.solve(self.UPsicXe, a)
        self.SigmaSqrd=(self.yd-ones([self.ne,1])*self.mud).T* b

        self.C=np.array([self.SigmaSqrc*self.PsicXc, self.rho*self.SigmaSqrc*self.PsicXcXe, self.rho*self.SigmaSqrc*self.PsicXeXc, np.power(self.rho,2)*self.SigmaSqrc*self.PsicXe+self.SigmaSqrd*self.PsidXe])
        np.reshape(c,[2,2])

        self.UC = np.linalg.cholesky(self.C)
Ejemplo n.º 16
0
    def kernel_submatrix(self):
        # Cache kernel evaluations between vectors in this dataset so we don't repeat this work every
        # time we call Bhattacharrya
        X = self.X
        (n,d) = X.shape
        K = mat.zeros((n,n))
        for i in xrange(n):
            for j in xrange(i+1):
                K[i,j] = self.kernel(X[i,:], X[j,:])
                K[j,i] = K[i,j]

        Ki = mat.sum(K,1) / n
        k  = mat.sum(K) / (n*n)
        Kc = K - Ki * mat.ones((1,n)) - mat.ones((n,1)) * Ki.T + k * mat.ones((n,n))
        return (K, Kc)
Ejemplo n.º 17
0
def test_conjgrad_solver(n):
    H = linalg.hilbert(n)
    x0 = matlib.ones([n, 1])
    b = H @ x0
    x = [0, 1]
    y1 = []
    y2 = []
    for xi in tqdm(x):
        x1, niter = conjgrad(H, b, ind=xi, eps=1e-15)
        y1.append(linalg.norm(x0 - x1, ord=np.inf))
        y2.append(niter)
        sleep(0.01)
    fig, left_axis = plt.subplots()
    right_axis = left_axis.twinx()
    plt.title("Conjugate gradient results for "+ \
        "$H_{"+str(n)+"}\bf{x}=\bf{b}$")
    left_axis.plot(x, y1, 'ro-')
    left_axis.set_xlabel("Preprocessing index")
    left_axis.set_ylabel("Infinite-norm error", color='r')
    left_axis.tick_params(axis='y', colors='r')
    right_axis.plot(x, y2, 'bo-')
    right_axis.set_xlabel("Same")
    right_axis.set_ylabel("Iteration step", color='b')
    right_axis.tick_params(axis='y', colors='b')
    plt.show()
Ejemplo n.º 18
0
def vel_source_matrix(Qab, rl, phid):
    velsl = zero_matrix_vector(Qab.shape, dtype=float)
    velsl.y = -Qab
    faco = ones(Qab.shape, dtype=float)
    faco[rl.z != 0.0] = -1.0
    velsl.z = multiply(faco, phid)
    return velsl
Ejemplo n.º 19
0
def kernel_matrix(X, kernel, n1, n2):
    (n, d) = X.shape
    assert n == n1 + n2

    K = mat.zeros((n, n))
    for i in xrange(n):
        for j in xrange(i + 1):
            K[i, j] = kernel(X[i, :], X[j, :])
            K[j, i] = K[i, j]

    U1 = mat.sum(K[0:n1, :], 0) / n1
    U2 = mat.sum(K[n1:n, :], 0) / n2
    U1m = mat.tile(U1, (n1, 1))
    U2m = mat.tile(U2, (n2, 1))
    U = mat.bmat('U1m; U2m')
    m1m1 = mat.sum(K[0:n1, 0:n1]) / (n1 * n1)
    m1m2 = mat.sum(K[0:n1, n1:n]) / (n1 * n2)
    m2m2 = mat.sum(K[n1:n, n1:n]) / (n2 * n2)
    mumu = mat.zeros((n, n))
    mumu[0:n1, 0:n1] = m1m1
    mumu[0:n1, n1:n] = m1m2
    mumu[n1:n, 0:n1] = m1m2
    mumu[n1:n, n1:n] = m2m2
    Kcu = K - U
    Kuc = Kcu.T
    N = mat.ones((n, n)) / n
    Kc = K - U - U.T + mumu
    return (K, Kuc, Kc)
Ejemplo n.º 20
0
    def kernel_submatrix(self):
        # Cache kernel evaluations between vectors in this dataset so we don't repeat this work every
        # time we call Bhattacharrya
        X = self.X
        (n, d) = X.shape
        K = mat.zeros((n, n))
        for i in xrange(n):
            for j in xrange(i + 1):
                K[i, j] = self.kernel(X[i, :], X[j, :])
                K[j, i] = K[i, j]

        Ki = mat.sum(K, 1) / n
        k = mat.sum(K) / (n * n)
        Kc = K - Ki * mat.ones((1, n)) - mat.ones(
            (n, 1)) * Ki.T + k * mat.ones((n, n))
        return (K, Kc)
Ejemplo n.º 21
0
def test_simpleit_solver(n):
    H = linalg.hilbert(n)
    x0 = matlib.ones([n, 1])
    b = H @ x0
    x = np.arange(0.1, 2.0, 0.1)
    y1 = []
    y2 = []
    #x1, niter = jacobi(H,b)
    #print(x1,niter)
    for xi in tqdm(x):
        x1, niter = gs_sor(H, b, omg=xi, eps=1e-15)
        y1.append(math.log(linalg.norm(x0 - x1, ord=np.inf), 10))
        y2.append(niter)
        sleep(0.01)
    fig, left_axis = plt.subplots()
    right_axis = left_axis.twinx()
    plt.title("SOR results for $H_{" + str(n) + "}\bf{x}=\bf{b}$")
    left_axis.plot(x, y1, 'ro-')
    left_axis.set_xlabel("Relaxation coefficient")
    left_axis.set_ylabel("log(Infinite-norm error)", color='r')
    left_axis.tick_params(axis='y', colors='r')
    right_axis.plot(x, y2, 'bo-')
    right_axis.set_xlabel("Same")
    right_axis.set_ylabel("Iteration step", color='b')
    right_axis.tick_params(axis='y', colors='b')
    plt.show()
Ejemplo n.º 22
0
 def doublet_velocity_potentials(self,
                                 pnts: MatrixVector,
                                 extraout: bool = False,
                                 sgnz: matrix = None,
                                 factor: bool = True,
                                 betx: float = 1.0):
     rls = self.points_to_local(pnts, betx=betx)
     absx = absolute(rls.x)
     rls.x[absx < tol] = 0.0
     absy = absolute(rls.y)
     rls.y[absy < tol] = 0.0
     absz = absolute(rls.z)
     rls.z[absz < tol] = 0.0
     if sgnz is None:
         sgnz = ones(rls.shape, float)
         sgnz[rls.z <= 0.0] = -1.0
     avs = rls - self.grdal
     phida, ams = phi_doublet_matrix(avs, rls, sgnz)
     bvs = rls - self.grdbl
     phidb, bms = phi_doublet_matrix(bvs, rls, sgnz)
     phids = phida - phidb
     if factor:
         phids = phids / fourPi
     if extraout:
         return phids, rls, avs, ams, bvs, bms
     else:
         return phids
Ejemplo n.º 23
0
def CanonicalFromDPH2(alpha, A, prec=1e-14):
    """
    Returns the canonical form of an order-2 discrete phase-type 
    distribution.
    
    Parameters
    ----------
    alpha : matrix, shape (1,2)
        Initial vector of the discrete phase-type distribution
    A : matrix, shape (2,2)
        Transition probability matrix of the discrete phase-type
        distribution
    prec : double, optional
      Numerical precision for checking the input, default value
      is 1e-14
    
    Returns
    -------
    beta : matrix, shape (1,2)
      The initial probability vector of the canonical form
    B : matrix, shape (2,2)
      Transition probability matrix of the canonical form
    """

    if butools.checkInput and not CheckMGRepresentation(alpha, A, prec):
        raise Exception(
            "CanonicalFromDPH2: Input is not a valid DPH representation!")

    if butools.checkInput and (A.shape[0] != 2 or A.shape[1] != 2):
        raise Exception("CanonicalFromDPH2: Dimension must be 2!")

    ev = la.eigvals(A)
    ix = np.argsort(-np.abs(np.real(ev)))
    lambd = ev[ix]
    e = ml.ones((2, 1))
    p1 = (alpha * (e - A * e))[0, 0]
    if lambd[0] > 0 and lambd[1] > 0 and lambd[0] != lambd[1]:
        d1 = (1 - lambd[0]) * (1 - p1 - lambd[1]) / (lambd[0] - lambd[1])
        d2 = p1 - d1
        beta = ml.matrix([[
            d1 * (lambd[0] - lambd[1]) / ((1 - lambd[0]) * (1 - lambd[1])),
            (d1 + d2) / (1 - lambd[1])
        ]])
        B = ml.matrix([[lambd[0], 1 - lambd[0]], [0, lambd[1]]])
    elif lambd[0] > 0 and lambd[0] == lambd[1]:
        d2 = p1
        d1 = (1 - lambd[0]) * (1 - d2 - lambd[0]) / lambd[0]
        beta = ml.matrix(
            [[d1 * lambd[0] / (1 - lambd[0])**2, d2 / (1 - lambd[0])]])
        B = ml.matrix([[lambd[0], 1 - lambd[0]], [0, lambd[0]]])
    elif lambd[0] > 0:
        d1 = (1 - lambd[0]) * (1 - p1 - lambd[1]) / (lambd[0] - lambd[1])
        d2 = p1 - d1
        beta = ml.matrix([[(d1 * lambd[0] + d2 * lambd[1]) / ((1 - lambd[0]) *
                                                              (1 - lambd[1])),
                           (d1 + d2) * (1 - lambd[0] - lambd[1]) /
                           ((1 - lambd[0]) * (1 - lambd[1]))]])
        B = ml.matrix([[lambd[0] + lambd[1], 1 - lambd[0] - lambd[1]],
                       [lambd[0] * lambd[1] / (lambd[0] + lambd[1] - 1), 0]])
    return (np.real(beta), np.real(B))
Ejemplo n.º 24
0
def applyTransform(points, para):
    T = ml.mat(para[:3]).T
    R = ml.mat(para[3:].reshape(3, 3)).T.I
    result = npy.array(points)
    result[:, :3] = ml.mat(points[:, :3]) * R + ml.ones(
        (points.shape[0], 1)) * T.T
    return result
Ejemplo n.º 25
0
Archivo: hmm.py Proyecto: imoonkey/embo
    def loglikelihood(self, obs):
        # compute the log likelihood given a sequence of obs
        t_mat = npmat.matrix(self.t_mat)
        # use alpha
        alpha = npmat.matrix((self.pi_vec * self.z_mat[obs[0], :])[:,np.newaxis])
#        print('self.z_mat[obs[0], :]: '+ str(self.z_mat[obs[0], :]))
#        print('self.pi_vec: '+ str(self.pi_vec))
#        print('self.pi_vec * self.z_mat[obs[0], :]: '+ str(self.pi_vec * self.z_mat[obs[0], :]))
#        print('alpha: '+ str(alpha))
        log_coef = 0
        for i in range(1,len(obs)):
            alpha = npmat.matrix(((t_mat * alpha).getA()[:,0] * self.z_mat[obs[i], :])[:,np.newaxis])
            asum = alpha.sum()
            alpha /= asum
            log_coef += np.log(asum)
        alpha_ll = log_coef + np.log(alpha.sum())
        # use beta
        if False:
            beta = npmat.ones((t_mat.shape[0], 1))
            log_coef = 0
            for i in range(len(obs)-2,-1,-1):
    #            print('self.z_mat[obs[i+1], :]: ' + str(self.z_mat[obs[i+1], :]))
    #            print('beta.getA()[0]: ' + str(beta.getA()[:,0]))
    #            print('self.z_mat[obs[i+1], :] * beta.getA()[:,0]: ' + str(self.z_mat[obs[i+1], :] * beta.getA()[:,0]))
    #            print('npmat.matrix(result): ' + str(npmat.matrix((self.z_mat[obs[i+1], :] * beta.getA()[:,0])[:,np.newaxis])))
    #            print('t_mat.getT(): ' + str(t_mat.getT()))
                beta = t_mat.getT() * npmat.matrix((self.z_mat[obs[i+1], :] * beta.getA()[:,0])[:,np.newaxis])
                bsum = beta.sum()
                beta /= bsum
                log_coef += np.log(bsum)
            beta_ll = log_coef + np.log(np.sum(beta.getA()[0] * self.pi_vec * self.z_mat[obs[0],:]))
    #        print('alpha_ll: ' + str(alpha_ll))
    #        print('beta_ll: ' + str(beta_ll))
        return alpha_ll
Ejemplo n.º 26
0
def applyTransformForPoints(points,
                            moving_res,
                            fixed_res,
                            R,
                            T,
                            C=npy.asmatrix([0, 0, 0]).T):
    points[:, :3] *= moving_res[:3]
    points[:, :3] -= C.T
    if T.shape[1] == 1:
        TT = ml.ones((points.shape[0], 1)) * T.T
    else:
        TT = T
    temp = ml.mat(points[:, :3]) * R + TT + ml.ones((points.shape[0], 1)) * C.T
    points[:, :3] = temp
    points[:, :3] /= fixed_res[:3]
    return points
Ejemplo n.º 27
0
 def doublet_velocity_potentials(self,
                                 pnts: MatrixVector,
                                 extraout: bool = False,
                                 sgnz: matrix = None,
                                 factor: bool = True,
                                 betx: float = 1.0):
     rls = self.points_to_local(pnts, betx=betx)
     absx = absolute(rls.x)
     rls.x[absx < tol] = 0.0
     absy = absolute(rls.y)
     rls.y[absy < tol] = 0.0
     absz = absolute(rls.z)
     rls.z[absz < tol] = 0.0
     if sgnz is None:
         sgnz = ones(rls.shape, float)
         sgnz[rls.z <= 0.0] = -1.0
     ovs = rls - self.grdol
     phido, oms = phi_doublet_matrix(ovs, rls, sgnz)
     phidt = phi_trailing_doublet_matrix(rls, sgnz, self.faco)
     phid = phido * self.faco + phidt
     if factor:
         phid = phid / fourPi
     if extraout:
         output = phid, rls, ovs, oms
     else:
         output = phid
     return output
Ejemplo n.º 28
0
def kernel_matrix(X, kernel, n1, n2):
    (n, d) = X.shape
    assert n == n1 + n2

    K = mat.zeros((n,n))
    for i in xrange(n):
        for j in xrange(i+1):
            K[i,j] = kernel(X[i,:], X[j,:])
            K[j,i] = K[i,j]

    U1 = mat.sum(K[0:n1,:],0) / n1
    U2 = mat.sum(K[n1:n,:],0) / n2
    U1m = mat.tile(U1, (n1,1))
    U2m = mat.tile(U2, (n2,1))
    U = mat.bmat('U1m; U2m')
    m1m1 = mat.sum(K[0:n1, 0:n1]) / (n1*n1)
    m1m2 = mat.sum(K[0:n1, n1:n]) / (n1*n2)
    m2m2 = mat.sum(K[n1:n, n1:n]) / (n2*n2) 
    mumu = mat.zeros((n,n))
    mumu[0:n1, 0:n1] = m1m1
    mumu[0:n1, n1:n] = m1m2
    mumu[n1:n, 0:n1] = m1m2
    mumu[n1:n, n1:n] = m2m2
    Kcu = K - U
    Kuc = Kcu.T
    N = mat.ones((n,n))/n
    Kc = K - U - U.T + mumu
    return (K, Kuc, Kc)
Ejemplo n.º 29
0
Archivo: em.py Proyecto: imoonkey/embo
def em_step(num_states, num_obs, pi_vec, z_mat, t_mat, obs_all):
    # given initial parameters, run em from the obs
    # obs are 2d with each row being a trajectory
    # returns estimated observation and transition matrices

    num_steps = obs_all.shape[1]
    t_mat = npmat.matrix(t_mat)

    gamma_sum = np.zeros((num_states,))
    xi_sum = np.zeros((num_states, num_states))
    obs_sum = np.zeros((num_obs, num_states))

    for t in range(obs_all.shape[0]):
        obs = obs_all[t, :]
        # compute alpha
        alpha = npmat.zeros((num_states, num_steps))
        alpha[:, 0] = npmat.matrix((pi_vec * z_mat[obs[0], :])[:, np.newaxis])
        for i in range(1, len(obs)):
            alpha[:, i] = npmat.matrix(((t_mat * alpha[:, i - 1]).getA()[:, 0] * z_mat[obs[i], :])[:, np.newaxis])
            asum = alpha[:, i].sum()
            alpha[:, i] /= asum
        # print('alpha\n' + str(alpha))
        # compute beta
        beta = npmat.zeros((num_states, num_steps))
        beta[:, num_steps - 1] = npmat.ones((num_states, 1))
        for i in range(len(obs) - 2, -1, -1):
            beta[:, i] = t_mat.getT() * npmat.matrix(
                (z_mat[obs[i + 1], :] * beta[:, i + 1].getA()[:, 0])[:, np.newaxis])
            bsum = beta[:, i].sum()
            beta[:, i] /= bsum
        # print(beta)
        # compute gamma
        gamma = alpha.getA() * beta.getA()
        gamma = gamma / (gamma.sum(axis=0)[np.newaxis, :])
        # print(gamma)
        # compute xi (transposed from the paper)
        xi = np.zeros((num_states, num_states, num_steps - 1))
        for i in range(num_steps - 1):
            xi[:, :, i] = np.transpose(alpha[:, i].getA()) * t_mat.getA() * (z_mat[obs[i + 1], :] *
                                                                             beta[:, i + 1].getA()[0])[:, np.newaxis]
            xsum = xi[:, :, i].sum()
            xi[:, :, i] /= xsum
        #            print('xi[:,:,i]' + str(xi[:,:,i]))
        #            print('np.transpose(alpha[:,i].getA()): ' + str(np.transpose(alpha[:,i].getA())))
        #            print('t_mat: ' + str(t_mat))
        #            print('result of *: ' + str(np.transpose(alpha[:,i].getA()) * t_mat.getA()))
        # add to the sums
        gamma_sum += gamma.sum(axis=1)
        xi_sum += xi.sum(axis=2)
        for z in range(num_obs):
            obs_sum[z, :] += gamma[:, obs == z].sum(axis=1)
        #        print('gamma_sum\n' + str(gamma_sum))
        #        print('xi_sum\n' + str(xi_sum))
        #        print('obs_sum\n' + str(obs_sum))
    # finally compute estimates
    est_z_mat = obs_sum / obs_sum.sum(axis=0)[np.newaxis, :]
    est_t_mat = xi_sum / xi_sum.sum(axis=0)[np.newaxis, :]

    return est_z_mat, est_t_mat;
Ejemplo n.º 30
0
 def predict(self, X, *args, **kwargs):
     m = X.shape[0]
     Z = None
     S = None
     A = numpy.concatenate((matlib.ones((m, 1)), X), axis=1)
     
     is_first_layer = True
     for Theta_index in range(len(self._Theta)):
         if is_first_layer:
             is_first_layer = False
         else:
             A = numpy.concatenate((matlib.ones((m, 1)), Z), axis=1)
         
         Z = A * self._Theta[Theta_index]
         S = sigmoid(Z)
     
     return S > 0.5
Ejemplo n.º 31
0
	def make_y_X_stack(self):
		VAR_attr = self.VAR_attr
		veclen = VAR_attr['veclen']
		laglen = VAR_attr['laglen']
		nuofobs = VAR_attr['nuofobs']
		avobs = VAR_attr['avobs']
		useconst = VAR_attr['useconst']
		data = self.data

		y = data[laglen:,:]
		X = MAT.zeros((avobs,veclen*laglen))
		for x1 in range(0,laglen,1):
			X[:,x1*veclen:(x1+1)*veclen] = data[(laglen-1)-x1:(nuofobs-1)-x1,:]
		if self.VAR_attr['useconst'] == 1:
			X = N.hstack((MAT.ones((avobs,1)),X[:,:]))
		try:
			self.ols_results['y'] = y
			self.ols_results['X'] = X
		except:
			self.ols()
			self.ols_results['y'] = y
			self.ols_results['X'] = X

		if useconst == 0:
			y_stack = MAT.zeros((avobs,veclen*laglen))
			y_stack_1 = MAT.zeros((avobs,veclen*laglen))
			y_stack[:,0:veclen] = y
			y_stack_1 = X

			y_stack = N.hstack((y_stack[:,0:veclen],y_stack_1[:,0:veclen*(laglen-1)]))

			self.ols_comp_results['X'] = y_stack_1
			self.ols_comp_results['y'] = y_stack
		else:
			y_stack = MAT.zeros((avobs,veclen*laglen))
			y_stack_1 = MAT.zeros((avobs,1+veclen*laglen))
			y_stack_1[:,0] = MAT.ones((avobs,1))[:,0]
			y_stack[:,0:veclen] = y
			y_stack_1 = X

			y_stack = N.hstack((y_stack[:,0:veclen],y_stack_1[:,1:veclen*(laglen-1)+1]))

			self.ols_comp_results['X'] = y_stack_1
			self.ols_comp_results['y'] = y_stack
Ejemplo n.º 32
0
 def sign_local_z(self, pnts: MatrixVector, betx: float = 1.0):
     vecs = pnts - self.pnto
     nrm = self.nrm
     if betx != 1.0:
         vecs.x = vecs.x / betx
         nrm = Vector(self.nrm.x / betx, self.nrm.y, self.nrm.z)
     locz = vecs * nrm
     sgnz = ones(locz.shape, float)
     sgnz[locz <= 0.0] = -1.0
     return sgnz
Ejemplo n.º 33
0
def trainNB0(trainMatrix, trainCategory):
    numTrainDocs = len(trainMatrix)  # Number of training sentence
    numWords = len(trainMatrix[0])  # Number of word in sentence
    pAbusive = sum(trainCategory) / float(
        numTrainDocs)  # Probability of abusive sentence in all sentences
    p0Num = ones(numWords)
    p1Num = ones(numWords)  #The reason why use 'ones', not 'zeros' is in P62
    p0Denom = 2.0
    p1Denom = 2.0
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:
            p1Num += trainMatrix[i]
            p1Denom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Denom += sum(trainMatrix[i])
    p1Vect = p1Num / p1Denom
    p0Vect = p0Num / p0Denom
    return p0Vect, p1Vect, pAbusive
Ejemplo n.º 34
0
def point(x, y):
    """
    2D affine point vector used in 2D spatial transformations. To use,
    call the matrix '*' operator.
    """
    m = ones((3, 1))
    m[0] = x
    m[1] = y

    return m
Ejemplo n.º 35
0
def applyRigidTransformOnPoints(points, res, T):  # Y = XT
    X = ml.ones([points.shape[0], 4], dtype=npy.float32)
    tmp = points.copy()
    tmp[:, :3] *= res
    X[:, :3] = tmp[:, :3]
    Y = X * T
    result_points = npy.array(points.copy())
    result_points[:, :3] = Y[:, :3]
    result_points[:, :3] /= res
    return result_points
Ejemplo n.º 36
0
    def KFDA_J(self, K, N_p, N_n, q=0):
        
        if q==0:
            q = N_p + N_n  
        a = Nmat.ones([N_p + N_n, 1])
        a[0:N_p] = 1 / N_p
        a[N_p:] = -1 / N_n
        A = Nmat.matrix([np.diag(1 / np.sqrt(N_p) * (Nmat.identity(N_p) - 1 / N_p * Nmat.ones([N_p, N_p]))), \
                         np.diag(1 / np.sqrt(N_n) * (Nmat.identity(N_n) - 1 / N_n * Nmat.ones([N_n, N_n])))])

        
        
        Q = K
        

        
        tmp = 1e8 * Nmat.identity(N_p + N_n) - 1e8 * A * Q * LA.inv(1e8 * Nmat.identity(q) + Q.T * A * A(Q)) * Q.T * A
        J = 1/1e-8 * (a.T * K * a - a.T * K * A * tmp * A * K * a)
        return J
Ejemplo n.º 37
0
def applyRigidTransformOnPoints(points, res, T): # Y = XT
    X = ml.ones([points.shape[0], 4], dtype = npy.float32)
    tmp = points.copy()
    tmp[:, :3] *= res
    X[:, :3] = tmp[:, :3]
    Y = X * T
    result_points = npy.array(points.copy())
    result_points[:, :3] = Y[:, :3]
    result_points[:, :3] /= res
    return result_points
Ejemplo n.º 38
0
    def __new__(cls, c):
        """
        """

        c = c.view(matlib.matrix).reshape(1, -1)

        T = matlib.vstack((
            matlib.hstack((matlib.identity(c.size), c)),
            matlib.hstack((matlib.zeros(c.size), matlib.ones(1))),
        ))
        return super().__new__(cls, T)
Ejemplo n.º 39
0
 def __new__(cls, c):
     """
     """
     
     c = c.view(matlib.matrix).reshape(1, -1)
         
     T = matlib.vstack((
         matlib.hstack((matlib.identity(c.size), c)),
         matlib.hstack((matlib.zeros(c.size), matlib.ones(1))),
     ))
     return super().__new__(cls, T)
Ejemplo n.º 40
0
    def KFDA_J(self,K,N_p,N_n,q=0):
        #calculate the kfda J
        #N_p:the number of the positive feature
        #N_n:the number of the negtive feature
        if q==0:
            q=N_p+N_q
        a=Nmat.ones([N_p+N_n,1])
        a[0:N_p]=1/N_p
        a[N_p:]=-1/N_n
        A=Nmat.matrix([np.diag(1/np.sqrt(N_p)*(Nmat.identity(N_p)-1/N_p*Nmat.ones([N_p,N_p]))),\
            np.diag(1/np.sqrt(N_n)*(Nmat.identity(N_n)-1/N_n*Nmat.ones([N_n,N_n])))])

        #
        Q=Clustr_kmeans(K,q)        
        #
        
        tmp=np.linaly.inv(1e-8*Nmat.identity(N_p+N_n)+A*K*A)
        tmp=1e8*Nmat.identity(N_p+N_n)-1e8*A*Q*LA.inv(1e8*Nmat.identity(q)+Q.T*A*A(Q))*Q.T*A
        J=1/(1e-8)*(a.T*K*a-a.T*K*A*tmp*A*K*a)
        return J
Ejemplo n.º 41
0
 def evalfun (oH, k=0):
     Ones = ml.ones(oH[0].shape)
     if k%2 == 0:
         dist = np.min(oH[0])
         for oHk in oH:
             dist = min(dist, np.min(oHk), np.min(Ones-oHk))
         return -dist
     else:
         dist = np.sum(oH[0][oH[0]<0])
         for oHk in oH:
             dist += min(np.sum(oHk[oHk<0]), np.sum(oHk[Ones-oHk<0]))
         return -dist
Ejemplo n.º 42
0
 def __call__(self, x, z=[], diag=False):
     if x.shape[0] == 0:
         return np.zeros((0, 0))
     if diag:
         return self.sf2*np.ones((x.shape[0], 1))
     sx = x*self.covd
     if z == []:
         K = np.asmatrix(ssd.squareform(ssd.pdist(sx, 'sqeuclidean')))
     else:
         sz = z*self.covd
         K = np.asmatrix(ssd.cdist(sx, sz, 'sqeuclidean'))
     K = self.sf2*np.exp(-K/2)
     return K
Ejemplo n.º 43
0
def CanonicalFromDPH2 (alpha,A,prec=1e-14):
    """
    Returns the canonical form of an order-2 discrete phase-type 
    distribution.
    
    Parameters
    ----------
    alpha : matrix, shape (1,2)
        Initial vector of the discrete phase-type distribution
    A : matrix, shape (2,2)
        Transition probability matrix of the discrete phase-type
        distribution
    prec : double, optional
      Numerical precision for checking the input, default value
      is 1e-14
    
    Returns
    -------
    beta : matrix, shape (1,2)
      The initial probability vector of the canonical form
    B : matrix, shape (2,2)
      Transition probability matrix of the canonical form
    """

    if butools.checkInput and not CheckMGRepresentation (alpha, A, prec):
        raise Exception("CanonicalFromDPH2: Input is not a valid DPH representation!")

    if butools.checkInput and (A.shape[0]!=2 or A.shape[1]!=2):
        raise Exception("CanonicalFromDPH2: Dimension must be 2!")

    ev = la.eigvals(A)
    ix = np.argsort(-np.abs(np.real(ev)))
    lambd = ev[ix]
    e=ml.ones((2,1))
    p1=(alpha*(e-A*e))[0,0]
    if lambd[0]>0 and lambd[1]>0 and lambd[0] != lambd[1]:
        d1=(1-lambd[0])*(1-p1-lambd[1])/(lambd[0]-lambd[1])
        d2=p1-d1
        beta=ml.matrix([[d1*(lambd[0]-lambd[1])/((1-lambd[0])*(1-lambd[1])),(d1+d2)/(1-lambd[1])]])
        B=ml.matrix([[lambd[0],1-lambd[0]],[0,lambd[1]]])
    elif lambd[0]>0 and lambd[0]==lambd[1]:
        d2=p1
        d1=(1-lambd[0])*(1-d2-lambd[0])/lambd[0]
        beta=ml.matrix([[d1*lambd[0]/(1-lambd[0])**2,d2/(1-lambd[0])]])
        B=ml.matrix([[lambd[0],1-lambd[0]],[0,lambd[0]]])
    elif lambd[0]>0:
        d1=(1-lambd[0])*(1-p1-lambd[1])/(lambd[0]-lambd[1])
        d2=p1-d1
        beta=ml.matrix([[(d1*lambd[0]+d2*lambd[1])/((1-lambd[0])*(1-lambd[1])),(d1+d2)*(1-lambd[0]-lambd[1])/((1-lambd[0])*(1-lambd[1]))]])
        B=ml.matrix([[lambd[0]+lambd[1],1-lambd[0]-lambd[1]],[lambd[0]*lambd[1]/(lambd[0]+lambd[1]-1),0]])
    return (np.real(beta),np.real(B))
    def __init__(self,
                 A=np.matrix(0.9),
                 B=np.matrix(1.),
                 Z1=np.matrix(1.),
                 Z2=np.matrix(1.),
                 noise_cov=np.matrix(.01),
                 seed=1):
        '''
		A,B are some Matrices here
			if state is mx1 then A is mxm, B is mxn then the u should be nx1
		Z1,Z2 are some positive semi-definite weight matrices mxm or scala
			that determines the trade-off between keeping state small and keeping action small.
		'''
        super(LQREnv, self).__init__()
        self.m = A.shape[0]
        assert B.shape[0] == A.shape[1]
        self.n = B.shape[1]
        if not np.isscalar(Z1):
            Z1.shape[0] == A.shape[0]
        if not np.isscalar(Z2):
            Z2.shape == A.shape

        self.A = A
        self.B = B
        self.Z1 = Z1
        self.Z2 = Z2
        self.state = None
        # noisy
        self.noise_mu = np.matrix(np.zeros(self.m).reshape(self.m, 1))
        self.noise_cov = noise_cov
        self.rng = self.set_seed(seed)

        high = mb.ones((self.n, 1)) * np.inf
        # TODO: Action now is continous
        self.action_space = Box(low=-high, high=high, dtype=np.float32)
        high = mb.ones((self.m, 1)) * np.inf
        # print(self.action_space)
        self.observation_space = Box(low=-high, high=high, dtype=np.float32)
Ejemplo n.º 45
0
def gradAscent(data,label,labelSet):
    hmat = zeros((data.shape[0],len(labelSet) - 1)).tolist()
#     print "hmat[2][1]",hmat[2][1]
    i = 0
#     print "ge zhong changdu",len(labelSet) - 1,data.shape[0]
    for k in range(len(labelSet) - 1):
        for j in range(data.shape[0]):
            if(label[j] == labelSet[k]):
                hmat[j][k] = 1
            
    iteration = 1
    error = 0.00001
    m,n = shape(data)
    labelNum = len(labelSet) - 1
    if(labelNum >= 2):
        weights = ones((n,labelNum))
    else:
        weights = ones((n,1))
    
#     for i in range(iteration):
    for i in range(labelNum):
        diff = 1
        hmat = mat(hmat)
        while(diff > error):
            if(labelNum == 1): 
                h = sigMoid(data * weights[:,i])
                h = mat(h)
                deri = mat(label).transpose() - h  ###11*1
            else:
                h = fakeSigMoid(data * weights)
                h = mat(h)
                deri = hmat[:,i] - h[:,i]  ###11*1
    #         cichu hai zhengchang
            formal = copy.deepcopy(weights[:,i])
            weights[:,i] = weights[:,i] + alpha * data.transpose() * deri   ####梯度下降法目标函数取最小值,所以此处为+
            diff = abs(formal.transpose() * formal - weights[:,i].transpose() * weights[:,i])
            print "diff = ",diff
    return weights
Ejemplo n.º 46
0
def PageRank(A,N,m,id2p,top,p_f):
    E = ones((N,N));
    MM = (1.0-m)*A + (m/N)*E;
    #MM = A;

    x = MM * (1.0/N) * ones((N,1));
    x = eigenCalc(x, MM,N,200)

    for i in range(0,N):
        top[i] = x[i][0,0]

    import operator
    s_t = sorted(top.iteritems(), key=operator.itemgetter(1))
    s_t.reverse();
    #print "sorted_top = ", sorted_top;
    f = open('listwithranks.txt', 'w')
    k = 0
    for protein in s_t:
        s = str(id2p[protein[0]]) + " " + str(protein[1]) + " flag " + str(p_f[protein[0]])
        print >> f, s
    f.close()

    return s_t
Ejemplo n.º 47
0
Archivo: core.py Proyecto: josipd/Gpy
 def inf(self, x, meanonly=False):
     x = np.asmatrix(x)
     assert x.shape[1] == self.d
     n = x.shape[0]
     # Handle empty test set
     if n == 0:
         return (np.zeros((0, 1)), np.zeros((0, 1)))
     ms = self.kernel.mean*np.ones((n, 1))
     Kbb = self.kernel(x, diag=True)
     # Handle empty training set
     if len(self) == 0:
         return (ms, np.asmatrix(np.diag(Kbb)).T)
     Kba = self.kernel(x, self.x)
     m = self.kernel.mean*np.ones((len(self), 1))
     fm = ms + Kba*scipy.linalg.cho_solve((self.L, True), self.y - m,
                                          overwrite_b=True)
     if meanonly:
         return fm
     else:
         W = scipy.linalg.cho_solve((self.L, True), Kba.T)
         fv = np.asmatrix(Kbb - np.sum(np.multiply(Kba.T, W), axis=0).T)
         # W = np.asmatrix(scipy.linalg.solve(self.L, Kba.T, lower=True))
         # fv = np.asmatrix(Kbb - np.sum(np.power(W, 2), axis=0).T)
         return (fm, fv)
Ejemplo n.º 48
0
def startComputation_np(m, N, e_limit, maxIt):        
    x = matlib.ones((N,1))  
    start = time.clock()     
    itCnt = 0
    success = False
    while itCnt < maxIt and not success:
        itCnt += 1
        y = m * x
        y_max = y.max()
        x_new = y / y_max
        e_k = vectorChange_np(N, x, x_new)
        x = x_new
        if e_k <= e_limit :
            success = True
    showResult(success, N, y_max, x.tolist(), time.clock()-start, itCnt)  
Ejemplo n.º 49
0
def MinimalRepFromMRAP (H, how="obscont", precision=1e-12):
    """
    Returns the minimal representation of a marked rational
    arrival process.
    
    Parameters
    ----------
    H : list of matrices of shape (M,M)
        The list of H0, H1, ..., HK matrices of the marked
        rational arrival process
    how : {"obs", "cont", "obscont"}, optional        
        Determines how the representation is minimized. 
        "cont" means controllability, "obs" means 
        observability, "obscont" means that the rational arrival
        process is minimized in both respects. Default value 
        is "obscont".
    precision : double, optional
       Precision used by the Staircase algorithm. The default
       value is 1e-12.
    
    Returns
    -------
    D : list of matrices of shape (M,M)
        The D0, D1, ..., DK matrices of the minimal 
        representation
    
    References
    ----------
    .. [1] P. Buchholz, M. Telek, "On minimal representation of 
           rational arrival processes." Madrid Conference on 
           Qeueuing theory (MCQT), June 2010.
    """

    if butools.checkInput and not CheckMRAPRepresentation (H):
        raise Exception("MinimalRepFromMRAP: Input is not a valid MRAP representation!")    

    if how=="cont":
        B, n = MStaircase (H, ml.ones((H[0].shape[0],1)), precision)
        return [(la.inv(B)*Hk*B)[0:n,0:n] for Hk in H]
    elif how=="obs":
        alpha, A = MarginalDistributionFromMRAP (H)
        G = [Hk.T for Hk in H]
        B, n = MStaircase (G, alpha.T, precision)
        return [(la.inv(B)*Hk*B)[0:n,0:n] for Hk in H]
    elif how=="obscont":
        D = MinimalRepFromMRAP(H, "cont", precision)
        return MinimalRepFromMRAP(D, "obs", precision)
Ejemplo n.º 50
0
 def __new__(cls, m, n):
     """
     """
     
     if not all((
             isinstance(m, int),
             isinstance(n, int),
         )):
         raise ValueError
     
     data = matlib.hstack([
         matlib.vstack((
             matlib.hstack((matlib.identity(n, dtype=int), matlib.matrix(p, dtype=int).T)),
             matlib.hstack((matlib.matrix(p, dtype=int), matlib.ones(1))),
         ))
         for p in product(range(m), repeat=n)
     ])
     return super().__new__(cls, data, dtype=int).view(cls)
Ejemplo n.º 51
0
    def kernel_supermatrix(self, i, j):
        kernel = self.kernel
        D1 = self.datasets[i]
        D2 = self.datasets[j]
        X1 = D1.X
        X2 = D2.X
        (n1, d) = X1.shape
        (n2, d) = X2.shape
        n = n1 + n2
        X = mat.bmat('X1; X2')
        K1 = D1.K
        K2 = D2.K
        K = mat.zeros((n,n))
        K[0:n1, 0:n1] = K1
        K[n1:n, n1:n] = K2
        for i in xrange(n1):
            for j in xrange(n1, n):
                K[i,j] = kernel(X[i,:], X[j,:])
                K[j,i] = K[i,j]

        # Inelegant - improve later
        U1 = mat.sum(K[0:n1,:],0) / n1
        U2 = mat.sum(K[n1:n,:],0) / n2
        U1m = mat.tile(U1, (n1,1))
        U2m = mat.tile(U2, (n2,1))
        U = mat.bmat('U1m; U2m')
        m1m1 = mat.sum(K[0:n1, 0:n1]) / (n1*n1)
        m1m2 = mat.sum(K[0:n1, n1:n]) / (n1*n2)
        m2m2 = mat.sum(K[n1:n, n1:n]) / (n2*n2) 
        mumu = mat.zeros((n,n))
        mumu[0:n1, 0:n1] = m1m1
        mumu[0:n1, n1:n] = m1m2
        mumu[n1:n, 0:n1] = m1m2
        mumu[n1:n, n1:n] = m2m2
        Kcu = K - U
        Kuc = Kcu.T
        N = mat.ones((n,n))/n
        Kc = K - U - U.T + mumu
        return (K, Kuc, Kc)
Ejemplo n.º 52
0
    def __init__(self, Xc, yc, Xe, ye):

        # Create the data arrays
        self.Xc = np.atleast_2d(Xc).T
        self.yc = yc
        self.nc = self.Xc.shape[0]

        self.Xe = np.atleast_2d(Xe).T

        self.ye = ye
        self.ne = self.Xe.shape[0]

        # rho regression parameter
        self.rho = 1.9961
        self.reorder_data()
        # self.traincheap()

        self.k = self.Xc.shape[1]
        # if self.Xe.shape[1] != self.Xc.shape[1]:
        #     print 'Xc and Xe must have the same number of design variables. Fatal error -- Exiting...'
        #     exit()

        # Configure the hyperparameter arrays
        self.thetad = np.ones(self.k)
        self.thetac = None
        # self.thetac = self.kc.theta

        self.pd = np.ones(self.k) * 2.
        # self.pc = self.kc.pl
        self.pc = np.ones(self.k) * 2.

        # Matrix Operations
        self.one=ones([self.ne+self.nc,1])
        self.y=[self.yc, self.ye]

        print 'here1'
Ejemplo n.º 53
0
def applyTransform(points, para):
    T = ml.mat(para[:3]).T;
    R = ml.mat(para[3:].reshape(3, 3)).T.I;
    result = npy.array(points)
    result[:, :3] = ml.mat(points[:, :3]) * R + ml.ones((points.shape[0], 1)) * T.T
    return result
Ejemplo n.º 54
0
def MinimalRepFromME (alpha, A, how="moment", precision=1e-12):
    """
    Returns the minimal representation of the given ME 
    distribution.
    
    Parameters
    ----------
    alpha : vector, shape (1,M)
        The initial vector of the matrix-exponential 
        distribution.
    A : matrix, shape (M,M)
        The matrix parameter of the matrix-exponential 
        distribution.
    how : {"obs", "cont", "obscont", "moment"}, optional        
        Determines how the representation is minimized. 
        Possibilities:
        'obs': observability, 
        'cont': controllability,
        'obscont': the minimum of observability and 
        controllability order,
        'moment': moment order (which is the default).
    precision : double, optional
       Precision used by the Staircase algorithm. The default
       value is 1e-12.
    
    Returns
    -------
    beta : vector, shape (1,N)
        The initial vector of the minimal representation
    B : matrix, shape (N,N)
        The matrix parameter of the minimal representation
    
    References
    ----------
    .. [1]  P. Buchholz, M. Telek, "On minimal representation
            of rational arrival processes." Madrid Conference on
            Qeueuing theory (MCQT), June 2010.
    """

    if butools.checkInput and not CheckMERepresentation (alpha, A):
        raise Exception("MinimalRepFromME: Input is not a valid ME representation!")

    if how=="cont":
        H0 = A
        H1 = np.sum(-A,1) * alpha
        B, n = MStaircase ([H0, H1], ml.ones((A.shape[0],1)), precision)
        return ((alpha*B)[0,0:n], (la.inv(B)*A*B)[0:n,0:n])
    elif how=="obs":
        H0 = A
        H1 = np.sum(-A,1) * alpha
        G = [H0.T,H1.T]
        B, n = MStaircase (G, alpha.T, precision)
        return ((alpha*B)[:,0:n], (la.inv(B)*A*B)[0:n,0:n])
    elif how=="obscont":
        alphav, Av = MinimalRepFromME (alpha, A, "cont", precision)
        return MinimalRepFromME (alphav, Av, "obs", precision)      
    elif how=="moment":
        N = MEOrder (alpha, A, "moment", precision)
        moms = MomentsFromME (alpha, A, 2*N-1)
        return MEFromMoments (moms)
        
Ejemplo n.º 55
0
    def setWidgetView(self, widget, color = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]):
        super(SurfaceView, self).setWidgetView(widget)
        
        if type(self.parent) is MdiChildRegistration:
            point_array_move = self.parent.getData('move').pointSet
            point_data_move = npy.array(point_array_move.getData('Contour'))
            point_data_result = npy.array(point_data_move)
            
            if point_data_result is None or not point_data_result.shape[0]:
                return
                
            self.spacing_mov = self.parent.getData('move').getResolution().tolist()
            self.spacing = self.parent.getData().getResolution().tolist()
            
            para = npy.array(self.parent.getData().getInfo().getData('transform'))
            R = ml.mat(para[:9].reshape(3, 3))
            T = ml.mat(para[9:12].reshape(3, 1))
            
            T = R.I * T
            T = -T
            point_data_result[:, :3] *= self.spacing_mov[:3]
            point_data_result[:, :3] = ml.mat(point_data_result[:, :3]) * R + ml.ones((point_data_result.shape[0], 1)) * T.T
            point_data_result[:, :3] /= self.spacing[:3]
        else:
            point_array_result = self.parent.getData().pointSet
            point_data_result = npy.array(point_array_result.getData('Contour'))
            point_array_move = point_array_result
            point_data_move = npy.array(point_array_move.getData('Contour'))

        
        zmin = int(npy.min(point_data_move[:, 2]) + 0.5)
        zmax = int(npy.max(point_data_move[:, 2]) + 0.5)
        self.spacing = self.parent.getData().getResolution().tolist()
        self.spacing = [float(x) / self.spacing[-1] for x in self.spacing]
        point_data_result[:, :2] *= self.spacing[:2]
        
        self.renderer = vtk.vtkRenderer()
        self.render_window = widget.GetRenderWindow()
        self.render_window.AddRenderer(self.renderer)
        self.window_interactor = vtk.vtkRenderWindowInteractor()
        self.render_window.SetInteractor(self.window_interactor)
        
        self.contours = []
        self.delaunay3D = []
        self.delaunayMapper = []
        self.surface_actor = []
        
        for cnt in range(3):
            self.contours.append(vtk.vtkPolyData())
            self.delaunay3D.append(vtk.vtkDelaunay3D())
            self.delaunayMapper.append(vtk.vtkDataSetMapper())
            self.surface_actor.append(vtk.vtkActor())
            
            point_result = point_data_result[npy.where(npy.round(point_data_result[:, -1]) == cnt)]
            point_move = point_data_move[npy.where(npy.round(point_data_move[:, -1]) == cnt)]
            if not point_result.shape[0]:
                continue
                
            self.cells = vtk.vtkCellArray()
            self.points = vtk.vtkPoints()
            l = 0
            for i in range(zmin, zmax + 1):
                data = point_result[npy.where(npy.round(point_move[:, 2]) == i)]
                if data is not None:
                    if data.shape[0] == 0:
                        continue
                    count = data.shape[0]
                    points = vtk.vtkPoints()
                    for j in range(count):
                        points.InsertPoint(j, data[j, 0], data[j, 1], data[j, 2])
                    
                    para_spline = vtk.vtkParametricSpline()
                    para_spline.SetPoints(points)
                    para_spline.ClosedOn()
                    
                    # The number of output points set to 10 times of input points
                    numberOfOutputPoints = count * 10
                    self.cells.InsertNextCell(numberOfOutputPoints)
                    for k in range(0, numberOfOutputPoints):
                        t = k * 1.0 / numberOfOutputPoints
                        pt = [0.0, 0.0, 0.0]
                        para_spline.Evaluate([t, t, t], pt, [0] * 9)
                        if pt[0] != pt[0]:
                            print pt
                            continue
                        self.points.InsertPoint(l, pt[0], pt[1], pt[2])
                        self.cells.InsertCellPoint(l)
                        l += 1

            self.contours[cnt].SetPoints(self.points)
            self.contours[cnt].SetPolys(self.cells)
            
            self.delaunay3D[cnt].SetInput(self.contours[cnt])
            self.delaunay3D[cnt].SetAlpha(2)
            
            self.delaunayMapper[cnt].SetInput(self.delaunay3D[cnt].GetOutput())
            
            self.surface_actor[cnt].SetMapper(self.delaunayMapper[cnt])
            self.surface_actor[cnt].GetProperty().SetDiffuseColor(color[cnt][0], color[cnt][1], color[cnt][2])
            self.surface_actor[cnt].GetProperty().SetSpecularColor(1, 1, 1)
            self.surface_actor[cnt].GetProperty().SetSpecular(0.4)
            self.surface_actor[cnt].GetProperty().SetSpecularPower(50)
            
            self.renderer.AddViewProp(self.surface_actor[cnt])
        
        self.renderer.ResetCamera()
        point = self.renderer.GetActiveCamera().GetFocalPoint()
        dis = self.renderer.GetActiveCamera().GetDistance()
        self.renderer.GetActiveCamera().SetViewUp(0, 0, 1)
        self.renderer.GetActiveCamera().SetPosition(point[0], point[1] - dis, point[2])
        self.renderer.ResetCameraClippingRange()
        self.render_window.Render()
        
        # Manually set to trackball style
        self.window_interactor.SetKeyCode('t')
        self.window_interactor.CharEvent()
        self.window_interactor.GetInteractorStyle().AddObserver("KeyPressEvent", self.KeyPressCallback)
        self.window_interactor.GetInteractorStyle().AddObserver("CharEvent", self.KeyPressCallback)
Ejemplo n.º 56
0
def SPIRIT(A, lamb, energy, k0=1, holdOffTime=0, reorthog=False, evalMetrics="F"):

    A = np.mat(A)

    n = A.shape[1]
    totalTime = A.shape[0]
    Proj = npm.ones((totalTime, n)) * np.nan
    recon = npm.zeros((totalTime, n))

    # initialize w_i to unit vectors
    W = npm.eye(n)
    d = 0.01 * npm.ones((n, 1))
    m = k0  # number of eigencomponents

    relErrors = npm.zeros((totalTime, 1))

    sumYSq = 0.0
    E_t = []
    sumXSq = 0.0
    E_dash_t = []

    res = {}
    k_hist = []
    W_hist = []
    anomalies = []

    # incremental update W
    lastChangeAt = 0

    for t in range(totalTime):

        k_hist.append(m)

        # update W for each y_t
        x = A[t, :].T  # new data as column vector

        for j in range(m):
            W[:, j], d[j], x = updateW(x, W[:, j], d[j], lamb)
            Wj = W[:, j]

        # Grams smit reorthog
        if reorthog == True:
            W[:, :m], R = npm.linalg.qr(W[:, :m])

        # compute low-D projection, reconstruction and relative error
        Y = W[:, :m].T * A[t, :].T  # project to m-dimensional space
        xActual = A[t, :].T  # actual vector of the current time
        xProj = W[:, :m] * Y  # reconstruction of the current time
        Proj[t, :m] = Y.T
        recon[t, :] = xProj.T
        xOrth = xActual - xProj
        relErrors[t] = npm.sum(npm.power(xOrth, 2)) / npm.sum(npm.power(xActual, 2))

        # update energy
        sumYSq = lamb * sumYSq + npm.sum(npm.power(Y, 2))
        E_dash_t.append(sumYSq)
        sumXSq = lamb * sumXSq + npm.sum(npm.power(A[t, :], 2))
        E_t.append(sumXSq)

        # Record RSRE
        if t == 0:
            top = 0.0
            bot = 0.0

        top = top + npm.power(npm.linalg.norm(xActual - xProj), 2)

        bot = bot + npm.power(npm.linalg.norm(xActual), 2)

        new_RSRE = top / bot

        if t == 0:
            RSRE = new_RSRE
        else:
            RSRE = npm.vstack((RSRE, new_RSRE))

        ### Metric EVALUATION ###
        # deviation from truth
        if evalMetrics == "T":

            Qt = W[:, :m]

            if t == 0:
                res["subspace_error"] = npm.zeros((totalTime, 1))
                res["orthog_error"] = npm.zeros((totalTime, 1))
                res["angle_error"] = npm.zeros((totalTime, 1))
                Cov_mat = npm.zeros([n, n])

            # Calculate Covarentce Matrix of data up to time t
            Cov_mat = lamb * Cov_mat + npm.dot(xActual, xActual.T)
            # Get eigenvalues and eigenvectors
            WW, V = npm.linalg.eig(Cov_mat)
            # Use this to sort eigenVectors in according to deccending eigenvalue
            eig_idx = WW.argsort()  # Get sort index
            eig_idx = eig_idx[::-1]  # Reverse order (default is accending)
            # v_r = highest r eigen vectors (accoring to thier eigenvalue if sorted).
            V_k = V[:, eig_idx[:m]]
            # Calculate subspace error
            C = npm.dot(V_k, V_k.T) - npm.dot(Qt, Qt.T)
            res["subspace_error"][t, 0] = 10 * np.log10(npm.trace(npm.dot(C.T, C)))  # frobenius norm in dB
            # Calculate angle between projection matrixes
            D = npm.dot(npm.dot(npm.dot(V_k.T, Qt), Qt.T), V_k)
            eigVal, eigVec = npm.linalg.eig(D)
            angle = npm.arccos(np.sqrt(max(eigVal)))
            res["angle_error"][t, 0] = angle

            # Calculate deviation from orthonormality
            F = npm.dot(Qt.T, Qt) - npm.eye(m)
            res["orthog_error"][t, 0] = 10 * np.log10(npm.trace(npm.dot(F.T, F)))  # frobenius norm in dB

        # Energy thresholding
        ######################
        # check the lower bound of energy level
        if sumYSq < energy[0] * sumXSq and lastChangeAt < t - holdOffTime and m < n:
            lastChangeAt = t
            m = m + 1
            anomalies.append(t)
        # print 'Increasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq)
        # check the upper bound of energy level
        elif sumYSq > energy[1] * sumXSq and lastChangeAt < t - holdOffTime and m < n and m > 1:
            lastChangeAt = t
            m = m - 1
        # print 'Decreasing m to %d at time %d (ratio %6.2f)\n' % (m, t, 100 * sumYSq/sumXSq)
        W_hist.append(W[:, :m])
    # set outputs

    # Grams smit reorthog
    if reorthog == True:
        W[:, :m], R = npm.linalg.qr(W[:, :m])

    # Data Stores
    res2 = {
        "hidden": Proj,  # Array for hidden Variables
        "E_t": np.array(E_t),  # total energy of data
        "E_dash_t": np.array(E_dash_t),  # hidden var energy
        "e_ratio": np.array(E_dash_t) / np.array(E_t),  # Energy ratio
        "rel_orth_err": relErrors,  # orthoX error
        "RSRE": RSRE,  # Relative squared Reconstruction error
        "recon": recon,  # reconstructed data
        "r_hist": k_hist,  # history of r values
        "W_hist": W_hist,  # history of Weights
        "anomalies": anomalies,
    }

    res.update(res2)

    return res
Ejemplo n.º 57
0
def GeneralFluidSolve (Q, R, Q0=[], prec=1e-14):
    """
    Returns the parameters of the matrix-exponentially 
    distributed stationary distribution of a general 
    Markovian fluid model, where the fluid rates associated
    with the states of the background process can be
    arbitrary (zero is allowed as well).
    
    Using the returned 4 parameters the stationary
    solution can be obtained as follows.
    
    The probability that the fluid level is zero while 
    being in different states of the background process
    is given by vector mass0.
    
    The density that the fluid level is x while being in
    different states of the background process is
    
    .. math::
        \pi(x)=ini\cdot e^{K x}\cdot clo.    
    
    Parameters
    ----------
    Q : matrix, shape (N,N)
        The generator of the background Markov chain
    R : diagonal matrix, shape (N,N)
        The diagonal matrix of the fluid rates associated
        with the different states of the background process
    Q0 : matrix, shape (N,N), optional
        The generator of the background Markov chain at 
        level 0. If not provided, or empty, then Q0=Q is 
        assumed. The default value is empty.
    precision : double, optional
        Numerical precision for computing the fundamental
        matrix. The default value is 1e-14
    
    Returns
    -------
    mass0 : matrix, shape (1,Np+Nm)
        The stationary probability vector of zero level
    ini : matrix, shape (1,Np)
        The initial vector of the stationary density
    K : matrix, shape (Np,Np)
        The matrix parameter of the stationary density
    clo : matrix, shape (Np,Np+Nm)
        The closing matrix of the stationary density
    """
    
    N = Q.shape[0]
    # partition the state space according to zero, positive and negative fluid rates
    ix = np.arange(N)
    ixz = ix[np.abs(np.diag(R))<=prec]
    ixp = ix[np.diag(R)>prec]
    ixn = ix[np.diag(R)<-prec]
    Nz = len(ixz)
    Np = len(ixp)
    Nn = len(ixn)
    # permutation matrix that converts between the original and the partitioned state ordering
    P = ml.zeros((N,N))
    for i in range(Nz):
        P[i,ixz[i]]=1
    for i in range(Np):
        P[Nz+i,ixp[i]]=1
    for i in range(Nn):
        P[Nz+Np+i,ixn[i]]=1
    iP = P.I
    Qv = P*Q*iP
    Rv = P*R*iP

    # new fluid process censored to states + and -
    iQv00 = la.pinv(-Qv[:Nz,:Nz])
    Qbar = Qv[Nz:, Nz:] + Qv[Nz:,:Nz]*iQv00*Qv[:Nz,Nz:]
    absRi = Diag(np.abs(1./np.diag(Rv[Nz:,Nz:])))
    Qz = absRi * Qbar

    Psi, K, U = FluidFundamentalMatrices (Qz[:Np,:Np], Qz[:Np,Np:], Qz[Np:,:Np], Qz[Np:,Np:], "PKU", prec)

    # closing matrix
    Pm = np.hstack((ml.eye(Np), Psi)) * absRi
    iCn = absRi[Np:,Np:]
    iCp = absRi[:Np,:Np]
    clo = np.hstack(((iCp*Qv[Nz:Nz+Np,:Nz]+Psi*iCn*Qv[Nz+Np:,:Nz])*iQv00, Pm))
    
    if len(Q0)==0: # regular boundary behavior
        clo = clo * P # go back the the original state ordering

        # calculate boundary vector   
        Ua = iCn*Qv[Nz+Np:,:Nz]*iQv00*ml.ones((Nz,1)) + iCn*ml.ones((Nn,1)) + Qz[Np:,:Np]*la.inv(-K)*clo*ml.ones((Nz+Np+Nn,1))
        pm = Linsolve (ml.hstack((U,Ua)).T, ml.hstack((ml.zeros((1,Nn)),ml.ones((1,1)))).T).T

        # create the result
        mass0 = ml.hstack((pm*iCn*Qv[Nz+Np:,:Nz]*iQv00, ml.zeros((1,Np)), pm*iCn))*P
        ini = pm*Qz[Np:,:Np]        
    else:
        # solve a linear system for ini(+), pm(-) and pm(0)        
        Q0v = P*Q0*iP
        M = ml.vstack((-clo*Rv, Q0v[Nz+Np:,:], Q0v[:Nz,:]))
        Ma = ml.vstack((np.sum(la.inv(-K)*clo,1), ml.ones((Nz+Nn,1))))
        sol = Linsolve (ml.hstack((M,Ma)).T, ml.hstack((ml.zeros((1,N)),ml.ones((1,1)))).T).T;
        ini = sol[:,:Np]
        clo = clo * P
        mass0 = ml.hstack((sol[:,Np+Nn:], ml.zeros((1,Np)), sol[:,Np:Np+Nn]))*P

    return mass0, ini, K, clo
def FRHH32(streams, rr, alpha, sci = 0):
    """ Fast row-Householder Subspace Traking Algorithm, Non adaptive version 
    
    """
#===============================================================================
#     #Initialise variables and data structures 
#===============================================================================
    # check input is type float32     
    
    streams = float32(streams)
    alpha = float32(alpha)
    
    N = streams.shape[1] # No. of streams 
    
    # Data Stores
    E_t = [float32(0)] # time series of total energy 
    E_dash_t = [float32(0)] # time series of reconstructed energy
    z_dash = npm.zeros((1,N), dtype = float32) # time series of reconstructed data 
    RSRE = mat([float32(0)])  # time series of Root squared Reconstruction Error
    hid_var = npm.zeros((streams.shape[0], N), dtype = float32) # Array of hidden Variables 
    
    seed(111)
     
    # Initial Q(0) - either random or I
    
    # Random     
    qq,RR = qr(rand(N,rr))   # generate random orthonormal matrix N x r 
    Q_t = [mat(float32(qq))]   # Initialise Q_t - N x r
    
    # Identity     
    # q_I = npm.eye(N, rr) 
    # Q_t = [q_I]

    S_t = [npm.ones((rr,rr), dtype = float32) * float32(0.00001)]   # Initialise S_t - r x r 
    
    No_inp_count = 0 # count of number of times there was no input i.e. z_t = [0,...,0]
    No_inp_marker = zeros((1,streams.shape[0] + 1))
    
    v_vec_min_1 = npm.zeros((rr,1), dtype = float32)
    
    iter_streams = iter(streams)
    
    for t in range(1, streams.shape[0] + 1):
        
        z_vec = mat(iter_streams.next())
        
        z_vec = z_vec.T # Now a column Vector
        
        hh = Q_t[t-1].T * z_vec                  # 13a

        Z = z_vec.T * z_vec - hh.T * hh           # 13b
        
        # Z = float(Z) # cheak that Z is really scalar
        
        if Z > 0.00000000001 :       
            
            # Refined version, sci accounts better for tracked eigen values
            if sci != 0: 
                u_vec = S_t[t-1] * v_vec_min_1 
                extra_term = 2 * alpha * sci * u_vec * v_vec_min_1.T
                extra_term = float32(extra_term)
            else:
                extra_term = float32(0)
                
            X = alpha * S_t[t-1]  + hh * hh.T - extra_term
            
            # QR method - hopefully more stable 
            aa = X.T
            b = sqrt(Z[0,0]) * hh
            
            # b_vec = solve(aa,b)
            b_vec = QRsolveM(aa,b)   
            
            b_vec =float32(b_vec)           
            
            beta = float32(4) * (b_vec.T * b_vec + 1)
            
            phi_sq_t = float32(0.5) + (float32(1.0) / sqrt(beta))
            
            phi_t = sqrt(phi_sq_t) 
            
            gamma = (float32(1) - float32(2) * phi_sq_t) / (float32(2) * phi_t)   
            
            delta = phi_t / sqrt(Z)          
            
            v_vec_t = multiply(gamma , b_vec)
            
            S_t.append(X - multiply(float32(1) /delta , v_vec_t * hh.T))         
            
            w_vec = multiply(delta , hh) - v_vec_t        
            
            e_vec = multiply(delta, z_vec) - (Q_t[t-1] * w_vec)
            
            Q_t.append(Q_t[t-1] - float32(2) * (e_vec * v_vec_t.T))
        
            v_vec_min_1 = v_vec_t # update for next time step
        
            # Record hidden variables
            hid_var[t-1,:hh.shape[0]] = hh.T
                
            # Record reconstrunted z 
            new_z_dash = Q_t[t-1] * hh
            z_dash = npm.vstack((z_dash, new_z_dash.T))
        
            # Record RSRE
            new_RSRE = RSRE[0,-1] + (((norm(new_z_dash - z_vec)) ** 2) / 
                                        (norm(z_vec) ** 2))                           
            RSRE = npm.vstack((RSRE, mat(new_RSRE))) 
        
        else:
            
            # Record hidden variables
            hid_var[t-1,:hh.shape[0]] = hh.T
            
            # Record reconstrunted z 
            new_z_dash = Q_t[t-1] * hh
            z_dash = npm.vstack((z_dash, new_z_dash.T))
        
            # Record RSRE
            new_RSRE = RSRE[0,-1] + (((norm(new_z_dash - z_vec)) ** 2) / 
                                    (norm(z_vec) ** 2))                           
            RSRE = npm.vstack((RSRE, mat(new_RSRE)))            
            
            # Repeat last entries
            Q_t.append(Q_t[-1])
            S_t.append(S_t[-1])            
                         
            # increment count
            No_inp_count += 1                        
            No_inp_marker[t-1] = 1 
            
    # convert to tuples to save memory        
    Q_t = tuple(Q_t)
    S_t = tuple(S_t)
    rr = array(rr)
    E_t = array(E_t)
    E_dash_t = array(E_dash_t)
            
    return  Q_t, S_t, rr, E_t, E_dash_t, hid_var, z_dash, RSRE, No_inp_count, No_inp_marker