예제 #1
0
    def __init__(self, sigma, arroots, maroots):
        self.arroots = np.atleast_1d(arroots).astype(np.complex128)
        self.maroots = np.atleast_1d(maroots).astype(np.complex128)

        self.p = len(arroots)
        self.q = len(maroots)

        if self.q >= self.p:
            raise ValueError("q must be less than p")
        if not np.all(self.arroots.real < 0.0):
            raise ValueError("arroots must have a negative real part")
        if not np.all(self.maroots.real <= 0.0):
            raise ValueError("maroots must have a negative real part")

        self.alpha = polyfromroots(self.arroots)
        self.alpha /= self.alpha[-1]
        self.beta = polyfromroots(self.maroots)
        self.beta /= self.beta[0]
        self.b = np.concatenate((self.beta, np.zeros(self.p - self.q - 1)))

        U = np.atleast_1d(arroots)[None, :]**np.arange(self.p)[:, None]
        self.b = np.dot(self.b, U)

        e = np.zeros(self.p, dtype=np.complex128)
        e[-1] = sigma
        J = np.linalg.solve(U, e)

        self.V = -J[:, None] * np.conjugate(J)[None, :]
        self.V /= (self.arroots[:, None] + np.conjugate(self.arroots)[None, :])

        self.reset()
예제 #2
0
파일: carma.py 프로젝트: dfm/carma
    def __init__(self, sigma, arroots, maroots):
        self.arroots = np.atleast_1d(arroots).astype(np.complex128)
        self.maroots = np.atleast_1d(maroots).astype(np.complex128)

        self.p = len(arroots)
        self.q = len(maroots)

        if self.q >= self.p:
            raise ValueError("q must be less than p")
        if not np.all(self.arroots.real < 0.0):
            raise ValueError("arroots must have a negative real part")
        if not np.all(self.maroots.real <= 0.0):
            raise ValueError("maroots must have a negative real part")

        self.alpha = polyfromroots(self.arroots)
        self.alpha /= self.alpha[-1]
        self.beta = polyfromroots(self.maroots)
        self.beta /= self.beta[0]
        self.b = np.concatenate((self.beta, np.zeros(self.p - self.q - 1)))

        U = np.atleast_1d(arroots)[None, :] ** np.arange(self.p)[:, None]
        self.b = np.dot(self.b, U)

        e = np.zeros(self.p, dtype=np.complex128)
        e[-1] = sigma
        J = np.linalg.solve(U, e)

        self.V = -J[:, None] * np.conjugate(J)[None, :]
        self.V /= (self.arroots[:, None] + np.conjugate(self.arroots)[None, :])

        self.reset()
예제 #3
0
파일: linear.py 프로젝트: R6500/PyJupyter
def linFromPZ(poles=[], zeros=[], gain=1.0, wgain=0, ingain=None):
    """
    @linFromPZ
    linFromPZ(poles,zeros,gain,ingain)
    Creates a system from the list of poles and zeros

    Parameters:
      poles : List of poles
      zeros : List of zeros 

    Gain can be defined as:
      gain : Gain defined as the quotient of first num/den coef.
     wgain : Frequency where gain is defined 
     igain : Gain defined at infinite freq. in high pass 

    Returns a linblk object 
    """
    # Create new block
    s = linblk()
    s.den = P.Polynomial(P.polyfromroots(poles))
    s.num = P.Polynomial(P.polyfromroots(zeros))
    # Add gain
    if ingain == None:
        #curr = s.gain()
        curr = np.abs(s.eval(1j * wgain))
        s.num = s.num * gain / curr
    else:
        curr = s.num.coef[-1] / s.den.coef[-1]
        s.num = s.num * gain / curr
    return s
예제 #4
0
 def test_polyfromroots(self):
     res = poly.polyfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5):
         roots = np.cos(np.linspace(-np.pi, 0, 2 * i + 1)[1::2])
         tgt = Tlist[i]
         res = poly.polyfromroots(roots) * 2 ** (i - 1)
         assert_almost_equal(trim(res), trim(tgt))
예제 #5
0
 def test_polyfromroots(self) :
     res = poly.polyfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1,5) :
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         tgt = Tlist[i]
         res = poly.polyfromroots(roots)*2**(i-1)
         assert_almost_equal(trim(res),trim(tgt))
예제 #6
0
def lsp_to_lpc(lsp):
    """Convert line spectral pairs to LPC"""
    ps = np.concatenate((lsp[:, 0], -lsp[::-1, 0], [np.pi]))
    qs = np.concatenate((lsp[:, 1], [0], -lsp[::-1, 1]))

    p = np.cos(ps) - np.sin(ps) * 1.0j
    q = np.cos(qs) - np.sin(qs) * 1.0j

    p = np.real(P.polyfromroots(p))
    q = -np.real(P.polyfromroots(q))

    a = 0.5 * (p + q)
    return a[:-1]
예제 #7
0
 def __init__(self, real_part, imag_part):
     self.real_part = real_part
     if imag_part < 0:
         self.imag_part = -imag_part
     else:
         self.imag_part = imag_part
     # the polynomial is stored in standard order
     if self.imag_part != 0.0:
         self.poly_coef = polyfromroots([
             np.complex(self.real_part, self.imag_part),
             np.complex(self.real_part, -self.imag_part),
         ]).real
     else:
         self.poly_coef = polyfromroots([self.real_part]).real
예제 #8
0
def process(seed, K):
    """
    K is model order / number of zeros
    """

    print(K, end=" ")

    # create the dirac locations with many, many points
    rng = np.random.RandomState(seed)
    tk = np.sort(rng.rand(K) * period)

    # true zeros
    uk = np.exp(-1j * 2 * np.pi * tk / period)
    coef_poly = poly.polyfromroots(uk)  # more accurate than np.poly

    # estimate zeros
    uk_hat = np.roots(np.flipud(coef_poly))

    # place on unit circle?
    uk_hat_unit = uk_hat / np.abs(uk_hat)

    # compute error
    min_dev_norm = distance(uk, uk_hat)[0]
    _err_roots = 20 * np.log10(np.linalg.norm(uk) / min_dev_norm)

    min_dev_norm = distance(uk, uk_hat_unit)[0]
    _err_unit = 20 * np.log10(np.linalg.norm(uk) / min_dev_norm)

    return _err_roots, _err_unit
예제 #9
0
파일: client.py 프로젝트: JJK96/SDM
    def _build_index(self, L, client_id: str = None):
        """
        This function takes as input:
        o Keyword list `L`
        o System parameter PM = {`self.PKs`, `self.SKg`}

        This function outputs secure index `IL`
        """
        # assert len(L) == self.PKs['l'], "Keyword list should be l long"
        if client_id is None:
            client_id = self.id

        SKg = self.SKg
        α = SKg['α']

        roots = [int(α * hash_Zn(client_id, self.PKs['group']))]
        for i in range(1, self.PKs['l']):
            if i < len(L) + 1:
                word = L[i - 1]
                print(word)
            else:
                word = '⊥'
            roots.append(int(α * hash_Zn(word, self.PKs['group'])))

        polynomial_coefficients = list(polyfromroots(roots))

        rs = num_Zn_star_not_one(self.PKs['q'], self.PKs['group'].random, ZR)

        g = self.PKs['g']

        IL = [
            g**(rs * self.PKs['group'].init(ZR, i))
            for i in polynomial_coefficients
        ]
        return IL
예제 #10
0
def process(seed, K):
    """
    K is model order / number of zeros
    """

    # create the dirac locations with many, many points
    rng = np.random.RandomState(seed)
    tk = np.sort(rng.rand(K)*period)

    # true zeros
    uk = np.exp(-1j*2*np.pi*tk/period)
    coef_poly = poly.polyfromroots(uk)   # more accurate than np.poly
    
    # estimate zeros
    uk_hat = np.roots(np.flipud(coef_poly))
    uk_hat_poly = poly.polyroots(coef_poly)
    uk_hat_mpmath = mpmath.polyroots(np.flipud(coef_poly), maxsteps=100, 
        cleanup=True, error=False, extraprec=50)

    # compute error
    min_dev_norm = distance(uk, uk_hat)[0]
    _err_roots = 20*np.log10(np.linalg.norm(uk)/min_dev_norm)

    min_dev_norm = distance(uk, uk_hat_poly)[0]
    _err_poly = 20*np.log10(np.linalg.norm(uk)/min_dev_norm)

    # for mpmath, need to compute error with its precision
    uk = np.sort(uk)
    uk_mpmath = [mpmath.mpc(z) for z in uk]
    uk_hat_mpmath = sorted(uk_hat_mpmath, key=cmp_to_key(compare_mpc))
    dev = [uk_mpmath[k] - uk_hat_mpmath[k] for k in range(len(uk_mpmath))]
    _err_mpmath = 20*mpmath.log(mpmath.norm(uk_mpmath) / mpmath.norm(dev), 
            b=10)

    return _err_roots, _err_poly, _err_mpmath
