コード例 #1
0
ファイル: mcmcARcontinuous.py プロジェクト: AraiKensuke/LOST
    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
コード例 #2
0
ファイル: mcmcARspk.py プロジェクト: AraiKensuke/LOST
    def setParams(self, psth_run=False, psth_knts=10):
        oo = self
        # #generate initial values of parameters
        #oo._d = _kfardat.KFARGauObsDat(oo.TR, oo.N, oo.k)
        #oo._d.copyData(oo.y)

        oo.Ns = _N.ones(oo.TR, dtype=_N.int) * oo.N
        oo.ks = _N.ones(oo.TR, dtype=_N.int) * oo.k

        oo.F = _N.zeros((oo.k, oo.k))
        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)
        oo.F[0] = _N.random.randn(oo.k) / _N.arange(1, oo.k + 1)**2
        oo.F[0, 0] = 0.8
        oo.Fs = _N.zeros((oo.TR, oo.k, oo.k))
        for tr in range(oo.TR):
            oo.Fs[tr] = oo.F
        oo.Ik = _N.identity(oo.k)
        oo.IkN = _N.tile(oo.Ik, (oo.N + 1, 1, 1))

        #  need TR
        #  pr_x[:, 0]  empty, not used
        #oo.p_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.p_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.p_x[:, 0, 0] = 0
        oo.p_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        oo.p_Vi = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        #oo.f_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.f_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.f_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))
        #oo.s_x   = _N.empty((oo.TR, oo.N+1, oo.k, 1))
        oo.s_x = _N.empty((oo.TR, oo.N + 1, oo.k))
        oo.s_V = _N.empty((oo.TR, oo.N + 1, oo.k, oo.k))

        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)
        oo.G = _N.zeros((oo.k, 1))
        oo.G[0, 0] = 1
        oo.Q = _N.empty(oo.TR)

        #  baseFN_inter   baseFN_comps   baseFN_comps

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

        oo.smpx = _N.zeros((oo.TR, (oo.N + 1) + 2, oo.k))  #  start at 0 + u
        oo.ws = _N.empty((oo.TR, oo.N + 1), dtype=_N.float)

        #############   ADDED THIS FOR DEBUG
        #oo.F_alfa_rep = _N.array([-0.4       +0.j,          0.96999828+0.00182841j,  0.96999828-0.00182841j, 0.51000064+0.02405102j,  0.51000064-0.02405102j,  0.64524011+0.04059507j, 0.64524011-0.04059507j]).tolist()
        if oo.F_alfa_rep is None:
            oo.F_alfa_rep = initF(oo.R, oo.Cs + oo.Cn,
                                  0).tolist()  #  init F_alfa_rep
        print("F_alfa_rep*********************")
        print(oo.F_alfa_rep)

        #print(ampAngRep(oo.F_alfa_rep))
        if oo.q20 is None:
            oo.q20 = 0.00077
        oo.q2 = _N.ones(oo.TR) * oo.q20

        oo.F0 = (-1 * _Npp.polyfromroots(oo.F_alfa_rep)[::-1].real)[1:]
        oo.Fs = _N.zeros((oo.TR, oo.k, oo.k))

        oo.F[0] = oo.F0
        _N.fill_diagonal(oo.F[1:, 0:oo.k - 1], 1)

        for tr in range(oo.TR):
            oo.Fs[tr] = oo.F

        ########  Limit the amplitude to something reasonable
        xE, nul = createDataAR(oo.N, oo.F0, oo.q20, 0.1)
        mlt = _N.std(xE) / 0.5  #  we want amplitude around 0.5
        oo.q2 /= mlt * mlt
        xE, nul = createDataAR(oo.N, oo.F0, oo.q2[0], 0.1)

        w = 5
        wf = gauKer(w)
        gk = _N.empty((oo.TR, oo.N + 1))
        fgk = _N.empty((oo.TR, oo.N + 1))
        for m in range(oo.TR):
            gk[m] = _N.convolve(oo.y[m], wf, mode="same")
            gk[m] = gk[m] - _N.mean(gk[m])
            gk[m] /= 5 * _N.std(gk[m])
            fgk[m] = bpFilt(15, 100, 1, 135, 500, gk[m])  #  we want
            fgk[m, :] /= 3 * _N.std(fgk[m, :])

            if oo.noAR:
                oo.smpx[m, 2:, 0] = 0
            else:
                oo.smpx[m, 2:, 0] = fgk[m, :]

            for n in range(2 + oo.k - 1, oo.N + 1 + 2):  # CREATE square smpx
                oo.smpx[m, n, 1:] = oo.smpx[m, n - oo.k + 1:n, 0][::-1]
            for n in range(2 + oo.k - 2, -1, -1):  # CREATE square smpx
                oo.smpx[m, n, 0:oo.k - 1] = oo.smpx[m, n + 1, 1:oo.k]
                oo.smpx[m, n, oo.k - 1] = _N.dot(oo.F0,
                                                 oo.smpx[m, n:n + oo.k,
                                                         oo.k - 2])  # no noise

        if oo.bpsth:
            psthKnts, apsth, aWeights = _spknts.suggestPSTHKnots(
                oo.dt,
                oo.TR,
                oo.N + 1,
                oo.y.T,
                psth_knts=psth_knts,
                psth_run=psth_run)

            _N.savetxt("apsth.txt", apsth, fmt="%.4f")
            _N.savetxt("psthKnts.txt", psthKnts, fmt="%.4f")

            apprx_ps = _N.array(_N.abs(aWeights))
            oo.u_a = -_N.log(1 / apprx_ps - 1)

            #  For oo.u_a, use the values we get from aWeights

            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0) * oo.dt,
                                        (oo.t1 - oo.t0)),
                            knots=(psthKnts * oo.dt),
                            include_intercept=True)  #  spline basis

            oo.B = oo.B.T  #  My convention for beta
            oo.aS = _N.array(oo.u_a)
            # fig = _plt.figure(figsize=(4, 7))
            # fig.add_subplot(2, 1, 1)
            # _plt.plot(apsth)
            # fig.add_subplot(2, 1, 2)
            # _plt.plot(_N.dot(oo.B.T, aWeights))
        else:
            oo.B = patsy.bs(_N.linspace(0, (oo.t1 - oo.t0) * oo.dt,
                                        (oo.t1 - oo.t0)),
                            df=4,
                            include_intercept=True)  #  spline basis

            oo.B = oo.B.T  #  My convention for beta
            oo.aS = _N.zeros(4)