예제 #11
0
 def test_polyroots(self) :
     assert_almost_equal(poly.polyroots([1]), [])
     assert_almost_equal(poly.polyroots([1, 2]), [-.5])
     for i in range(2,5) :
         tgt = np.linspace(-1, 1, i)
         res = poly.polyroots(poly.polyfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
예제 #12
0
파일: key.py 프로젝트: nekorobov/HomoML
def newkey(sec=18, N=2):
    '''
    Generates a secret key with a security parameter sec.
    sec parameter defines amount of complex roots of the private key's polynomial part.

    Result of this function is a key with three fields: polynomial, base, secret encryption number.

    :param sec: security parameter
    :return: secret key object
    '''
    random.seed(SEED)
    r = random.randint(2, N)
    deg = nxtodd(sec)
    sec = (int)((deg - 1) / 2)
    roots = (random.randint(1, r), )
    key = [0 for x in range(deg + 1)]
    for i in range(sec):
        roots += complexroot(r)
    poly = nppp.polyfromroots(roots)
    for d in range(deg + 1):
        key[d] = int(poly[d].real)
    key[0] += r
    coefs = []
    for i in range(len(key)):
        coefs.append(key[i])
    return (coefs, roots[0], r)
예제 #13
0
    def __init__(self, poly_degree=7, numPoints = 300, numTrain = None, h5=None):
        if h5 is not None:
            self.h5read(h5)
            return
        assert poly_degree > 0, "must have at least 1 root"
        if numTrain is None:
            numTrain = max(1,int(.1 * numPoints))
        assert 3*numTrain < numPoints, "numTrain must be < 3*numPoints to produce test/train/cv sets"
        # make a polynomial with the given number of roots, but no root at 0
        poly_roots = list(np.arange(poly_degree + 1) - int(poly_degree//2))
        poly_roots.remove(0)
        poly_roots = np.array(poly_roots)
        poly_coeffs = polynomial.polyfromroots(poly_roots)

        self.x_all = vec2columnMat(np.linspace(start = poly_roots[0],
                                                   stop =  poly_roots[-1],
                                                   num = 300))
        self.y_all = polynomial.polyval(self.x_all[:], poly_coeffs)
        inds = range(len(self.x_all))
        random.shuffle(inds)
        self.x_train = vec2columnMat(self.x_all[:][inds[0:numTrain]])
        self.y_train = vec2columnMat(self.y_all[:][inds[0:numTrain]])
        self.x_test = vec2columnMat(self.x_all[:][inds[numTrain:(2*numTrain)]])
        self.y_test = vec2columnMat(self.y_all[:][inds[numTrain:(2*numTrain)]])
        self.x_cv = vec2columnMat(self.x_all[:][inds[(2*numTrain):(3*numTrain)]])
        self.y_cv = vec2columnMat(self.y_all[:][inds[(2*numTrain):(3*numTrain)]])
예제 #14
0
def get_polynomials(orbit_to_amount_dictionary):
    cycle_roots = [
        get_cycle_roots(lengths) for lengths in orbit_to_amount_dictionary
    ]
    new_eigenvalues = [[np.real(x + 1 / x) for x in y] for y in cycle_roots]
    new_polynomials = [np.round(polyfromroots(x)) for x in new_eigenvalues]
    return new_polynomials
예제 #15
0
def lagrange_interpolation(a, b, n, f, cheby):
    """
        Encuentra el polinomio interpolador de 'f' en [a, b] usando polinomios de lagrange de orden n
        @param cheby: True si se usan nodos de chebyshev, de lo contrario equiespaciados
        @return (polinomio, (nodos_x, f(nodos_x)))
            Los coeficientes del polinomio son: [an,..., a1, a0] que el orden en que np.polyval() recibe los coeficientes 
            
        ¡Esta función fue realizada por Rafael Nicolás Trozzo, yo simplemente la copie y pegué.!
    """
    if cheby:
        zj = [
            np.cos((2 * j + 1) / (2 * (n + 1)) * np.pi) for j in range(n + 1)
        ]
        x_nodes = [(b + a) / 2 + (b - a) / 2 * z for z in zj]
    else:
        x_nodes = np.linspace(a, b, n + 1)
    y_nodes = [f(x) for x in x_nodes]
    # hay n+1 polinomios con n+1 coeficientes
    lag_pols = np.zeros((n + 1, n + 1))
    for k in range(len(x_nodes)):
        # Formamos el polinomio de lagrange k a partir de sus raices
        lag_roots = np.delete(x_nodes, k)
        lag_pols[k] = np.flip(
            poly.polyfromroots(lag_roots)) / np.prod(x_nodes[k] -
                                                     lag_roots) * y_nodes[k]
        print(lag_pols[k]
              )  # comentar si no se quieren ver los polinomios de Lagrange
    interp_poly = np.sum(lag_pols, axis=0)

    return (interp_poly, (x_nodes, y_nodes))
예제 #16
0
def build_signal(N, dt, comps, wgts):
    obsvd = np.zeros(N)
    ic = -1

    for comp in comps:
        ic += 1
        r_comp = comp[1]
        osc_comp = comp[0]

        l_alfa = []
        for thr in osc_comp:
            r = thr[1]
            hz = thr[0]
            th = 2 * hz * dt

            l_alfa.append(r * (np.cos(np.pi * th) + 1j * np.sin(np.pi * th)))
            l_alfa.append(r * (np.cos(np.pi * th) - 1j * np.sin(np.pi * th)))
        for r in r_comp:
            l_alfa.append(r)

        alfa = np.array(l_alfa)
        ARcoeff = (-1 * _Npp.polyfromroots(alfa)[::-1][1:]).real
        sgnlC, y = createDataAR(N, ARcoeff, 0.01, 0)
        obsvd += wgts[ic] * sgnlC

    return obsvd / np.std(obsvd)
예제 #17
0
    def runDirAlloc(self, pckl, trials=None):  ###########  RUN
        """
        """
        oo = self  #  call self oo.  takes up less room on line
        oo.setname = os.getcwd().split("/")[-1]

        oo.Cs = len(oo.freq_lims)
        oo.C = oo.Cn + oo.Cs
        oo.R = 1
        oo.k = 2 * oo.C + oo.R
        #  x0  --  Gaussian prior
        oo.u_x00 = _N.zeros(oo.k)
        oo.s2_x00 = _arl.dcyCovMat(oo.k, _N.ones(oo.k), 0.4)

        oo.loadDat(trials)
        oo.setParams()
        oo.us = pckl[0]
        oo.q2 = _N.ones(oo.TR) * pckl[1]
        oo.F0 = _N.zeros(oo.k)
        print len(pckl[2])
        for l in xrange(len(pckl[2])):
            oo.F0 += (-1 * _Npp.polyfromroots(pckl[2][l])[::-1].real)[1:]
        oo.F0 /= len(pckl[2])
        oo.aS = pckl[3]

        oo.dirichletAllocate()
예제 #18
0
 def test_polyroots(self):
     assert_almost_equal(poly.polyroots([1]), [])
     assert_almost_equal(poly.polyroots([1, 2]), [-.5])
     for i in range(2, 5):
         tgt = np.linspace(-1, 1, i)
         res = poly.polyroots(poly.polyfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
예제 #19
0
파일: linear.py 프로젝트: R6500/PyJupyter
    def clean(self, ratio=1000.0):
        """
        Eliminates poles and zeros that cancel each other
        A pole and a zero are considered equal if their distance
        is lower than 1/ratio its magnitude
           ratio : Ratio to cancel PZ (default = 1000)
        Return a new object   
        """
        gain = self.gain()
        poles = self.poles()
        zeros = self.zeros()

        # Return if there are no poles or zeros
        if len(poles) == 0: return
        if len(zeros) == 0: return

        outPoles = []  # Empty pole list
        for pole in poles:
            outZeros = []  # Empty zero list
            found = False
            for zero in zeros:
                if not found:
                    distance = np.absolute(pole - zero)
                    threshold = (np.absolute(pole) +
                                 np.absolute(zero)) / (2.0 * ratio)
                    if distance > threshold:
                        outZeros.append(zero)
                    else:
                        found = True
                else:
                    outZeros.append(zero)
            if not found:
                outPoles.append(pole)
            zeros = outZeros
        poles = outPoles

        s = linblk()
        s.den = P.Polynomial(P.polyfromroots(poles))
        s.num = P.Polynomial(P.polyfromroots(zeros))

        # Add gain
        # curr = s.num.coef[0]/s.den.coef[0]
        curr = s.gain()
        s.num = s.num * gain / curr

        return s
예제 #20
0
def _pq_completion(P):
    """
    Find polynomial Q given P such that the following matrix is unitary.
    [[P(a), i Q(a) sqrt(1-a^2)],
      i Q*(a) sqrt(1-a^2), P*(a)]]

    Args:
        P: Polynomial object P.

    Returns:
        Polynomial object Q giving described unitary matrix.
    """
    pcoefs = P.coef

    P = Polynomial(pcoefs)
    Pc = Polynomial(pcoefs.conj())
    roots = (1. - P * Pc).roots()

    # catagorize roots
    real_roots = np.array([], dtype=np.float64)
    imag_roots = np.array([], dtype=np.float64)
    cplx_roots = np.array([], dtype=np.complex128)

    tol = 1e-6
    for root in roots:
        if np.abs(np.imag(root)) < tol:
            # real roots
            real_roots = np.append(real_roots, np.real(root))
        elif np.real(root) > -tol and np.imag(root) > -tol:
            if np.real(root) < tol:
                imag_roots = np.append(imag_roots, np.imag(root))
            else:
                cplx_roots = np.append(cplx_roots, root)

    # remove root r_i = +/- 1
    ridx = np.argmin(np.abs(1. - real_roots))
    real_roots = np.delete(real_roots, ridx)
    ridx = np.argmin(np.abs(-1. - real_roots))
    real_roots = np.delete(real_roots, ridx)

    # choose real roots in +/- pairs
    real_roots = np.sort(real_roots)
    real_roots = real_roots[::2]

    # include negative conjugate of complex roots
    cplx_roots = np.r_[cplx_roots, -cplx_roots]

    # construct Q
    Q = Polynomial(
        polyfromroots(np.r_[real_roots, 1j * imag_roots, cplx_roots]))
    Qc = Polynomial(Q.coef.conj())

    # normalize
    norm = np.sqrt(
        (1 - P * Pc).coef[-1] / (Q * Qc * Polynomial([1, 0, -1])).coef[-1])

    return Q * norm
예제 #21
0
파일: realpoly.py 프로젝트: saridut/FloriPy
def wilkinsonpoly(n, kind=1):
    '''
    The Wilkinson polynomials.
    Ref: https://en.wikipedia.org/wiki/Wilkinson%27s_polynomial
    '''
    if kind == 1:
        roots = np.arange(1, (n + 1))
    elif kind == 2:
        roots = np.asarray([2**(-i) for i in range(1, (n + 1))])
    else:
        raise ValueError('Unknown value for kind')

    c = poly.polyfromroots(roots)
    return c
def test_pathological_roots(r, nroots, delta, rseed=42, tol=1E-5):
    rand = np.random.RandomState(rseed)
    roots = [root for root in rand.rand(max([int(nroots / 2), 1]))]
    for i in range(nroots - len(roots)):
        rt = roots[i] + np.sign(0.5 - rand.rand()) * delta
        if abs(rt) < 1:
            roots.append(rt)

    roots = np.sort(roots)
    p = pol.polyfromroots(roots)
    q = pol.polyfromroots(roots)

    pp = PseudoPolynomial(p=p, q=q, r=r)

    pproots = np.sort(pp.real_roots())

    print(roots, pproots)

    proots = pol.polyroots(p)
    qroots = pol.polyroots(q)

    for root in proots:
        dr = min(np.absolute(np.array(roots) - root))
        assert (dr < tol)

    for root in qroots:
        dr = min(np.absolute(np.array(roots) - root))
        assert (dr < tol)

    for root in pproots:
        assert (abs(pp(root)) < tol)

    for root in roots:
        dr = min(
            np.absolute(np.array(pproots) - root) /
            max([tol, np.absolute(root)]))
        assert (abs(pp(root)) < tol and dr < tol)
예제 #23
0
 def __init__(self, coef, coef_type='monic'):
     if len(coef) == 0:
         coef = [1.]
     elif coef_type == 'monic':
         if type(coef) == list:
             coef = coef + [1.]
         else:
             coef = np.concatenate((coef, np.array([1.])))
     elif coef_type == 'zeros':
         coef = npol.polyfromroots(coef)
     elif coef_type == 'normal':
         if np.abs(coef[-1] - 1.) > 1e-15:
             raise Exception('Coefficients don\'t define a monic polynomial')
     else:
         raise Exception('Invalid coef_type, should be \'normal\', \'monic\' or \'zeros\'.')
     super(monicPolynomial, self).__init__(coef)
예제 #24
0
def filter_coeffs(p, norm=np.sqrt(2), linphase=False):
    ''' Calculate the filter coefficients for Daubechies Wavelets

    c.f. Strang, Nguyen - "Wavelets and Filters" ch. 5.5
    :param p: number of vanishing moments
    :param norm: specifies the euclidean norm of the final coefficients
    :param linphase: whether to use the "most linear phase" approximation or minimal phase
    '''
    if p < 1:
        raise ValueError("The order of the Wavelets must be at least 1")
    if p > 34:
        raise ValueError("Sorry, the method does currently not work for orders higher than 34")
    if norm <= 0:
        raise ValueError("The norm must be larger than 0")
    # find roots of the defining polynomial B(y)
    B_y = [comb(p + i - 1, i, exact=True) for i in range(p)]
    y = poly.polyroots(B_y)
    # calculate the roots of C(z) from the roots y via a quadratic formula
    roots = [poly.polyroots([1, 4*yi - 2, 1]) for yi in y]
    if not linphase:
        # take the ones inside the unit circle
        z = [root for pair in roots for root in pair if np.abs(root) < 1]
    else:
        # sort roots according to Kateb & Lemarie-Rieusset (1995)
        # note: This does not always yield the 'most linear' set
        #       but some 'more linear phase'. I might also have misread the paper
        imaglargerzero = list(filter(lambda root: np.imag(root[0]) >= 0, roots))
        imagsmallerzero = list(filter(lambda root: np.imag(root[0]) < 0, roots))
        imaglargerzero.sort(key=lambda x: np.absolute(x[0]))
        imagsmallerzero.sort(key=lambda x: np.absolute(x[0]), reverse=True)
        sortedroots = imaglargerzero + imagsmallerzero
        # take them in some alternating manner
        z = []
        for i, root in enumerate(sortedroots):
            takelarger = ( (i+1)%4 <= 1 )
            z.append(root[takelarger*1])
    z += [-1]*p
    # put together the polynomial C(z) and normalize the coefficients
    C_z = np.real(poly.polyfromroots(z)) # imaginary part may be non-zero because of rounding errors
    C_z *= norm / sum(C_z)
    return C_z[::-1]
예제 #25
0
def AR2(f, amp, N, dt):
    """
    f in Hz
    """
    Nyqf = 0.5 / dt

    zp = amp * (_N.cos(f * _N.pi / Nyqf) + 1j * _N.sin(f * _N.pi / Nyqf))
    zn = amp * (_N.cos(f * _N.pi / Nyqf) - 1j * _N.sin(f * _N.pi / Nyqf))

    F = -1 * _Npp.polyfromroots(_N.array([zp, zn]))[::-1].real

    xc = _N.zeros(N)

    e = 0.1
    xc[0] = e * _N.random.randn()
    xc[1] = e * _N.random.randn()

    for t in range(2, N):
        xc[t] = F[1] * xc[t - 1] + F[2] * xc[t - 2] + e * _N.random.randn()
    xc /= _N.std(xc)
    return xc
예제 #26
0
def build_signal(N, comps, wgts, innovation_var=0.01, normalize=True):
    dt = 0.001
    nComps = len(comps)
    obsvd = _N.zeros((N, nComps+1))
    ic = -1

    for comp in comps:
        ic += 1

        print(comp)
        r_comp = comp[1]
        osc_comp = comp[0]

        l_alfa = []
        for thr in osc_comp:
            r = thr[1]
            hz= thr[0]
            th = 2*hz*dt

            l_alfa.append(r*(_N.cos(_N.pi*th) + 1j*_N.sin(_N.pi*th)))
            l_alfa.append(r*(_N.cos(_N.pi*th) - 1j*_N.sin(_N.pi*th)))
        for r in r_comp:
            l_alfa.append(r)

        alfa  = _N.array(l_alfa)
        ARcoeff          = (-1*_Npp.polyfromroots(alfa)[::-1][1:]).real
        sgnlC, y = createDataAR(N, ARcoeff, innovation_var, 0)
        obsvd[:, ic] = wgts[ic]*sgnlC

        obsvd[:, nComps] += obsvd[:, ic]

    if normalize:
        for ic in range(nComps):
            obsvd[:, ic] /= _N.std(obsvd[:, nComps])
        obsvd[:, nComps] /= _N.std(obsvd[:, nComps])

    if len(comps) == 1:
        return ARcoeff, obsvd
    else:
        return obsvd
    def _roots0(self):
        """
        Complex roots of pseudo-poly using `np.polynomial.polyroots` method,
        which finds the eigenvalues of the companion matrix of the root-finding
        poly.

        First finds common roots between `p` and `q` polynomials, then divides
        these roots from the `root_finding_poly`.

        Returns
        -------
        roots : list (of floats)
            (Complex) roots of the `root_finding_poly`
        p : tuple of floats
            Coefficients of the root finding polynomial
        dp : tuple of floats
            Coefficients of the derivative of the root finding polynomial
        """
        p  = self.root_finding_poly()
        dp = remove_zeros(pol.polyder(p))

        roots_p = pol.polyroots(self.p)
        roots_q = pol.polyroots(self.q)

        roots = []
        for root in roots_p:
            if any([ abs(rq - root) < 1E-5 for rq in roots_q ]):
                roots.append(root)

        pr    = remove_zeros(pol.polyfromroots(roots))
        p2, _ = pol.polydiv(p, pol.polymul(pr, pr))
        p2    = remove_zeros(p2)

        new_roots = list(pol.polyroots(p2))

        roots.extend(new_roots)

        return roots, p, dp
예제 #28
0
파일: __init__.py 프로젝트: 1950/sawbuck
def polyfromroots(roots) :
    from numpy.polynomial.polynomial import polyfromroots
    return polyfromroots(roots)
예제 #29
0
# get parameterized roots
speeds = np.array([1,4,1,4])
num_times = vid_len * frame_ps
param_roots = tf.parameterized_roots1(speeds, num_times)
max_iter = 50

# create image sequence
for i in range(num_times):
    # print progress
    img_file_name = directory + '/' + imagename + '%05d' % (i+1) + '.png'
    print 'Creating frame ' + str(i+1) + ' of ' + str(num_times)

    # create polynomial from roots
    known_roots = param_roots[i,:]
    p = np.flipud(poly.polyfromroots(known_roots))
    dp = tf.poly_der(p)

    # update param dictionary
    params['p'] = p
    params['dp'] = dp

    # newton's method
    roots, con_root, con_num = gn.newton_method(Z, f_val, df_val, params, disp_time=False, known_roots=known_roots, max_iter = max_iter)

    # create image in folder
    gn.newton_plot(con_root, con_num, colors, save_path=img_file_name, max_shade=max_iter)

# create the movie
ctrlStr = 'ffmpeg -r %d -i %s%%05d.png -c:v libx264 -preset slow -crf %d %s' %(frame_ps, directory + '/' + imagename, quality, filename)
subprocess.call(ctrlStr, shell=True)
예제 #30
0
    def gibbsSamp(
            self,
            smpls_fn_incl_trls=False):  ###########################  GIBBSSAMPH
        global interrupted
        oo = self

        signal.signal(signal.SIGINT, signal_handler)

        print("****!!!!!!!!!!!!!!!!  dohist  %s" % str(oo.dohist))

        ooTR = oo.TR
        ook = oo.k

        ooN = oo.N
        _kfar.init(oo.N, oo.k, oo.TR)
        oo.x00 = _N.array(oo.smpx[:, 2])
        oo.V00 = _N.zeros((ooTR, ook, ook))
        if oo.dohist:
            oo.loghist = _N.zeros(oo.Hbf.shape[0])
        else:
            print("fixed hist is")
            print(oo.loghist)

        print("oo.mcmcRunDir    %s" % oo.mcmcRunDir)
        if oo.mcmcRunDir is None:
            oo.mcmcRunDir = ""
        elif (len(oo.mcmcRunDir) > 0) and (oo.mcmcRunDir[-1] != "/"):
            oo.mcmcRunDir += "/"

        ARo = _N.zeros((ooTR, ooN + 1))

        kpOws = _N.empty((ooTR, ooN + 1))
        lv_f = _N.zeros((ooN + 1, ooN + 1))
        lv_u = _N.zeros((ooTR, ooTR))
        Bii = _N.zeros((ooN + 1, ooN + 1))

        #alpC.reverse()
        #  F_alfa_rep = alpR + alpC  already in right order, no?

        Wims = _N.empty((ooTR, ooN + 1, ooN + 1))
        Oms = _N.empty((ooTR, ooN + 1))
        smWimOm = _N.zeros(ooN + 1)
        smWinOn = _N.zeros(ooTR)
        bConstPSTH = False

        D_f = _N.diag(_N.ones(oo.B.shape[0]) * oo.s2_a)  #  spline
        iD_f = _N.linalg.inv(D_f)
        D_u = _N.diag(_N.ones(oo.TR) * oo.s2_u)  #  This should
        iD_u = _N.linalg.inv(D_u)
        iD_u_u_u = _N.dot(iD_u, _N.ones(oo.TR) * oo.u_u)

        if oo.bpsth:
            BDB = _N.dot(oo.B.T, _N.dot(D_f, oo.B))
            DB = _N.dot(D_f, oo.B)
            BTua = _N.dot(oo.B.T, oo.u_a)

        it = -1

        oous_rs = oo.us.reshape((ooTR, 1))
        #runTO = ooNMC + oo.burn - 1 if (burns is None) else (burns - 1)
        runTO = oo.ITERS - 1
        oo.allocateSmp(runTO + 1, Bsmpx=oo.doBsmpx)
        if cython_arc:
            _arcfs.init(ooN + 1 - oo.ignr,
                        oo.k,
                        oo.TR,
                        oo.R,
                        oo.Cs,
                        oo.Cn,
                        aro=_cd.__NF__)
            alpR = _N.array(oo.F_alfa_rep[0:oo.R])
            alpC = _N.array(oo.F_alfa_rep[oo.R:])
        else:
            alpR = oo.F_alfa_rep[0:oo.R]
            alpC = oo.F_alfa_rep[oo.R:]

        BaS = _N.zeros(oo.N + 1)  #_N.empty(oo.N+1)

        #  H shape    100 x 9
        Hbf = oo.Hbf

        RHS = _N.empty((oo.histknots, 1))

        print("-----------    histknots %d" % oo.histknots)

        cInds = _N.arange(oo.iHistKnotBeginFixed, oo.histknots)
        vInds = _N.arange(0, oo.iHistKnotBeginFixed)
        #cInds = _N.array([4, 12, 13])
        #vInds = _N.array([0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, ])
        #vInds = _N.arange(0, oo.iHistKnotBeginFixed)

        RHS[cInds, 0] = 0

        Msts = []
        for m in range(ooTR):
            Msts.append(_N.where(oo.y[m] == 1)[0])
        HcM = _N.ones((len(vInds), len(vInds)))

        HbfExpd = _N.zeros((oo.histknots, ooTR, oo.N + 1))

        #HbfExpd = _N.zeros((oo.histknots, ooTR, oo.Hbf.shape[0]))
        #  HbfExpd is 11 x M x 1200
        #  find the mean.  For the HISTORY TERM
        for i in range(oo.histknots):
            for m in range(oo.TR):
                sts = Msts[m]
                HbfExpd[i, m, 0:sts[0]] = 0
                for iss in range(len(sts) - 1):
                    t0 = sts[iss]
                    t1 = sts[iss + 1]
                    #HbfExpd[i, m, t0+1:t1+1] = Hbf[1:t1-t0+1, i]#Hbf[0:t1-t0, i]
                    HbfExpd[i, m, t0 + 1:t1 + 1] = Hbf[0:t1 - t0, i]
                HbfExpd[i, m, sts[-1] + 1:] = 0

        _N.dot(oo.B.T, oo.aS, out=BaS)
        if oo.hS is None:
            oo.hS = _N.zeros(oo.histknots)

        if oo.dohist:
            _N.dot(Hbf, oo.hS, out=oo.loghist)
        oo.stitch_Hist(ARo, oo.loghist, Msts)

        ##  ORDER OF SAMPLING
        ##  f_xx, f_V
        ##  DA:  PG, kpOws
        ##  history, build ARo
        ##  psth
        ##  offset
        ##  DA:  latent state
        ##  AR coefficients
        ##  q2

        K = _N.empty((oo.TR, oo.N + 1, oo.k))  #  kalman gain

        iterBLOCKS = oo.ITERS // oo.peek
        smpx_C_cont = _N.empty((oo.TR, oo.N + 1, oo.k))  #  need C contiguous

        #  oo.smpx[:, 1+oo.ignr:, 0:ook], oo.smpx[:, oo.ignr:, 0:ook-1]
        smpx_contiguous1 = _N.zeros((oo.TR, oo.N + 2, oo.k))
        smpx_contiguous2 = _N.zeros((oo.TR, (oo.N + 1) + 2, oo.k - 1))
        if (cython_inv_v == 3) or (cython_inv_v == 5):
            oo.if_V = _N.array(oo.f_V)
            oo.chol_L_fV = _N.array(oo.f_V)
        ######  Gibbs sampling procedure
        ttts = _N.zeros((oo.ITERS, 9))
        for itrB in range(iterBLOCKS):
            it = itrB * oo.peek
            if it > 0:
                #  0.5*oo.fs  because (dt*2)  ->  1 corresponds to Fs/2

                print("---------it: %(it)d    mnStd  %(mnstd).3f" % {
                    "it": itrB * oo.peek,
                    "mnstd": oo.mnStds[it - 1]
                })
                if not oo.noAR:
                    print(prt)
                mnttt = _N.mean(ttts[0:it - 1], axis=0)
                for ti in range(9):
                    print("t%(2)d-t%(1)d  %(ttt).4f" % {
                        "1": ti + 1,
                        "2": ti + 2,
                        "ttt": mnttt[ti]
                    })

            if interrupted:
                break
            for it in range(itrB * oo.peek, (itrB + 1) * oo.peek):
                ttt1 = _tm.time()

                itstore = it // oo.BsmpxSkp

                #  generate latent AR state
                oo.f_x[:, 0] = oo.x00
                if it == 0:
                    for m in range(ooTR):
                        oo.f_V[m, 0] = oo.s2_x00
                else:
                    oo.f_V[:, 0] = _N.mean(oo.f_V[:, 1:], axis=1)

                ###  PG latent variable sample
                ttt2 = _tm.time()

                for m in range(ooTR):
                    lw.rpg_devroye(oo.rn,
                                   oo.smpx[m, 2:, 0] + oo.us[m] + BaS +
                                   ARo[m] + oo.knownSig[m],
                                   out=oo.ws[m])  ######  devryoe
                ttt3 = _tm.time()

                if ooTR == 1:
                    oo.ws = oo.ws.reshape(1, ooN + 1)
                _N.divide(oo.kp, oo.ws, out=kpOws)

                if oo.dohist:
                    O = kpOws - oo.smpx[..., 2:, 0] - oo.us.reshape(
                        (ooTR, 1)) - BaS - oo.knownSig

                    #print(oo.ws)

                    # for i in vInds:
                    #     #print("i   %d" % i)
                    #     #print(_N.sum(HbfExpd[i]))
                    #     for j in vInds:
                    #         #print("j   %d" % j)
                    #         #print(_N.sum(HbfExpd[j]))
                    #         HcM[i-iOf, j-iOf] = _N.sum(oo.ws*HbfExpd[i]*HbfExpd[j])

                    #     RHS[i, 0] = _N.sum(oo.ws*HbfExpd[i]*O)
                    #     for cj in cInds:
                    #         RHS[i, 0] -= _N.sum(oo.ws*HbfExpd[i]*HbfExpd[cj])*RHS[cj, 0]
                    for ii in range(len(vInds)):
                        #print("i   %d" % i)
                        #print(_N.sum(HbfExpd[i]))
                        i = vInds[ii]
                        for jj in range(len(vInds)):
                            j = vInds[jj]
                            #print("j   %d" % j)
                            #print(_N.sum(HbfExpd[j]))
                            HcM[ii,
                                jj] = _N.sum(oo.ws * HbfExpd[i] * HbfExpd[j])

                        RHS[ii, 0] = _N.sum(oo.ws * HbfExpd[i] * O)
                        for cj in cInds:
                            RHS[ii, 0] -= _N.sum(
                                oo.ws * HbfExpd[i] * HbfExpd[cj]) * RHS[cj, 0]

                    # print("HbfExpd..............................")
                    # for i in range(oo.histknots):
                    #     print(_N.sum(HbfExpd[i]))
                    # print("HcM..................................")
                    # print(HcM)
                    # print("RHS..................................")
                    # print(RHS[vInds])
                    vm = _N.linalg.solve(HcM, RHS[vInds])
                    Cov = _N.linalg.inv(HcM)
                    #print vm
                    #print(Cov)
                    #print(vm[:, 0])
                    cfs = _N.random.multivariate_normal(vm[:, 0], Cov, size=1)

                    RHS[vInds, 0] = cfs[0]
                    oo.smp_hS[it] = RHS[:, 0]

                    #RHS[2:6, 0] = vm[:, 0]
                    #vv = _N.dot(Hbf, RHS)
                    #print vv.shape
                    #print oo.loghist.shape
                    _N.dot(Hbf, RHS[:, 0], out=oo.loghist)
                    oo.smp_hist[it] = oo.loghist
                    oo.stitch_Hist(ARo, oo.loghist, Msts)
                else:
                    oo.smp_hist[it] = oo.loghist
                    oo.stitch_Hist(ARo, oo.loghist, Msts)

                #  Now that we have PG variables, construct Gaussian timeseries
                #  ws(it+1)    using u(it), F0(it), smpx(it)

                #  cov matrix, prior of aS

                # oo.gau_obs = kpOws - BaS - ARo - oous_rs - oo.knownSig
                # oo.gau_var =1 / oo.ws   #  time dependent noise
                ttt4 = _tm.time()
                if oo.bpsth:
                    Oms = kpOws - oo.smpx[..., 2:,
                                          0] - ARo - oous_rs - oo.knownSig
                    _N.einsum("mn,mn->n", oo.ws, Oms, out=smWimOm)  #  sum over
                    ilv_f = _N.diag(_N.sum(oo.ws, axis=0))
                    #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                    _N.fill_diagonal(lv_f, 1. / _N.diagonal(ilv_f))
                    lm_f = _N.dot(lv_f, smWimOm)  #  nondiag of 1./Bi are inf
                    #  now sample
                    iVAR = _N.dot(oo.B, _N.dot(ilv_f, oo.B.T)) + iD_f
                    ttt4a = _tm.time()
                    VAR = _N.linalg.inv(iVAR)  #  knots x knots
                    ttt4b = _tm.time()
                    #iBDBW = _N.linalg.inv(BDB + lv_f)   # BDB not diag
                    #Mn    = oo.u_a + _N.dot(DB, _N.dot(iBDBW, lm_f - BTua))

                    #  BDB + lv_f     (N+1 x N+1)
                    #  lm_f - BTua    (N+1)
                    Mn = oo.u_a + _N.dot(
                        DB, _N.linalg.solve(BDB + lv_f, lm_f - BTua))

                    #t4c = _tm.time()

                    oo.aS = _N.random.multivariate_normal(Mn, VAR,
                                                          size=1)[0, :]
                    oo.smp_aS[it] = oo.aS
                    _N.dot(oo.B.T, oo.aS, out=BaS)

                ttt5 = _tm.time()
                ########     per trial offset sample  burns==None, only psth fit
                Ons = kpOws - oo.smpx[..., 2:, 0] - ARo - BaS - oo.knownSig

                #  solve for the mean of the distribution

                if not oo.bpsth:  # if not doing PSTH, don't constrain offset, as there are no confounds controlling offset
                    _N.einsum("mn,mn->m", oo.ws, Ons,
                              out=smWinOn)  #  sum over trials
                    ilv_u = _N.diag(_N.sum(oo.ws, axis=1))  #  var  of LL
                    #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                    _N.fill_diagonal(lv_u, 1. / _N.diagonal(ilv_u))
                    lm_u = _N.dot(
                        lv_u, smWinOn)  #  nondiag of 1./Bi are inf, mean LL
                    #  now sample
                    iVAR = ilv_u + iD_u
                    VAR = _N.linalg.inv(iVAR)  #
                    Mn = _N.dot(VAR, _N.dot(ilv_u, lm_u) + iD_u_u_u)
                    oo.us[:] = _N.random.multivariate_normal(Mn, VAR,
                                                             size=1)[0, :]
                    if not oo.bIndOffset:
                        oo.us[:] = _N.mean(oo.us)
                    oo.smp_u[it] = oo.us
                else:
                    H = _N.ones((oo.TR - 1, oo.TR - 1)) * _N.sum(oo.ws[0])
                    uRHS = _N.empty(oo.TR - 1)
                    for dd in range(1, oo.TR):
                        H[dd - 1, dd - 1] += _N.sum(oo.ws[dd])
                        uRHS[dd - 1] = _N.sum(oo.ws[dd] * Ons[dd] -
                                              oo.ws[0] * Ons[0])

                    MM = _N.linalg.solve(H, uRHS)
                    Cov = _N.linalg.inv(H)

                    oo.us[1:] = _N.random.multivariate_normal(MM, Cov, size=1)
                    oo.us[0] = -_N.sum(oo.us[1:])
                    if not oo.bIndOffset:
                        oo.us[:] = _N.mean(oo.us)
                    oo.smp_u[it] = oo.us

                # Ons  = kpOws - ARo
                # _N.einsum("mn,mn->m", oo.ws, Ons, out=smWinOn)  #  sum over trials
                # ilv_u  = _N.diag(_N.sum(oo.ws, axis=1))  #  var  of LL
                # #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                # _N.fill_diagonal(lv_u, 1./_N.diagonal(ilv_u))
                # lm_u  = _N.dot(lv_u, smWinOn)  #  nondiag of 1./Bi are inf, mean LL
                # #  now sample
                # iVAR = ilv_u + iD_u
                # VAR  = _N.linalg.inv(iVAR)  #
                # Mn    = _N.dot(VAR, _N.dot(ilv_u, lm_u) + iD_u_u_u)
                # oo.us[:]  = _N.random.multivariate_normal(Mn, VAR, size=1)[0, :]
                # if not oo.bIndOffset:
                #     oo.us[:] = _N.mean(oo.us)
                # oo.smp_u[:, it] = oo.us

                ttt6 = _tm.time()
                if not oo.noAR:
                    #  _d.F, _d.N, _d.ks,
                    #_kfar.armdl_FFBS_1itrMP(oo.gau_obs, oo.gau_var, oo.Fs, _N.linalg.inv(oo.Fs), oo.q2, oo.Ns, oo.ks, oo.f_x, oo.f_V, oo.p_x, oo.p_V, oo.smpx, K)

                    oo.gau_obs = kpOws - BaS - ARo - oous_rs - oo.knownSig
                    oo.gau_var = 1 / oo.ws  #  time dependent noise

                    #print(oo.Fs)
                    #print(_N.linalg.inv(oo.Fs))
                    if (cython_inv_v == 2):
                        _kfar.armdl_FFBS_1itrMP(oo.gau_obs, oo.gau_var, oo.Fs,
                                                _N.linalg.inv(oo.Fs), oo.q2,
                                                oo.Ns, oo.ks, oo.f_x, oo.f_V,
                                                oo.p_x, oo.p_V, smpx_C_cont, K)
                    else:
                        _kfar.armdl_FFBS_1itrMP(oo.gau_obs, oo.gau_var, oo.Fs,
                                                _N.linalg.inv(oo.Fs), oo.q2,
                                                oo.Ns, oo.ks, oo.f_x, oo.f_V,
                                                oo.chol_L_fV, oo.if_V, oo.p_x,
                                                oo.p_V, smpx_C_cont, K)

                    oo.smpx[:, 2:] = smpx_C_cont
                    oo.smpx[:, 1, 0:ook - 1] = oo.smpx[:, 2, 1:]
                    oo.smpx[:, 0, 0:ook - 2] = oo.smpx[:, 2, 2:]

                    if oo.doBsmpx and (it % oo.BsmpxSkp == 0):
                        oo.Bsmpx[it // oo.BsmpxSkp, :, 2:] = oo.smpx[:, 2:, 0]
                        #oo.Bsmpx[it // oo.BsmpxSkp, :, 2:]    = oo.smpx[:, 2:, 0]
                    stds = _N.std(oo.smpx[:, 2 + oo.ignr:, 0], axis=1)
                    oo.mnStds[it] = _N.mean(stds, axis=0)

                    ttt7 = _tm.time()
                    #print("..................................")
                    #print(alpR)
                    #print(alpC)

                    #print(alpR)
                    #print(alpC)

                    # print(oo.smpx[0, 0:20, 0])
                    # print(oo.q2)

                    if cython_arc:
                        _N.copyto(smpx_contiguous1, oo.smpx[:, 1 + oo.ignr:])
                        _N.copyto(smpx_contiguous2, oo.smpx[:, oo.ignr:,
                                                            0:ook - 1])

                        #ARcfSmpl(int N, int k, int TR, AR2lims_nmpy, smpxU, smpxW, double[::1] q2, int R, int Cs, int Cn, complex[::1] valpR, complex[::1] valpC, double sig_ph0L, double sig_ph0H, double prR_s2)

                        oo.uts[itstore], oo.wts[itstore] = _arcfs.ARcfSmpl(
                            ooN + 1 - oo.ignr, ook, oo.TR, oo.AR2lims,
                            smpx_contiguous1, smpx_contiguous2, oo.q2, oo.R,
                            oo.Cs, oo.Cn, alpR, alpC, oo.sig_ph0L, oo.sig_ph0H,
                            0.2 * 0.2)
                    else:
                        oo.uts[itstore], oo.wts[itstore] = _arcfs.ARcfSmpl(
                            ooN + 1 - oo.ignr,
                            ook,
                            oo.AR2lims,
                            oo.smpx[:, 1 + oo.ignr:, 0:ook],
                            oo.smpx[:, oo.ignr:, 0:ook - 1],
                            oo.q2,
                            oo.R,
                            oo.Cs,
                            oo.Cn,
                            alpR,
                            alpC,
                            oo.TR,
                            aro=oo.ARord,
                            sig_ph0L=oo.sig_ph0L,
                            sig_ph0H=oo.sig_ph0H)
                    #oo.F_alfa_rep = alpR + alpC   #  new constructed
                    oo.F_alfa_rep[0:oo.R] = alpR
                    oo.F_alfa_rep[oo.R:] = alpC

                    prt, rank, f, amp = ampAngRep(oo.F_alfa_rep,
                                                  oo.dt,
                                                  f_order=True)
                    #print(f)
                    #print(amp)
                    ttt8 = _tm.time()
                    #print prt
                    #ut, wt = FilteredTimeseries(ooN+1, ook, oo.smpx[:, 1:, 0:ook], oo.smpx[:, :, 0:ook-1], oo.q2, oo.R, oo.Cs, oo.Cn, alpR, alpC, oo.TR)
                    #ranks[it]    = rank
                    oo.allalfas[it] = oo.F_alfa_rep

                    for m in range(ooTR):
                        #oo.wts[m, it, :, :]   = wt[m, :, :, 0]
                        #oo.uts[m, it, :, :]   = ut[m, :, :, 0]
                        if not oo.bFixF:
                            oo.amps[it, :] = amp
                            oo.fs[it, :] = f

                    ttt9 = _tm.time()
                    oo.F0 = (-1 *
                             _Npp.polyfromroots(oo.F_alfa_rep)[::-1].real)[1:]
                    for tr in range(oo.TR):
                        oo.Fs[tr, 0] = oo.F0[:]

                    #  sample u     WE USED TO Do this after smpx
                    #  u(it+1)    using ws(it+1), F0(it), smpx(it+1), ws(it+1)

                    oo.a2 = oo.a_q2 + 0.5 * (ooTR * ooN + 2)  #  N + 1 - 1
                    #oo.a2 = 0.5*(ooTR*(ooN-oo.ignr) + 2)  #  N + 1 - 1
                    BB2 = oo.B_q2
                    #BB2 = 0
                    for m in range(ooTR):
                        #   set x00
                        oo.x00[m] = oo.smpx[m, 2] * 0.1

                        #####################    sample q2
                        rsd_stp = oo.smpx[m, 3 + oo.ignr:, 0] - _N.dot(
                            oo.smpx[m, 2 + oo.ignr:-1], oo.F0).T
                        #oo.rsds[it, m] = _N.dot(rsd_stp, rsd_stp.T)
                        BB2 += 0.5 * _N.dot(rsd_stp, rsd_stp.T)

                    oo.q2[:] = _ss.invgamma.rvs(oo.a2, scale=BB2)
                    oo.smp_q2[it] = oo.q2
                    ttt10 = _tm.time()
                else:
                    ttt7 = ttt8 = ttt9 = ttt10 = ttt6

                ttt10 = _tm.time()
                ttts[it, 0] = ttt2 - ttt1
                ttts[it, 1] = ttt3 - ttt2
                ttts[it, 2] = ttt4 - ttt3
                ttts[it, 3] = ttt5 - ttt4
                ttts[it, 4] = ttt6 - ttt5
                ttts[it, 5] = ttt7 - ttt6
                ttts[it, 6] = ttt8 - ttt7
                ttts[it, 7] = ttt9 - ttt8
                ttts[it, 8] = ttt10 - ttt9

            oo.last_iter = it
            if it > oo.minITERS:
                smps = _N.empty((3, it + 1))
                smps[0, :it + 1] = oo.amps[:it + 1, 0]

                smps[1, :it + 1] = oo.fs[:it + 1, 0]
                smps[2, :it + 1] = oo.mnStds[:it + 1]

                #frms = _mg.stationary_from_Z_bckwd(smps, blksz=oo.peek)
                if _mg.stationary_test(oo.amps[:it + 1, 0],
                                       oo.fs[:it + 1, 0],
                                       oo.mnStds[:it + 1],
                                       it + 1,
                                       blocksize=oo.mg_blocksize,
                                       points=oo.mg_points):
                    break
                """
                fig = _plt.figure(figsize=(8, 8))
                fig.add_subplot(3, 1, 1)
                _plt.plot(range(1, it), oo.amps[1:it, 0], color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.amps[0:it, 0], color="black", lw=3)
                _plt.ylabel("amp")
                fig.add_subplot(3, 1, 2)
                _plt.plot(range(1, it), oo.fs[1:it, 0]/(2*oo.dt), color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.fs[0:it, 0]/(2*oo.dt), color="black", lw=3)
                _plt.ylabel("f")
                fig.add_subplot(3, 1, 3)
                _plt.plot(range(1, it), oo.mnStds[1:it], color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.mnStds[0:it], color="black", lw=3)
                _plt.ylabel("amp")
                _plt.xlabel("iter")
                _plt.savefig("%(dir)stmp-fsamps%(it)d" % {"dir" : oo.mcmcRunDir, "it" : it+1})
                fig.subplots_adjust(left=0.15, bottom=0.15, right=0.95, top=0.95)
                _plt.close()
                """
                #if it - frms > oo.stationaryDuration:
                #   break

        oo.getComponents()
        oo.dump_smps(0,
                     toiter=(it + 1),
                     dir=oo.mcmcRunDir,
                     smpls_fn_incl_trls=smpls_fn_incl_trls)
예제 #31
0
    def gibbsSamp(self, N, ITER, obsvd, peek=50, skp=50):
        """
        peek 
        """
        oo = self
        oo.TR           = 1
        sig_ph0L      = -1
        sig_ph0H      = 0   #  
        oo.obsvd         = obsvd
        oo.skp = skp

        radians      = buildLims(0, oo.freq_lims, nzLimL=1., Fs=oo.Fs)
        AR2lims      = 2*_N.cos(radians)

        F_alfa_rep  = initF(oo.R, oo.C, 0).tolist()   #  init F_alfa_rep

        if ram:
            alpR        = _N.array(F_alfa_rep[0:oo.R], dtype=_N.complex)
            alpC        = _N.array(F_alfa_rep[oo.R:], dtype=_N.complex)
            alpC_tmp    = _N.array(F_alfa_rep[oo.R:], dtype=_N.complex)
        else:
            alpR        = F_alfa_rep[0:oo.R]
            alpC        = F_alfa_rep[oo.R:]
            alpC_tmp        = list(F_alfa_rep[oo.R:])
        q2          = _N.array([0.01])

        oo.smpx        = _N.empty((oo.TR, N+2, oo.k))

        oo.fs           = _N.empty((ITER//skp, oo.C))
        oo.rs           = _N.empty((ITER//skp, oo.R))
        oo.amps         = _N.empty((ITER//skp, oo.C))
        oo.q2s          = _N.empty(ITER//skp)
        oo.uts          = _N.empty((ITER//skp, oo.TR, oo.R, N+1, 1))
        oo.wts          = _N.empty((ITER//skp, oo.TR, oo.C, N+2, 1))

        #  oo.smpx[:, 1+oo.ignr:, 0:ook], oo.smpx[:, oo.ignr:, 0:ook-1]
        if ram:
            _arcfs.init(N, oo.k, 1, oo.R, oo.C, 0, aro=_cd.__NF__)
            smpx_contiguous1        = _N.zeros((oo.TR, N + 1, oo.k))
            smpx_contiguous2        = _N.zeros((oo.TR, N + 2, oo.k-1))

        for n in range(N):
            oo.smpx[0, n+2] = oo.obsvd[0, n:n+oo.k][::-1]
        for m in range(oo.TR):
            oo.smpx[0, 1, 0:oo.k-1]   = oo.smpx[0, 2, 1:]
            oo.smpx[0, 0, 0:oo.k-2]   = oo.smpx[0, 2, 2:]
        if ram:
            _N.copyto(smpx_contiguous1, 
                      oo.smpx[:, 1:])
            _N.copyto(smpx_contiguous2, 
                      oo.smpx[:, 0:, 0:oo.k-1])

        oo.allalfas     = _N.empty((ITER, oo.k), dtype=_N.complex)


        for it in range(ITER):
            itstore = it // skp
            if it % peek == 0:
                if it > 0:
                    print("%d  -----------------" % it)
                    print(prt)

            if ram:
                oo.uts[itstore], oo.wts[itstore] = _arcfs.ARcfSmpl(N+1, oo.k, oo.TR, AR2lims, smpx_contiguous1, smpx_contiguous2, q2, oo.R, 0, oo.C, alpR, alpC, sig_ph0L, sig_ph0H, 0.2*0.2)
            else:
                oo.uts[itstore], oo.wts[itstore] = _arcfs.ARcfSmpl(N, oo.k, AR2lims, oo.smpx[:, 1:, 0:oo.k], oo.smpx[:, :, 0:oo.k-1], q2, oo.R, oo.C, 0, alpR, alpC, oo.TR, aro=ARord, sig_ph0L=sig_ph0L, sig_ph0H=sig_ph0H)

            F_alfa_rep[0:oo.R] = alpR
            F_alfa_rep[oo.R:]  = alpC
            oo.allalfas[it] = F_alfa_rep
            #F_alfa_rep = alpR + alpC   #  new constructed
            prt, rank, f, amp = ampAngRep(F_alfa_rep, oo.dt, f_order=True)

            #  reorder

            if oo.freq_order:
                # coh = _N.where(amp > 0.95)[0]
                # slow= _N.where(f[coh] < f_thr)[0]
                # #  first, rearrange 

                for i in range(oo.C):
                    alpC_tmp[2*i] = alpC[rank[i]*2]
                    alpC_tmp[2*i+1] = alpC[rank[i]*2+1]
                for i in range(oo.C):
                    alpC[2*i] = alpC_tmp[2*i]
                    alpC[2*i+1] = alpC_tmp[2*i+1]

                oo.amps[itstore, :]  = amp[rank]
                oo.fs[itstore, :]    = 0.5*(f[rank]/oo.dt)
            else:
                oo.amps[itstore, :]  = amp
                oo.fs[itstore, :]    = 0.5*(f/oo.dt)

            oo.rs[itstore]       = alpR

            F0          = (-1*_Npp.polyfromroots(F_alfa_rep)[::-1].real)[1:]

            a2 = oo.a_q2 + 0.5*(oo.TR*N + 2)  #  N + 1 - 1
            BB2 = oo.B_q2

            for m in range(oo.TR):
                #   set x00 
                rsd_stp = oo.smpx[m, 3:, 0] - _N.dot(oo.smpx[m, 2:-1], F0).T

                BB2 += 0.5 * _N.dot(rsd_stp, rsd_stp.T)
            q2[:] = _ss.invgamma.rvs(a2, scale=BB2)

            oo.q2s[itstore] = q2[0]

        it0=0
        it1=ITER
        it0 = it0 // skp
        it1 = it1 // skp
예제 #32
0
파일: __init__.py 프로젝트: Bankq/CS6998
def polyfromroots(roots):
    from numpy.polynomial.polynomial import polyfromroots
    return polyfromroots(roots)
예제 #33
0
    def find_zeros_and_poles_(self,
                              eps_reg=1e-15,
                              eps_stop=1e-10,
                              P_estimate=0,
                              pprint=False):
        """
        Find the zeros and poles of a complex function (C->C) inside a closed curve.

        input:
            -fun: callable, the function of which the poles have to be found
            -dfun: callable, the derivative of the function
            -contours: list of callables, the contours (R->C) that constitute
                a closed curve in the complex plane
            -dcontours: list of callables, the derivatives (R->C) of the
                respective contours
            -t_params: list of arrays, the parametrizations of the contours

        !!! Hard to get stopping right !!!
        """
        # total multiplicity of zeros (number of zeros * order)
        pol_unity = monicPolynomial([])
        p_unity = pol_unity.f_polynomial()
        s0 = int(round(self.inner_prod(p_unity, p_unity).real))
        N = s0 + 2 * P_estimate
        if pprint: print('N =', N)
        # list of FOPs
        phis = []
        pols = []
        phis.append(p_unity)
        pols.append(pol_unity)  # phi_0
        # if s0 is zero
        if s0 == 0:
            n = 0
            # arithmetic mean of nodes is zero
            mu = 0.
            pol = monicPolynomial([0.])
            phis.append(pol.f_polynomial())
            pols.append(pol)  # phi_1
            zeros = np.array([])
            r = 0
            t = 1
        else:
            # compute arithmetic mean of nodes
            p_aux = monicPolynomial([0.]).f_polynomial()
            mu = self.inner_prod(p_unity, p_aux) / N
            if pprint: print('mu =', mu)
            # append first polynomial
            pol = monicPolynomial([-mu])
            phis.append(pol.f_polynomial())
            pols.append(pol)  # phi_1
            # if the algorithm quits after the first zero, it is mu
            zeros = np.array([mu])
            n = 1
            # initialization
            r = 1
            t = 0
        while r + t < N:
            if pprint: print('1.')
            # naive criterion to check if phi_r+t+1 is regular
            prod_aux, maxpsum = self.inner_prod(phis[-1],
                                                phis[-1],
                                                compute_maxpsum=True)
            # compute eigenvalues of the next pencil
            G, G1 = self.generalized_hankel_matrices(phis, pols)
            eigv = la.eigvals(G1, G)
            # check if these eigenvalues lie within the contour
            if self.points_within_contour(eigv + mu):
                # if np.abs(prod_aux)/maxpsum > eps_reg:
                if pprint: print('1.1')
                # compute next FOP in the regular way
                pol = monicPolynomial(eigv, coef_type='zeros')
                phis.append(pol.f_polynomial())
                pols.append(pol)
                r += t + 1
                t = 0
                allsmall = True
                tau = 0
                while allsmall and (r + tau) < N:
                    if pprint: print('1.1.1')
                    # if all further inner porducts are zero
                    taupol = npol.polyfromroots([mu for _ in range(tau)])
                    tauphi = monicPolynomial(
                        npol.polymul(taupol, pols[-1].coef),
                        coef_type='normal').f_polynomial()
                    ip, maxpsum = self.inner_prod(tauphi,
                                                  phis[r],
                                                  compute_maxpsum=True)
                    tau += 1
                    if np.abs(ip) / maxpsum > eps_stop:
                        allsmall = False
                if allsmall:
                    if pprint: print('1.1.2: STOP')
                    n = r
                    zeros = eigv + mu
                    t = N  # STOP
            else:
                if pprint: print('1.2')
                c = npol.polyfromroots([mu])
                pol = monicPolynomial(npol.polymul(c, pols[-1].coef),
                                      coef_type='normal')
                pols.append(pol)
                phis.append(pol.f_polynomial())
                t += 1

        # check multiplicities
        # constuct vandermonde system
        A = np.vander(zeros).T[::-1, :]
        b = np.array(
            [self.inner_prod(p_unity, lambda x, k=k: x**k) for k in range(n)])
        # solve the Vandermonde system
        nu = la.solve(A, b)
        nu = np.round(nu).real.astype(int)
        # for printing result
        if pprint:
            print('>>> Result of computation of zeros <<<')
            print('number of zeros = ', n - len(np.where(nu == 0)[0]))
            for i, zero in enumerate(zeros):
                print('zero #' + str(i + 1) + ' = ' + str(zero) +
                      ' (multiplicity = ' + str(nu[i]) + ')')
            print('')
        # eliminate spurious zeros (the ones with zero multiplicity)
        inds = np.where(nu > 0)[0]
        return zeros[inds], n, nu[inds]
예제 #34
0
    def find_zeros(self, pprint=False):
        """
        Find the zeros and poles of a complex function (C->C) inside a closed curve.

        input:
        """
        # total multiplicity of zeros (number of zeros * order)
        pol_unity = monicPolynomial([])
        p_unity = pol_unity.f_polynomial()
        residue = self.inner_prod(p_unity, p_unity)
        N = int(round(residue.real))
        accuracy = np.abs(N - residue)
        if pprint: print('N =', N, ' (accuracy =', accuracy, ')')
        # if N is zero
        if N == 0:
            n = 0
            zeros = np.array([])
        else:
            # list of FOPs
            phis = []
            pols = []
            phis.append(p_unity)
            pols.append(pol_unity)  # phi_0
            # compute arithmetic mean of nodes
            p_aux = monicPolynomial([0.]).f_polynomial()
            mu = self.inner_prod(p_unity, p_aux) / N
            if pprint: print('mu =', mu)
            # append first polynomial
            pol = monicPolynomial([-mu])
            phis.append(pol.f_polynomial())
            pols.append(pol)  # phi_1
            # if the algorithm quits after the first zero, it is mu
            zeros = np.array([mu])
            n = 1
            # initialization
            r = 1
            t = 0
            while r + t < N:
                if pprint: print(str(r + t))
                # naive criterion to check if phi_r+t+1 is regular
                prod_aux, maxpsum = self.inner_prod(phis[-1],
                                                    phis[-1],
                                                    compute_maxpsum=True)
                # compute eigenvalues of the next pencil
                G, G1 = self.generalized_hankel_matrices(phis, pols)
                eigv = la.eigvals(G1, G)
                # print eigv
                # check if these eigenvalues lie within the contour
                if self.points_within_contour(eigv + mu):
                    # if np.abs(prod_aux)/maxpsum > eps_reg:
                    if pprint: print(str(r + t) + '.1')
                    # compute next FOP in the regular way
                    pol = monicPolynomial(eigv, coef_type='zeros')
                    phis.append(pol.f_polynomial())
                    pols.append(pol)
                    r += t + 1
                    t = 0
                    n = r
                    zeros = eigv + mu
                else:
                    if pprint: print(str(r + t) + '.2')
                    c = npol.polyfromroots([mu])
                    pol = monicPolynomial(npol.polymul(c, pols[-1].coef),
                                          coef_type='normal')
                    pols.append(pol)
                    phis.append(pol.f_polynomial())
                    t += 1

        # check multiplicities
        # constuct vandermonde system
        if len(zeros) > 0:
            A = np.vander(zeros).T[::-1, :]
            b = np.array([
                self.inner_prod(p_unity, lambda x, k=k: x**k) for k in range(n)
            ])
            # solve the Vandermonde system
            nu = la.solve(A, b)
            nu = np.round(nu).real.astype(int)
            sane = (np.sum(nu) == N)
        else:
            nu = np.array([])
            sane = True
        # for printing result
        if pprint:
            print('>>> Result of computation of zeros <<<')
            print('number of zeros = ', n - len(np.where(nu == 0)[0]))
            pstring = 'yes!' if sane == 1 else 'no!'
            print('sane? ' + pstring)
            for i, zero in enumerate(zeros):
                print('zero #' + str(i + 1) + ' = ' + str(zero) +
                      ' (multiplicity = ' + str(nu[i]) + ')')
            print('')
        # eliminate spurious zeros (the ones with zero multiplicity)
        inds_ = np.argsort(zeros.real)
        # inds = np.where(nu[inds_] > 0)[0]

        if self.use_known_zeros:
            self.global_zeros = np.concatenate(
                (self.global_zeros, zeros[inds_][inds]))
            self.global_zmultiplicities = np.concatenate(
                (self.global_zmultiplicities, nu[inds_][inds]))
            iglob = np.argsort(self.global_zeros.real)
            self.global_zeros = self.global_zeros[iglob]
            self.global_zmultiplicities = self.global_zmultiplicities[iglob]

        return zeros[inds_], (n - len(np.where(nu == 0)[0]), nu[inds_], sane)
예제 #35
0
    def gibbsSamp(self):  ###########################  GIBBSSAMPH
        oo = self

        print("****!!!!!!!!!!!!!!!!  dohist  %s" % str(oo.dohist))

        ooTR = oo.TR
        ook = oo.k

        ooN = oo.N
        _kfar.init(oo.N, oo.k, oo.TR)
        oo.x00 = _N.array(oo.smpx[:, 2])
        oo.V00 = _N.zeros((ooTR, ook, ook))
        if oo.dohist:
            oo.loghist = _N.zeros(oo.N + 1)
        else:
            print("fixed hist is")
            print(oo.loghist)

        print("oo.mcmcRunDir    %s" % oo.mcmcRunDir)
        if oo.mcmcRunDir is None:
            oo.mcmcRunDir = ""
        elif (len(oo.mcmcRunDir) > 0) and (oo.mcmcRunDir[-1] != "/"):
            oo.mcmcRunDir += "/"

        ARo = _N.zeros((ooTR, ooN + 1))

        kpOws = _N.empty((ooTR, ooN + 1))
        lv_f = _N.zeros((ooN + 1, ooN + 1))
        lv_u = _N.zeros((ooTR, ooTR))
        Bii = _N.zeros((ooN + 1, ooN + 1))

        #alpC.reverse()
        #  F_alfa_rep = alpR + alpC  already in right order, no?

        Wims = _N.empty((ooTR, ooN + 1, ooN + 1))
        Oms = _N.empty((ooTR, ooN + 1))
        smWimOm = _N.zeros(ooN + 1)
        smWinOn = _N.zeros(ooTR)
        bConstPSTH = False

        D_f = _N.diag(_N.ones(oo.B.shape[0]) * oo.s2_a)  #  spline
        iD_f = _N.linalg.inv(D_f)
        D_u = _N.diag(_N.ones(oo.TR) * oo.s2_u)  #  This should
        iD_u = _N.linalg.inv(D_u)
        iD_u_u_u = _N.dot(iD_u, _N.ones(oo.TR) * oo.u_u)

        if oo.bpsth:
            BDB = _N.dot(oo.B.T, _N.dot(D_f, oo.B))
            DB = _N.dot(D_f, oo.B)
            BTua = _N.dot(oo.B.T, oo.u_a)

        it = -1

        oous_rs = oo.us.reshape((ooTR, 1))
        #runTO = ooNMC + oo.burn - 1 if (burns is None) else (burns - 1)
        runTO = oo.ITERS - 1
        oo.allocateSmp(runTO + 1, Bsmpx=oo.doBsmpx)
        alpR = oo.F_alfa_rep[0:oo.R]
        alpC = oo.F_alfa_rep[oo.R:]

        BaS = _N.zeros(oo.N + 1)  #_N.empty(oo.N+1)

        #  H shape    100 x 9
        Hbf = oo.Hbf

        RHS = _N.empty((oo.histknots, 1))

        print("-----------    histknots %d" % oo.histknots)
        if oo.h0_1 > 1:  #  no spikes in first few time bins
            print("!!!!!!!   hist scenario 1")
            #cInds = _N.array([0, 1, 5, 6, 7, 8, 9, 10])
            #cInds = _N.array([0, 4, 5, 6, 7, 8, 9])
            cInds = _N.array([0, 5, 6, 7, 8, 9])
            #vInds = _N.array([2, 3, 4])
            vInds = _N.array([1, 2, 3, 4])
            RHS[cInds, 0] = 0
            RHS[0, 0] = -5
        elif oo.hist_max_at_0:  #  no refractory period
            print("!!!!!!!   hist scenario 2")
            #cInds = _N.array([5, 6, 7, 8, 9, 10])
            cInds = _N.array([
                0,
                4,
                5,
                6,
                7,
                8,
            ])
            vInds = _N.array([1, 2, 3])
            #vInds = _N.array([0, 1, 2, 3, 4])
            RHS[cInds, 0] = 0
            RHS[0, 0] = 0
        else:
            print("!!!!!!!   hist scenario 3")
            #cInds = _N.array([5, 6, 7, 8, 9, 10])
            cInds = _N.array([
                4,
                5,
                6,
                7,
                8,
                9,
            ])
            vInds = _N.array([
                0,
                1,
                2,
                3,
            ])
            #vInds = _N.array([0, 1, 2, 3, 4])
            RHS[cInds, 0] = 0

        Msts = []
        for m in range(ooTR):
            Msts.append(_N.where(oo.y[m] == 1)[0])
        HcM = _N.empty((len(vInds), len(vInds)))

        HbfExpd = _N.zeros((oo.histknots, ooTR, oo.N + 1))
        #  HbfExpd is 11 x M x 1200
        #  find the mean.  For the HISTORY TERM
        for i in range(oo.histknots):
            for m in range(oo.TR):
                sts = Msts[m]
                HbfExpd[i, m, 0:sts[0]] = 0
                for iss in range(len(sts) - 1):
                    t0 = sts[iss]
                    t1 = sts[iss + 1]
                    #HbfExpd[i, m, t0+1:t1+1] = Hbf[1:t1-t0+1, i]#Hbf[0:t1-t0, i]
                    HbfExpd[i, m, t0 + 1:t1 + 1] = Hbf[0:t1 - t0, i]
                HbfExpd[i, m, sts[-1] + 1:] = 0

        _N.dot(oo.B.T, oo.aS, out=BaS)
        if oo.hS is None:
            oo.hS = _N.zeros(oo.histknots)

        if oo.dohist:
            _N.dot(Hbf, oo.hS, out=oo.loghist)
        oo.stitch_Hist(ARo, oo.loghist, Msts)

        ##  ORDER OF SAMPLING
        ##  f_xx, f_V
        ##  DA:  PG, kpOws
        ##  history, build ARo
        ##  psth
        ##  offset
        ##  DA:  latent state
        ##  AR coefficients
        ##  q2

        K = _N.empty((oo.TR, oo.N + 1, oo.k))  #  kalman gain

        iterBLOCKS = oo.ITERS // oo.peek
        smpx_tmp = _N.empty((oo.TR, oo.N + 1, oo.k))

        ######  Gibbs sampling procedure
        for itrB in range(iterBLOCKS):
            it = itrB * oo.peek
            if it > 0:
                print("it: %(it)d    mnStd  %(mnstd).3f" % {
                    "it": itrB * oo.peek,
                    "mnstd": oo.mnStds[it - 1]
                })

            #tttA = _tm.time()
            for it in range(itrB * oo.peek, (itrB + 1) * oo.peek):
                #ttt1 = _tm.time()

                #  generate latent AR state
                oo.f_x[:, 0] = oo.x00
                if it == 0:
                    for m in range(ooTR):
                        oo.f_V[m, 0] = oo.s2_x00
                else:
                    oo.f_V[:, 0] = _N.mean(oo.f_V[:, 1:], axis=1)

                ###  PG latent variable sample
                #ttt2 = _tm.time()

                for m in range(ooTR):
                    lw.rpg_devroye(oo.rn,
                                   oo.smpx[m, 2:, 0] + oo.us[m] + BaS +
                                   ARo[m] + oo.knownSig[m],
                                   out=oo.ws[m])  ######  devryoe
                #ttt3 = _tm.time()

                if ooTR == 1:
                    oo.ws = oo.ws.reshape(1, ooN + 1)
                _N.divide(oo.kp, oo.ws, out=kpOws)

                if oo.dohist:
                    O = kpOws - oo.smpx[..., 2:, 0] - oo.us.reshape(
                        (ooTR, 1)) - BaS - oo.knownSig
                    if it == 2000:
                        _N.savetxt("it2000.dat", O)

                    iOf = vInds[0]  #  offset HcM index with RHS index.
                    #print(oo.ws)

                    for i in vInds:
                        #print("i   %d" % i)
                        #print(_N.sum(HbfExpd[i]))
                        for j in vInds:
                            #print("j   %d" % j)
                            #print(_N.sum(HbfExpd[j]))
                            HcM[i - iOf, j - iOf] = _N.sum(oo.ws * HbfExpd[i] *
                                                           HbfExpd[j])

                        RHS[i, 0] = _N.sum(oo.ws * HbfExpd[i] * O)
                        for cj in cInds:
                            RHS[i, 0] -= _N.sum(
                                oo.ws * HbfExpd[i] * HbfExpd[cj]) * RHS[cj, 0]

                    # print("HbfExpd..............................")
                    # print(HbfExpd)
                    # print("HcM..................................")
                    # print(HcM)
                    # print("RHS..................................")
                    # print(RHS[vInds])
                    vm = _N.linalg.solve(HcM, RHS[vInds])
                    Cov = _N.linalg.inv(HcM)
                    #print vm
                    #print(Cov)
                    #print(vm[:, 0])
                    cfs = _N.random.multivariate_normal(vm[:, 0], Cov, size=1)

                    RHS[vInds, 0] = cfs[0]
                    oo.smp_hS[:, it] = RHS[:, 0]

                    #RHS[2:6, 0] = vm[:, 0]
                    #vv = _N.dot(Hbf, RHS)
                    #print vv.shape
                    #print oo.loghist.shape
                    _N.dot(Hbf, RHS[:, 0], out=oo.loghist)
                    oo.smp_hist[:, it] = oo.loghist
                    oo.stitch_Hist(ARo, oo.loghist, Msts)
                else:
                    oo.smp_hist[:, it] = oo.loghist
                    oo.stitch_Hist(ARo, oo.loghist, Msts)

                #  Now that we have PG variables, construct Gaussian timeseries
                #  ws(it+1)    using u(it), F0(it), smpx(it)

                #  cov matrix, prior of aS

                # oo.gau_obs = kpOws - BaS - ARo - oous_rs - oo.knownSig
                # oo.gau_var =1 / oo.ws   #  time dependent noise
                #ttt4 = _tm.time()
                if oo.bpsth:
                    Oms = kpOws - oo.smpx[..., 2:,
                                          0] - ARo - oous_rs - oo.knownSig
                    _N.einsum("mn,mn->n", oo.ws, Oms, out=smWimOm)  #  sum over
                    ilv_f = _N.diag(_N.sum(oo.ws, axis=0))
                    #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                    _N.fill_diagonal(lv_f, 1. / _N.diagonal(ilv_f))
                    lm_f = _N.dot(lv_f, smWimOm)  #  nondiag of 1./Bi are inf
                    #  now sample
                    iVAR = _N.dot(oo.B, _N.dot(ilv_f, oo.B.T)) + iD_f
                    #ttt4a = _tm.time()
                    VAR = _N.linalg.inv(iVAR)  #  knots x knots
                    #ttt4b = _tm.time()
                    #iBDBW = _N.linalg.inv(BDB + lv_f)   # BDB not diag
                    #Mn    = oo.u_a + _N.dot(DB, _N.dot(iBDBW, lm_f - BTua))

                    #  BDB + lv_f     (N+1 x N+1)
                    #  lm_f - BTua    (N+1)
                    Mn = oo.u_a + _N.dot(
                        DB, _N.linalg.solve(BDB + lv_f, lm_f - BTua))

                    #t4c = _tm.time()

                    oo.aS = _N.random.multivariate_normal(Mn, VAR,
                                                          size=1)[0, :]
                    oo.smp_aS[it, :] = oo.aS
                    _N.dot(oo.B.T, oo.aS, out=BaS)

                #ttt5 = _tm.time()
                ########     per trial offset sample  burns==None, only psth fit
                Ons = kpOws - oo.smpx[..., 2:, 0] - ARo - BaS - oo.knownSig

                #  solve for the mean of the distribution

                if not oo.bpsth:  # if not doing PSTH, don't constrain offset, as there are no confounds controlling offset
                    _N.einsum("mn,mn->m", oo.ws, Ons,
                              out=smWinOn)  #  sum over trials
                    ilv_u = _N.diag(_N.sum(oo.ws, axis=1))  #  var  of LL
                    #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                    _N.fill_diagonal(lv_u, 1. / _N.diagonal(ilv_u))
                    lm_u = _N.dot(
                        lv_u, smWinOn)  #  nondiag of 1./Bi are inf, mean LL
                    #  now sample
                    iVAR = ilv_u + iD_u
                    VAR = _N.linalg.inv(iVAR)  #
                    Mn = _N.dot(VAR, _N.dot(ilv_u, lm_u) + iD_u_u_u)
                    oo.us[:] = _N.random.multivariate_normal(Mn, VAR,
                                                             size=1)[0, :]
                    if not oo.bIndOffset:
                        oo.us[:] = _N.mean(oo.us)
                    oo.smp_u[:, it] = oo.us
                else:
                    H = _N.ones((oo.TR - 1, oo.TR - 1)) * _N.sum(oo.ws[0])
                    uRHS = _N.empty(oo.TR - 1)
                    for dd in range(1, oo.TR):
                        H[dd - 1, dd - 1] += _N.sum(oo.ws[dd])
                        uRHS[dd - 1] = _N.sum(oo.ws[dd] * Ons[dd] -
                                              oo.ws[0] * Ons[0])

                    MM = _N.linalg.solve(H, uRHS)
                    Cov = _N.linalg.inv(H)

                    oo.us[1:] = _N.random.multivariate_normal(MM, Cov, size=1)
                    oo.us[0] = -_N.sum(oo.us[1:])
                    if not oo.bIndOffset:
                        oo.us[:] = _N.mean(oo.us)
                    oo.smp_u[:, it] = oo.us

                # Ons  = kpOws - ARo
                # _N.einsum("mn,mn->m", oo.ws, Ons, out=smWinOn)  #  sum over trials
                # ilv_u  = _N.diag(_N.sum(oo.ws, axis=1))  #  var  of LL
                # #  diag(_N.linalg.inv(Bi)) == diag(1./Bi).  Bii = inv(Bi)
                # _N.fill_diagonal(lv_u, 1./_N.diagonal(ilv_u))
                # lm_u  = _N.dot(lv_u, smWinOn)  #  nondiag of 1./Bi are inf, mean LL
                # #  now sample
                # iVAR = ilv_u + iD_u
                # VAR  = _N.linalg.inv(iVAR)  #
                # Mn    = _N.dot(VAR, _N.dot(ilv_u, lm_u) + iD_u_u_u)
                # oo.us[:]  = _N.random.multivariate_normal(Mn, VAR, size=1)[0, :]
                # if not oo.bIndOffset:
                #     oo.us[:] = _N.mean(oo.us)
                # oo.smp_u[:, it] = oo.us

                #ttt6 = _tm.time()
                if not oo.noAR:
                    #  _d.F, _d.N, _d.ks,
                    #_kfar.armdl_FFBS_1itrMP(oo.gau_obs, oo.gau_var, oo.Fs, _N.linalg.inv(oo.Fs), oo.q2, oo.Ns, oo.ks, oo.f_x, oo.f_V, oo.p_x, oo.p_V, oo.smpx, K)

                    oo.gau_obs = kpOws - BaS - ARo - oous_rs - oo.knownSig
                    oo.gau_var = 1 / oo.ws  #  time dependent noise

                    _kfar.armdl_FFBS_1itrMP(oo.gau_obs, oo.gau_var, oo.Fs,
                                            _N.linalg.inv(oo.Fs), oo.q2, oo.Ns,
                                            oo.ks, oo.f_x, oo.f_V, oo.p_x,
                                            oo.p_V, smpx_tmp, K)

                    oo.smpx[:, 2:] = smpx_tmp
                    oo.smpx[:, 1, 0:ook - 1] = oo.smpx[:, 2, 1:]
                    oo.smpx[:, 0, 0:ook - 2] = oo.smpx[:, 2, 2:]

                    if oo.doBsmpx and (it % oo.BsmpxSkp == 0):
                        oo.Bsmpx[:, it // oo.BsmpxSkp, 2:] = oo.smpx[:, 2:, 0]
                        #oo.Bsmpx[it // oo.BsmpxSkp, :, 2:]    = oo.smpx[:, 2:, 0]
                    stds = _N.std(oo.smpx[:, 2 + oo.ignr:, 0], axis=1)
                    oo.mnStds[it] = _N.mean(stds, axis=0)

                    #ttt7 = _tm.time()
                    if not oo.bFixF:
                        #ARcfSmpl(oo.lfc, ooN+1-oo.ignr, ook, oo.AR2lims, oo.smpx[:, 1+oo.ignr:, 0:ook], oo.smpx[:, oo.ignr:, 0:ook-1], oo.q2, oo.R, oo.Cs, oo.Cn, alpR, alpC, oo.TR, prior=oo.use_prior, accepts=8, aro=oo.ARord, sig_ph0L=oo.sig_ph0L, sig_ph0H=oo.sig_ph0H)
                        ARcfSmpl(ooN + 1 - oo.ignr,
                                 ook,
                                 oo.AR2lims,
                                 oo.smpx[:, 1 + oo.ignr:, 0:ook],
                                 oo.smpx[:, oo.ignr:, 0:ook - 1],
                                 oo.q2,
                                 oo.R,
                                 oo.Cs,
                                 oo.Cn,
                                 alpR,
                                 alpC,
                                 oo.TR,
                                 prior=oo.use_prior,
                                 accepts=8,
                                 aro=oo.ARord,
                                 sig_ph0L=oo.sig_ph0L,
                                 sig_ph0H=oo.sig_ph0H)
                        oo.F_alfa_rep = alpR + alpC  #  new constructed
                        prt, rank, f, amp = ampAngRep(oo.F_alfa_rep,
                                                      f_order=True)
                        #print prt
                    #ut, wt = FilteredTimeseries(ooN+1, ook, oo.smpx[:, 1:, 0:ook], oo.smpx[:, :, 0:ook-1], oo.q2, oo.R, oo.Cs, oo.Cn, alpR, alpC, oo.TR)
                    #ranks[it]    = rank
                    oo.allalfas[it] = oo.F_alfa_rep

                    for m in range(ooTR):
                        #oo.wts[m, it, :, :]   = wt[m, :, :, 0]
                        #oo.uts[m, it, :, :]   = ut[m, :, :, 0]
                        if not oo.bFixF:
                            oo.amps[it, :] = amp
                            oo.fs[it, :] = f

                    oo.F0 = (-1 *
                             _Npp.polyfromroots(oo.F_alfa_rep)[::-1].real)[1:]
                    for tr in range(oo.TR):
                        oo.Fs[tr, 0] = oo.F0[:]

                    #  sample u     WE USED TO Do this after smpx
                    #  u(it+1)    using ws(it+1), F0(it), smpx(it+1), ws(it+1)

                    oo.a2 = oo.a_q2 + 0.5 * (ooTR * ooN + 2)  #  N + 1 - 1
                    #oo.a2 = 0.5*(ooTR*(ooN-oo.ignr) + 2)  #  N + 1 - 1
                    BB2 = oo.B_q2
                    #BB2 = 0
                    for m in range(ooTR):
                        #   set x00
                        oo.x00[m] = oo.smpx[m, 2] * 0.1

                        #####################    sample q2
                        rsd_stp = oo.smpx[m, 3 + oo.ignr:, 0] - _N.dot(
                            oo.smpx[m, 2 + oo.ignr:-1], oo.F0).T
                        #oo.rsds[it, m] = _N.dot(rsd_stp, rsd_stp.T)
                        BB2 += 0.5 * _N.dot(rsd_stp, rsd_stp.T)

                    oo.q2[:] = _ss.invgamma.rvs(oo.a2, scale=BB2)
                    oo.smp_q2[:, it] = oo.q2

                #ttt8 = _tm.time()

    #             print("--------------------------------")
    #             print ("t2-t1  %.4f" % (#ttt2-#ttt1))
    #             print ("t3-t2  %.4f" % (#ttt3-#ttt2))
    #             print ("t4-t3  %.4f" % (#ttt4-#ttt3))
    # #            print ("t4b-t4a  %.4f" % (t4b-t4a))
    # #            print ("t4c-t4b  %.4f" % (t4c-t4b))
    # #            print ("t4-t4c  %.4f" % (t4-t4c))
    #             print ("t5-t4  %.4f" % (#ttt5-#ttt4))
    #             print ("t6-t5  %.4f" % (#ttt6-#ttt5))
    #             print ("t7-t6  %.4f" % (#ttt7-#ttt6))
    #             print ("t8-t7  %.4f" % (#ttt8-#ttt7))
    #tttB = _tm.time()
    #print("#tttB - #tttA  %.4f" % (#tttB - #tttA))

            oo.last_iter = it
            if it > oo.minITERS:
                smps = _N.empty((3, it + 1))
                smps[0, :it + 1] = oo.amps[:it + 1, 0]
                smps[1, :it + 1] = oo.fs[:it + 1, 0]
                smps[2, :it + 1] = oo.mnStds[:it + 1]

                #frms = _mg.stationary_from_Z_bckwd(smps, blksz=oo.peek)
                if _mg.stationary_test(oo.amps[:it + 1, 0],
                                       oo.fs[:it + 1, 0],
                                       oo.mnStds[:it + 1],
                                       it + 1,
                                       blocksize=oo.mg_blocksize,
                                       points=oo.mg_points):
                    break
                """
                fig = _plt.figure(figsize=(8, 8))
                fig.add_subplot(3, 1, 1)
                _plt.plot(range(1, it), oo.amps[1:it, 0], color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.amps[0:it, 0], color="black", lw=3)
                _plt.ylabel("amp")
                fig.add_subplot(3, 1, 2)
                _plt.plot(range(1, it), oo.fs[1:it, 0]/(2*oo.dt), color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.fs[0:it, 0]/(2*oo.dt), color="black", lw=3)
                _plt.ylabel("f")
                fig.add_subplot(3, 1, 3)
                _plt.plot(range(1, it), oo.mnStds[1:it], color="grey", lw=1.5)
                _plt.plot(range(0, it), oo.mnStds[0:it], color="black", lw=3)
                _plt.ylabel("amp")
                _plt.xlabel("iter")
                _plt.savefig("%(dir)stmp-fsamps%(it)d" % {"dir" : oo.mcmcRunDir, "it" : it+1})
                fig.subplots_adjust(left=0.15, bottom=0.15, right=0.95, top=0.95)
                _plt.close()
                """
                #if it - frms > oo.stationaryDuration:
                #   break

        oo.dump_smps(0, toiter=(it + 1), dir=oo.mcmcRunDir)
        oo.VIS = ARo  #  to examine this from outside
예제 #36
0
def run_n_dimension(args, radius, eigvals):
    num_points = args.num_points
    eps = args.eps
    power = args.power
    real = args.real
    by_coeffs = args.coeffs
    dim = args.dimension

    root_pts = {}
    residuals = {}
    powerpolys = []
    chebpolys = []
    if by_coeffs:
        for i in range(dim):
            from numalgsolve.polynomial import getPoly
            powerpolys.append(getPoly(num_points, dim, power=True))
            chebpolys.append(getPoly(num_points, dim, power=False))
    else:
        r = np.random.random((num_points, dim)) * radius + eps
        roots = 2 * r - radius

        root_pts = {'roots': np.array(list(product(*np.rot90(roots))))}

        for i in range(dim):
            coeffs = np.zeros((num_points + 1, ) * dim)
            idx = [
                slice(None),
            ] * dim
            idx[i] = 0
            coeffs[tuple(idx)] = polyfromroots(roots[:, i])
            lt = [0] * dim
            lt[i] = num_points
            powerpolys.append(
                MultiPower(coeffs))  #, lead_term=lt, clean_zeros=False))

            coeffs[tuple(idx)] = chebfromroots(roots[:, i])
            chebpolys.append(
                MultiCheb(coeffs))  #, lead_term=lt, clean_zeros=False))
            # plt.subplot(121);plt.imshow(coeffs);plt.subplot(122);plt.imshow(chebpolys[0].coeff);plt.show()

    for solver in all_solvers:
        if not isinstance(solver, OneDSolver) and solver.basis in [
                'power', 'both'
        ]:
            # if ((not eigvals) or solver.eigvals):
            name = str(solver) + ' Power'
            root_pts[name] = solver(powerpolys)
            residuals[name] = maximal_residual(powerpolys, root_pts[name])

    for solver in all_solvers:
        if not isinstance(solver, OneDSolver) and solver.basis in [
                'cheb', 'both'
        ]:
            # if ((not eigvals) or solver.eigvals):
            name = str(solver) + ' Cheb'
            root_pts[name] = solver(chebpolys)
            residuals[name] = maximal_residual(chebpolys, root_pts[name])

    if args.hist:
        evaluations = {}
        for k, v in root_pts.items():
            if k == 'roots': continue
            # evaluations[k] = []
            polys = powerpolys if 'Power' in k else chebpolys
            # for poly in polys:
            evaluations[k] = sum(np.abs(poly(root_pts[k])) for poly in polys)
        ncols = len(evaluations)
        # plt.figure(figsize=(12,6))
        fig, ax = plt.subplots(1, ncols, sharey=True, figsize=(12, 4))
        minimal = -15
        maximal = 1
        for i, (k, v) in enumerate(evaluations.items()):
            ax[i].hist(np.clip(np.log10(v), minimal, maximal),
                       range=(minimal, maximal),
                       bins=40)
            ax[i].set_xlabel(r'$log_{10}(p(r_i))$')
            ax[i].set_title(k)
        plt.suptitle("Eigenvalues" if eigvals else "Eigenvectors")
        plt.show()

    return root_pts, residuals
예제 #37
0
파일: carma.py 프로젝트: dfm/carma
def get_alpha_and_beta(arroots, maroots):
    alpha = polyfromroots(arroots)
    beta = polyfromroots(maroots)
    beta /= beta[0]
    return alpha, beta
예제 #38
0
파일: plotPSD.py 프로젝트: kobyafrank/kali
from matplotlib.colors import LinearSegmentedColormap
import numpy.polynomial.polynomial as nppoly
import pdb


numFreqs = 1000
T_incr = 0.02
T = 4000

colormapping = {'0': '#a6cee3', '2': '#b2df8a', '4': '#fb9a99', '6': '#fdbf6f', '8': '#cab2d6', '10': '#ffff99'}

freqs = np.logspace(m.log10(1./T),m.log10(1./T_incr),numFreqs)

#aListRoots = [-0.73642081+0j, -0.01357919+0j, -0.025-0.75j, -0.025+0.75j]
aListRoots = [-0.73642081+0j, -0.01357919+0j]
aPoly = nppoly.polyfromroots(aListRoots)
aPoly = aPoly.tolist()
aPoly.reverse()
aPoly.pop(0)
aPoly = [coeff.real for coeff in aPoly]

print "aPoly"
print aPoly

#aList = [0.75,0.01]
aList = aPoly

bList = [1.16e-9,6.8e-9]

if (CF.checkParams(aList,bList) == 1):
	maxDenomOrder = 2*len(aList)
예제 #39
0
def nonlinearphase_channel(x, taps = 5, debug = False):
  '''
  *SOME TESTING, BUGGY*

  Implements an all pass filter (flat frequency response).  The plan was to
  generate an all pass filter with a very ugly phase response by generating
  random taps (while maintaining flat frequency response).  However, this does
  not actually do what I hoped it would.  I expected a terribly ugly
  phase response.  However, the phase response of this random IIR filter turns
  out to actually be pretty linear...

  The frequency response seems to also SOMTIMES output some crazy behaviour as 
  the frequency gets close to pi.  Would be wise to use with debug = True.
  '''
  bar = Bar('Passing through nonlinearphase channel...', max = taps + 1)
  bar.next()

  rnge = (0.2, 0.80)
  P = [] #Poles
  if taps % 2 == 0:
    pass
  else:
    taps = taps - 1
    p = random.uniform(*rnge)
    p = random.choice([-1,1])*p
    P.append(p)
  for i in range(taps): 
    p_real = random.uniform(*rnge)
    p_real = random.choice([-1, 1])*p_real
    p_imag = random.uniform (0.2, 0.80*sqrt(1 - p_real**2))
    p_imag = random.choice([-1, 1])*p_imag

    p1 = p_real + (1j)*p_imag #1j is an imaginary number
    p2 = p_real + (1j)*(-1)*p_imag
    P.append(p1)
    P.append(p2)
    bar.next()
    
  a = poly.polyfromroots(P) #a is the 'a' coefficients of the IIR filter

  b = np.conj(a) #b must be the reversed & conjugated coefficients of a
  b = np.array(list(reversed(b)))

  #The polynomial library stores it's coefficients in the opposite order
  #of scipy.signal.  So i reverse both here again.
  #Reversing a twice brings it back to it's original form, but I am hopefully
  #avoiding some confusion because it is being done for 2 different reasons.
  b = np.array(list(reversed(b)))
  a = np.array(list(reversed(a)))

  if debug == True:
    plot_filter(b,a)
    plot_filter_pz(b,a)
  
  x = lfilter(b, a, x)
  max_ampl = max(max(x), -1*min(x))
  x = x/max_ampl

  bar.next()
  bar.finish()
  return x
예제 #40
0
import matplotlib.pyplot as plt
import random
from collections import namedtuple
import numpy as np
import numpy.polynomial.polynomial as polynomial
import tensorflow as tf

random.seed(234921)


# make a polynomial with the given number of roots, but no root at 0
poly_degree = 7
poly_roots = list(np.arange(poly_degree + 1) - int(poly_degree / 2))
poly_roots.remove(0)
poly_roots = np.array(poly_roots)
poly_coeffs = polynomial.polyfromroots(poly_roots)

deltax = 0.1
x_all = np.arange(poly_roots[0] - 0.2, poly_roots[-1] + 0.2, deltax)
y_all = polynomial.polyval(x_all, poly_coeffs)

number_points_to_train = 20
number_points_to_test = 20
inds = range(len(x_all))
random.shuffle(inds)
x_train = x_all[inds[0:number_points_to_train]]
y_train = y_all[inds[0:number_points_to_train]]
x_test = x_all[inds[number_points_to_train : (number_points_to_train + number_points_to_test)]]
y_test = y_all[inds[number_points_to_train : (number_points_to_train + number_points_to_test)]]