コード例 #3
0
sts01 = _N.zeros(N)
while t < N:
    t += int(T_smp * (1 + 0.15 * _N.random.randn()))
    ups = _N.where(_N.random.rand(6) < 0.5)[0]

    for i in range(len(ups)):
        if t + ups[i] < N:
            sts01[t + ups[i]] = 1

stoch_T = 20
nz_ts = _N.where(_N.random.rand(N) < 1 / stoch_T)[0]
sts01[nz_ts] = 1

#_plt.xcorr(ts - _N.mean(ts), cont - _N.mean(cont), maxlags=100)

roots = initF(0, 1, 0, ifs=[(_N.pi / (Fs * 0.5)) * f_cont], ir=0.95)

alfa = []

for irt in range(len(roots) // 2):
    alfa.append([roots[irt * 2], roots[irt * 2 + 1]])

nRhythms = len(alfa)
ARcoeff = _N.empty((nRhythms, 2))

lat_obs = _N.empty(N + 1)
for n in range(nRhythms):
    # [0.1, 0.1] (x - 0.1) * (x - 0.1)
    ARcoeff[n] = (-1 * _Npp.polyfromroots(alfa[n])[::-1][1:]).real

    xy = createDataAR(N, ARcoeff[0], 0.001, N + 1, trim=0)
コード例 #4
0
ファイル: mcmcARcontinuous2.py プロジェクト: AraiKensuke/LOST
#     #fig.add_subplot(2, 2, 1)
#     _plt.psd(obsvd[0], Fs=Fs/2)

fSigMax = Fs / (2 * skip)  #  fixed parameters
#fSigMax       = 150.    #  fixed parameters
#freq_lims     = [[1 / .85, fSigMax]]
freq_lims = [[0.000001, fSigMax]] * Cs

sig_ph0L = -1
sig_ph0H = -(0.05 * 0.05)  #95*0.95)   #
#sig_ph0H      = -(0.9*0.9)   #

radians = buildLims(Cn, freq_lims, nzLimL=0.1)
AR2lims = 2 * _N.cos(radians)

F_alfa_rep = initF(R, Cs, Cn).tolist()  #  init F_alfa_rep

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

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

#  q2  --  Inverse Gamma prior
#a_q2         = 0.5;          B_q2         = 1e-2