def test():
    ## Provided the weights for the stationary distribution and the exchangeable coefficients
    ## we use them to generate the true rate matrix and the sequences to get the averaged
    ## sufficient statistics for the sequences throughout a larger number of replications
    ## Based on the sufficient statistics and we fix the exchangeable coefficients to
    ## its true values, we use HMC to estimate the weights for the stationary distribution
    ## The rate matrix is an un-normalized version
    
    ## The correctness of HMC and ExpectedCompleteReversibleObjective has been tested
    ## The estimated stationary distribution 'stationaryDistEst' is very close to stationaryDist

    nStates = 4
    nRep = 1000
    seedNum = np.arange(0, nRep)
    np.random.seed(123)
    weights = np.random.uniform(0, 1, nStates)
    print(weights)
    exchangeCoef = np.array((1, 2, 3, 4, 5, 6))

    ## get the rate matrix
    testRateMtx = ReversibleRateMtxPiAndExchangeGTR(nStates, weights, exchangeCoef)
    stationaryDist = testRateMtx.getStationaryDist()
    rateMtx = testRateMtx.getRateMtx()
    bt = 5.0
    nSeq = 100

    nInit = np.zeros(nStates)
    holdTimes = np.zeros(nStates)
    nTrans = np.zeros((nStates, nStates))

    for j in range(nRep):
        ## do forward sampling
        seqList = generateFullPathUsingRateMtxAndStationaryDist(nSeq, nStates, seedNum[j], rateMtx, stationaryDist, bt)
        ## summarize the sufficient statistics
        ## extract first state from sequences
        firstStates = getFirstAndLastStateOfListOfSeq(seqList)['firstLastState'][:, 0]
        unique, counts = np.unique(firstStates, return_counts=True)
        nInitCount = np.asarray((unique, counts)).T
        nInit = nInit + nInitCount[:, 1]

        for i in range(nSeq):
            sequences = seqList[i]
            holdTimes = holdTimes + sequences['sojourn']
            nTrans = nTrans + sequences['transitCount']

    avgNTrans = nTrans / nRep
    avgHoldTimes = holdTimes / nRep
    avgNInit = nInit / nRep

    expectedCompleteReversibleObjective = ExpectedCompleteReversibleObjective(holdTimes=avgHoldTimes, nInit=avgNInit, nTrans=avgNTrans, kappa=1, exchangeCoef=exchangeCoef)
    prng = np.random.RandomState(1)
    hmc = HMC(40, 0.02, expectedCompleteReversibleObjective, expectedCompleteReversibleObjective)
    sample = prng.uniform(0, 1, nStates)
    samples = hmc.run(0, 5000, sample)
    avgWeights = np.sum(samples, axis=0) / samples.shape[0]
    stationaryDistEst = np.exp(avgWeights)/np.sum(np.exp(avgWeights))
    print(weights)
    print(avgWeights)
    print(stationaryDist)
    print(stationaryDistEst)
def testHMCForStationaryAndBivariateWeights(data, nMCMCIter = 500, nLeapFrogSteps=40, stepSize=0.002, nIterPerPath=1000):
    avgNTrans = data['transitCount']
    avgHoldTimes = data['sojourn']
    avgNInit = data['nInit']
    bivariateDictionary = data['bivariateDictionary']

    ## run HMC to estimate the stationary distribution                                          nBivariateFeatWeightsDictionary=bivariateDictionary)
    nStates = len(data['stationaryWeights'])
    expectedCompleteReversibleObjective = ExpectedCompleteReversibleObjective(holdTimes=avgHoldTimes, nInit=avgNInit,
                                                                              nTrans=avgNTrans, kappa=1,
                                                                              nBivariateFeatWeightsDictionary=bivariateDictionary)

    hmc = HMC(nLeapFrogSteps, stepSize, expectedCompleteReversibleObjective, expectedCompleteReversibleObjective)
    sample = np.random.uniform(0, 1, int(nStates + nStates * (nStates - 1) / 2))
    lastSample = sample
    exchangeableParamSamples = np.zeros((nMCMCIter, int(nStates * (nStates - 1) / 2)))
    binaryWeightsSamples = np.zeros((nMCMCIter, int(nStates * (nStates - 1) / 2)))
    stationaryDistEstSamples = np.zeros((nMCMCIter, nStates))
    for i in range(nMCMCIter):

        for k in range(nIterPerPath):
            hmcResult = hmc.doIter(nLeapFrogSteps, stepSize, lastSample, expectedCompleteReversibleObjective,
                               expectedCompleteReversibleObjective, True)
            lastSample = hmcResult.next_q

        sample = lastSample
        initialStationaryWeights = sample[nStates]
        initialBinaryWeights = sample[nStates:int(nStates+(nStates * (nStates - 1) / 2))]


        newRateMtx = ReversibleRateMtxPiAndBinaryWeightsWithGraphicalStructure(nStates, initialStationaryWeights,
                                                                           initialBinaryWeights,
                                                                           bivariateFeatIndexDictionary=bivariateDictionary)
        exchangeableParam = newRateMtx.getExchangeCoef()
        stationaryDistEst = newRateMtx.getStationaryDist()
        exchangeableParamSamples[i, :] = exchangeableParam
        stationaryDistEstSamples[i, :] = stationaryDistEst
        binaryWeightsSamples[i, :] = initialBinaryWeights
        print(exchangeableParam)
        print(i)
    result = {}
    result['exchangeableCoef'] = exchangeableParamSamples
    result['stationaryDist'] = stationaryDistEstSamples
    result['binaryWeights'] = binaryWeightsSamples
    return result
Exemplo n.º 3
0
    def run(self, S, N, T, model):

        NT = N * T
        #data
        y = self.y
        x = self.x
        pi = self.pi
        w = self.w

        #prior
        self._prior_set()
        #sigma_beta_sqr = self.sigma_beta_sqr
        #sigma_xi_sqr = self.sigma_xi_sqr
        #sigma_z_sqr = self.sigma_z_sqr
        #sigma_gamma_sqr = self.sigma_gamma_sqr
        #sigma_delta_sqr = self.sigma_sqr
        #initialize
        beta, delta, xi, gamma, z, u, eta, alpha, sigma_alpha_sqr, sigma_v_sqr = Initialize(
        ).generate(model, x, pi, w)

        #all_MCMC
        all_beta = np.zeros([S, x.shape[1]])
        all_xi = np.zeros([S, pi.shape[1]])
        all_delta = np.zeros([S, pi.shape[1]])
        all_z = np.zeros([S, w.shape[1]])
        all_gamma = np.zeros([S, w.shape[1]])
        all_sigma_v_sqr = np.zeros([
            S,
        ])
        all_sigma_alpha_sqr = np.zeros([
            S,
        ])
        all_alpha = np.zeros([S, N])
        all_eta = np.zeros([S, N])
        all_u = np.zeros([S, NT])
        #adjust particles number
        H = self.H
        H = H - 1
        gpu = self.gpu

        for i in range(S):
            print(i)
            ### Posterior
            # beta
            V_beta = inv((np.dot(x.T, x) * self.sigma_beta_sqr + sigma_v_sqr) /
                         (sigma_v_sqr * self.sigma_beta_sqr))
            mu_beta = np.dot(
                V_beta,
                np.dot(
                    x.T, y - u - np.kron(alpha, np.ones([
                        T,
                    ])) - np.kron(eta, np.ones(T, ))) / sigma_v_sqr)
            beta = multivariate_normal.rvs(mu_beta, V_beta)
            # sigma_v_sqr
            y_tilda = y - np.dot(x, beta) - u - np.kron(alpha, np.ones(
                T, )) - np.kron(eta, np.ones(T, ))
            shape = (NT + 1) / 2
            scale = 2 / (0.0001 + np.dot(y_tilda.T, y_tilda))
            sigma_v_sqr = 1 / np.random.gamma(shape, scale)
            # sigma_alpha_sqr
            shape = (N + 1) / 2
            scale = 2 / (0.0001 + np.dot(alpha.T, alpha))
            sigma_alpha_sqr = 1 / np.random.gamma(shape, scale)

            if gpu:
                eta, alpha, u = self.sampler(w, gamma, pi, xi, z, alpha, u, N,
                                             T, y, x, beta, sigma_v_sqr,
                                             sigma_alpha_sqr, eta, H, delta)
            else:
                eta, alpha, u = self.sampler2(w, gamma, pi, xi, z, alpha, u, N,
                                              T, y, x, beta, sigma_v_sqr,
                                              sigma_alpha_sqr, eta, H, delta)

            kwargs = {
                'delta': delta,
                'sigma_delta_sqr': self.sigma_delta_sqr,
                'pi': pi,
                'u': u,
                'xi': xi,
                'sigma_xi_sqr': self.sigma_xi_sqr,
                'z': z,
                'sigma_z_sqr': self.sigma_z_sqr,
                'w': w,
                'gamma': gamma,
                'eta': eta
            }

            if model == 'A':
                #sigma_eta
                shape = (N + 1) / 2
                scale = 2 / (0.0001 + np.dot(eta.T, eta))
                sigma_eta_sqr = 1 / np.random.gamma(shape, scale)
                #sigma_u
                shape = (NT + 1) / 2
                scale = 2 / (0.0001 + np.dot(u.T, u))
                sigma_u_sqr = 1 / np.random.gamma(shape, scale)
                #decompose for PMCMC
                gamma = np.array([np.log(sigma_eta_sqr), 0])
                xi = np.array([np.log(sigma_u_sqr), 0])
            elif model == 'B':
                delta, z = HMC().sampler(model, **kwargs)
            elif model == 'C':
                xi, gamma = HMC().sampler(model, **kwargs)
            elif model == 'D':
                delta, z, xi, gamma = HMC().sampler(model, **kwargs)

            print('beta')
            print(beta)
            print('delta')
            print(delta)
            print('xi')
            print(xi)
            print('z')
            print(z)
            print('gamma')
            print(gamma)
            print('sigma_alpha_sqr')
            print(sigma_alpha_sqr)
            print('sigma_v_sqr')
            print(sigma_v_sqr)
            print('eta_mean_std')
            print(eta.mean())
            print(eta.std())
            print('u_mean_std')
            print(u.mean())
            print(u.std())

            all_beta[i, :] = beta
            all_xi[i, :] = xi
            all_delta[i, :] = delta
            all_z[i, :] = z
            all_gamma[i, :] = gamma
            all_sigma_alpha_sqr[i] = sigma_alpha_sqr
            all_sigma_v_sqr[i] = sigma_v_sqr
            all_alpha[i, :] = alpha
            all_eta[i, :] = eta
            all_u[i, :] = u
        return all_beta, all_xi, all_delta, all_z, all_gamma, all_sigma_alpha_sqr, all_sigma_v_sqr, all_alpha, all_eta, all_u
Exemplo n.º 4
0
    cfset = ax.contourf(xx, yy, f, cmap=cmap)
    ax.set_yticks([])
    ax.set_xticks([])







if __name__ == '__main__':


    # sess = tf.Session() #For target distribution

    infmet = HMC(log_p_true) #Inference method


    fig = plt.figure(figsize=(12,5), facecolor='white')
    plt.ion()
    plt.show(block=False)


    ax = fig.add_subplot(141, frameon=False)
    target_distribution = lambda x: np.exp(infmet.sess.run(log_p_true(x, 2)))
    plot_isocontours(ax, target_distribution, cmap='Reds')
    plt.gca().set_aspect('equal', adjustable='box')
    

    # mean = [[0.,0.]]
    def succesiveConditionalSimulatorWithInitialWeightAndData(
            self, initialParamAndData, K, nStates, nBivariateFeat,
            bivariateFeatDictionary, seed, bt, nSeq, interLength, HMCPlusBPS,
            onlyHMC, nLeapFrogSteps, stepSize, nItersPerPathAuxVar, rfOptions,
            mcmcOptions):

        thetaStationaryWeights = initialParamAndData['stationaryWeights']
        thetaBinaryWeights = initialParamAndData['binaryWeights']

        theta0StationarySamples = np.zeros((K, nStates))
        theta0BinaryWeightsSamples = np.zeros((K, nBivariateFeat))

        ## for debuging reasons, we ave the stationary distribution and exchangeable coef
        ## actually, we only need the last sample at the last iteration K
        theta0StationaryDistSamples = np.zeros((K, nStates))
        theta0ExchangeableCoefSamples = np.zeros(
            (K, int(nStates * (nStates - 1) / 2)))

        theta0StationarySamples[0, :] = thetaStationaryWeights
        theta0BinaryWeightsSamples[0, :] = thetaBinaryWeights
        theta0StationaryDistSamples[
            0, :] = np.exp(thetaStationaryWeights) / np.sum(
                np.exp(thetaStationaryWeights))
        initialExchangeCoef = getExchangeCoef(nStates, thetaBinaryWeights,
                                              bivariateFeatDictionary)
        theta0ExchangeableCoefSamples[0, :] = initialExchangeCoef

        sample = None

        if onlyHMC:
            sample = np.zeros((nStates + nBivariateFeat))
            sample[0:nStates] = thetaStationaryWeights
            sample[nStates:(nStates + nBivariateFeat)] = thetaBinaryWeights

        if HMCPlusBPS:
            sample = thetaStationaryWeights

        suffStat = initialParamAndData['suffStat']
        nInit = suffStat['nInit']
        nTrans = suffStat['nTrans']
        holdTime = suffStat['sojourn']

        expectedCompleteReversibleObjective = None
        for i in np.arange(1, K, 1):

            if HMCPlusBPS:
                expectedCompleteReversibleObjective = ExpectedCompleteReversibleObjective(
                    holdTime, nInit, nTrans, 1.0, initialExchangeCoef)
            if onlyHMC:
                expectedCompleteReversibleObjective = ExpectedCompleteReversibleObjective(
                    holdTime,
                    nInit,
                    nTrans,
                    1.0,
                    nBivariateFeatWeightsDictionary=bivariateFeatDictionary)

            #####################################
            hmc = HMC(nLeapFrogSteps, stepSize,
                      expectedCompleteReversibleObjective,
                      expectedCompleteReversibleObjective)
            lastSample = sample

            for k in range(nItersPerPathAuxVar):
                hmcResult = hmc.doIter(nLeapFrogSteps, stepSize, lastSample,
                                       expectedCompleteReversibleObjective,
                                       expectedCompleteReversibleObjective,
                                       True)
                lastSample = hmcResult.next_q
            sample = lastSample

            if onlyHMC:
                thetaStationaryWeights = sample[0:nStates]
                thetaBinaryWeights = sample[nStates:(nStates + nBivariateFeat)]
                theta0StationarySamples[i, :] = thetaStationaryWeights
                theta0BinaryWeightsSamples[i, :] = thetaBinaryWeights

            if HMCPlusBPS:
                thetaStationaryWeights = sample
                theta0StationarySamples[i, :] = thetaStationaryWeights
                # update stationary distribution elements to the latest value
                thetaStationaryDist = np.exp(sample) / np.sum(np.exp(sample))
                # sample exchangeable coefficients using local bouncy particle sampler
                ## define the model
                model = ExpectedCompleteReversibleModelWithBinaryFactors(
                    expectedCompleteReversibleObjective, nStates,
                    thetaBinaryWeights, thetaStationaryDist,
                    bivariateFeatDictionary)
                ## define the sampler to use
                ## local sampler to use

                localSampler = LocalRFSamplerForBinaryWeights(
                    model, rfOptions, mcmcOptions, nStates,
                    bivariateFeatDictionary)
                phyloLocalRFMove = PhyloLocalRFMove(
                    model=model,
                    sampler=localSampler,
                    initialPoints=thetaBinaryWeights,
                    options=rfOptions,
                    prng=RandomState(i))
                thetaBinaryWeights = phyloLocalRFMove.execute()
                theta0BinaryWeightsSamples[i, :] = thetaBinaryWeights

            initialRateMtx = ReversibleRateMtxPiAndBinaryWeightsWithGraphicalStructure(
                nStates,
                thetaStationaryWeights,
                thetaBinaryWeights,
                bivariateFeatIndexDictionary=bivariateFeatDictionary)

            initialStationaryDist = initialRateMtx.getStationaryDist()
            initialExchangeCoef = initialRateMtx.getExchangeCoef()
            theta0StationaryDistSamples[i, :] = initialStationaryDist
            theta0ExchangeableCoefSamples[i, :] = initialExchangeCoef

            weightGenerationRegime = WeightGenerationRegime(
                nStates=nStates,
                nBivariateFeat=nBivariateFeat,
                stationaryWeights=thetaStationaryWeights,
                bivariateWeights=thetaBinaryWeights)

            prng = RandomState(np.random.choice(2**32 - 1, 1))

            dataRegime = DataGenerationRegime(
                nStates=nStates,
                bivariateFeatIndexDictionary=bivariateFeatDictionary,
                btLength=bt,
                nSeq=nSeq,
                weightGenerationRegime=weightGenerationRegime,
                prng=prng,
                interLength=interLength)
            ## generate the sequences data
            seqList = dataRegime.generatingSeq()
            suffStat = dataRegime.getSufficientStatFromSeq(seqList)
            firstLastStatesArrayAll = dataRegime.generatingSeqGivenRateMtxAndBtInterval(
                seqList)

            # replicate K iterations to get new parameters
            nTrans = suffStat["transitCount"]
            holdTime = suffStat["sojourn"]

            nInit = np.zeros(nStates)
            unique, counts = np.unique(firstLastStatesArrayAll[0][:, 0],
                                       return_counts=True)
            nInitCount = np.asarray((unique, counts)).T
            nInit[nInitCount[:, 0].astype(int)] = nInitCount[:, 1]

        result = {}
        result['stationaryDist'] = theta0StationaryDistSamples[(K - 1), :]
        result['exchangeCoef'] = theta0ExchangeableCoefSamples[(K - 1), :]
        result['stationaryWeights'] = thetaStationaryWeights
        result['binaryWeights'] = thetaBinaryWeights
        ## after testing the code is right, the following two lines should be removed
        #result['StationaryDistSamples'] = theta0StationaryDistSamples
        #result['ExchangeableCoefSamples'] = theta0ExchangeableCoefSamples
        return result
    nTrans = np.zeros((nStates, nStates))

    for j in range(nPairSeq):
        suffStat = endPointSamplerSummarizeStatisticsOneBt(
            True, prng, initialRateMatrix, firstLastStatesArrayAll[j], 1.0)
        nInit = nInit + suffStat['nInit']
        holdTime = holdTime + suffStat['holdTimes']
        nTrans = nTrans + suffStat['nTrans']

    # construct expected complete reversible model objective
    expectedCompleteReversibleObjective = ExpectedCompleteReversibleObjective(
        holdTime, nInit, nTrans, 1.0, initialExchangeCoef)

    # sample stationary distribution elements using HMC
    startTimehmc = datetime.now()
    hmc = HMC(40, 0.02, expectedCompleteReversibleObjective,
              expectedCompleteReversibleObjective)
    sample = np.random.uniform(0, 1, nStates)
    samples = hmc.run(0, 1000, sample)
    avgWeights = np.sum(samples, axis=0) / samples.shape[0]
    endTimehmc = datetime.now()
    timeElapsedhmc = endTimehmc - startTimehmc
    timeElapsedHMCArray[i] = (timeElapsedhmc).total_seconds()

    initialWeights = avgWeights
    stationaryDistEst = np.exp(avgWeights) / np.sum(np.exp(avgWeights))
    # update stationary distribution elements to the latest value
    initialStationaryDist = stationaryDistEst

    # sample exchangeable coefficients using local bouncy particle sampler
    ## define the model
    model = ExpectedCompleteReversibleModelWithBinaryFactors(
Exemplo n.º 7
0
    def run(self, S, N, T, model):
        NT = N * T
        #data
        y = self.y
        x = self.x
        pi = self.pi
        w = self.w

        #initalize
        all_beta, all_xi, all_delta, all_z, all_gamma, all_sigma_v_sqr, all_sigma_alpha_sqr, all_alpha, all_eta, all_u = self._initalize(
            S, N, T, model)
        beta = self.beta
        delta = self.delta
        xi = self.xi
        gamma = self.gamma
        z = self.z
        u = self.u
        eta = self.eta
        alpha = self.alpha
        sigma_alpha_sqr = self.sigma_alpha_sqr
        sigma_v_sqr = self.sigma_v_sqr

        #beginning
        for i in range(S):
            print(i)
            ### Posterior
            # beta
            V_beta = inv((np.dot(x.T, x) * self.sigma_beta_sqr + sigma_v_sqr) /
                         (sigma_v_sqr * self.sigma_beta_sqr))
            mu_beta = np.dot(
                V_beta,
                np.dot(
                    x.T, y - u - np.kron(alpha, np.ones([
                        T,
                    ])) - np.kron(eta, np.ones(T, ))) / sigma_v_sqr)
            beta = multivariate_normal.rvs(mu_beta, V_beta)
            # sigma_v_sqr
            y_tilda = y - np.dot(x, beta) - u - np.kron(alpha, np.ones(
                T, )) - np.kron(eta, np.ones(T, ))
            shape = (NT + 1) / 2
            scale = 2 / (0.0001 + np.dot(y_tilda.T, y_tilda))
            sigma_v_sqr = 1 / np.random.gamma(shape, scale)
            # sigma_alpha_sqr
            shape = (N + 1) / 2
            scale = 2 / (0.0001 + np.dot(alpha.T, alpha))
            sigma_alpha_sqr = 1 / np.random.gamma(shape, scale)
            #eta
            V_eta = 1 / (np.exp(-np.dot(w, gamma)) + T / sigma_v_sqr)
            mu_eta = V_eta * (
                (y - np.dot(x, beta) - u - np.kron(alpha, np.ones(
                    T, ))).reshape([N, T]).sum(axis=1) / sigma_v_sqr +
                np.exp(-np.dot(w, gamma)) * np.dot(w, z))
            myclip_a = 0
            my_mean = mu_eta
            my_std = V_eta**0.5
            a, b = (myclip_a - my_mean) / my_std, np.inf * np.ones([
                N,
            ])
            eta = truncnorm.rvs(a, b, loc=my_mean, scale=my_std)
            #u
            V_u = 1 / (np.exp(-np.dot(pi, xi)) + 1 / sigma_v_sqr)
            mu_u = V_u * ((y - np.dot(x, beta) - np.kron(eta, np.ones(T, )) -
                           np.kron(alpha, np.ones(T, ))) / sigma_v_sqr +
                          np.exp(-np.dot(pi, xi)) * np.dot(pi, delta))
            myclip_a = 0
            my_mean = mu_u
            my_std = V_u**0.5
            a, b = (myclip_a - my_mean) / my_std, np.inf * np.ones([
                NT,
            ])
            u = truncnorm.rvs(a, b, loc=my_mean, scale=my_std)
            #alpha
            scale = sigma_alpha_sqr * sigma_v_sqr / (T * sigma_alpha_sqr +
                                                     sigma_v_sqr)
            y_bar = y - np.dot(x, beta) - np.kron(eta, np.ones(T, )) - u
            loc = scale / sigma_v_sqr * y_bar.reshape([N, T]).sum(axis=1)
            alpha = norm.rvs(loc=loc, scale=scale)
            #determinants
            kwargs = {
                'delta': delta,
                'sigma_delta_sqr': self.sigma_delta_sqr,
                'pi': pi,
                'u': u,
                'xi': xi,
                'sigma_xi_sqr': self.sigma_xi_sqr,
                'z': z,
                'sigma_z_sqr': self.sigma_z_sqr,
                'w': w,
                'gamma': gamma,
                'eta': eta
            }
            if model == 'A':
                #sigma_eta
                shape = (N + 1) / 2
                scale = 2 / (0.0001 + np.dot(eta.T, eta))
                sigma_eta_sqr = 1 / np.random.gamma(shape, scale)
                #sigma_u
                shape = (NT + 1) / 2
                scale = 2 / (0.0001 + np.dot(u.T, u))
                sigma_u_sqr = 1 / np.random.gamma(shape, scale)
                #decompose for PMCMC
                gamma = np.array([np.log(sigma_eta_sqr), 0])
                xi = np.array([np.log(sigma_u_sqr), 0])
            elif model == 'B':
                delta, z = HMC().sampler(model, **kwargs)
            elif model == 'C':
                xi, gamma = HMC().sampler(model, **kwargs)
            elif model == 'D':
                delta, z, xi, gamma = HMC().sampler(model, **kwargs)

            print('beta')
            print(beta)
            print('delta')
            print(delta)
            print('xi')
            print(xi)
            print('z')
            print(z)
            print('gamma')
            print(gamma)
            print('sigma_alpha_sqr')
            print(sigma_alpha_sqr)
            print('sigma_v_sqr')
            print(sigma_v_sqr)
            print('eta_mean_std')
            print(eta.mean())
            print(eta.std())
            print('u_mean_std')
            print(u.mean())
            print(u.std())
            all_beta[i, :] = beta
            all_xi[i, :] = xi
            all_delta[i, :] = delta
            all_z[i, :] = z
            all_gamma[i, :] = gamma
            all_sigma_alpha_sqr[i] = sigma_alpha_sqr
            all_sigma_v_sqr[i] = sigma_v_sqr
            all_alpha[i, :] = alpha
            all_eta[i, :] = eta
            all_u[i, :] = u
        return all_beta, all_xi, all_delta, all_z, all_gamma, all_sigma_alpha_sqr, all_sigma_v_sqr, all_alpha, all_eta, all_u
Exemplo n.º 8
0
    def run(self, S, N, T, model):
        NT = N * T
        #data
        y = self.y
        x = self.x
        pi = self.pi
        w = self.w

        #initalize
        all_beta, all_xi, all_delta, all_z, all_gamma, all_sigma_v_sqr, all_sigma_alpha_sqr, all_alpha, all_eta, all_u = self._initalize(
            S, N, T, model)
        beta = self.beta
        delta = self.delta
        xi = self.xi
        gamma = self.gamma
        z = self.z
        u = self.u
        eta = self.eta
        alpha = self.alpha
        sigma_alpha_sqr = self.sigma_alpha_sqr
        sigma_v_sqr = self.sigma_v_sqr

        #beginning
        for i in range(S):
            print(i)
            ### Posterior
            # delta_
            delta_ = alpha + eta
            #delta_
            mu_eta = np.dot(w, z)
            sigma_eta_sqr = np.exp(np.dot(w, gamma))
            sigma_ = (sigma_alpha_sqr + sigma_eta_sqr)**0.5
            ratio = (sigma_eta_sqr / sigma_alpha_sqr)**0.5
            args = (mu_eta, sigma_eta_sqr, sigma_alpha_sqr, ratio, sigma_, y,
                    x, beta, u, N, T, sigma_v_sqr)
            mu_delta_, H_ = self._newton(delta_, args)

            delta__star = np.random.normal(mu_delta_, (-1 / H_)**0.5)
            A = (self._F(delta__star, *args) / self._F(mu_delta_, *args)
                 ) * np.exp(-0.5 * (delta__star - mu_delta_)**2 / (-1 / H_))
            U = np.random.uniform(0, 1, [
                N,
            ])
            select = U < A
            if select.any():
                delta_[select] = delta__star[select]  #change value if over u
            # beta
            V_beta = inv((np.dot(x.T, x) * self.sigma_beta_sqr + sigma_v_sqr) /
                         (sigma_v_sqr * self.sigma_beta_sqr))
            mu_beta = np.dot(
                V_beta,
                np.dot(x.T, y - u - np.kron(delta_, np.ones([
                    T,
                ]))) / sigma_v_sqr)
            beta = multivariate_normal.rvs(mu_beta, V_beta)
            # sigma_v_sqr
            y_tilda = y - np.dot(x, beta) - u - np.kron(delta_, np.ones(T, ))
            shape = (NT + 1) / 2
            scale = 2 / (0.0001 + np.dot(y_tilda.T, y_tilda))
            sigma_v_sqr = 1 / np.random.gamma(shape, scale)
            #eta
            A = inv(
                np.ones([T, T]) * sigma_alpha_sqr +
                np.diag(np.ones([T])) * sigma_v_sqr)
            Sigma_eta = 1 / (A.sum() + 1 / sigma_eta_sqr)
            eta_tilda = y - np.dot(x, beta) - u
            Mu_eta = Sigma_eta * (np.dot(eta_tilda.reshape(
                [N, T]), A).sum(axis=1) + mu_eta / sigma_eta_sqr)
            myclip_a = 0
            my_mean = Mu_eta
            my_std = Sigma_eta**0.5
            a, b = (myclip_a - my_mean) / my_std, np.inf * np.ones([
                N,
            ])
            eta = truncnorm.rvs(a, b, loc=my_mean, scale=my_std)
            # alpha
            alpha = delta_ - eta
            # sigma_alpha_sqr
            shape = (N + 1) / 2
            scale = 2 / (0.0001 + np.dot(alpha.T, alpha))
            sigma_alpha_sqr = 1 / np.random.gamma(shape, scale)
            # u
            V_u = 1 / (np.exp(-np.dot(pi, xi)) + 1 / sigma_v_sqr)
            mu_u = V_u * (
                (y - np.dot(x, beta) - np.kron(delta_, np.ones(T, ))) /
                sigma_v_sqr + np.exp(-np.dot(pi, xi)) * np.dot(pi, delta))
            myclip_a = 0
            my_mean = mu_u
            my_std = V_u**0.5
            a, b = (myclip_a - my_mean) / my_std, np.inf * np.ones([
                NT,
            ])
            u = truncnorm.rvs(a, b, loc=my_mean, scale=my_std)

            kwargs = {
                'delta': delta,
                'sigma_delta_sqr': self.sigma_delta_sqr,
                'pi': pi,
                'u': u,
                'xi': xi,
                'sigma_xi_sqr': self.sigma_xi_sqr,
                'z': z,
                'sigma_z_sqr': self.sigma_z_sqr,
                'w': w,
                'gamma': gamma,
                'eta': eta
            }
            if model == 'A':
                #sigma_eta
                shape = (N + 1) / 2
                scale = 2 / (0.0001 + np.dot(eta.T, eta))
                sigma_eta_sqr = 1 / np.random.gamma(shape, scale)
                #sigma_u
                shape = (NT + 1) / 2
                scale = 2 / (0.0001 + np.dot(u.T, u))
                sigma_u_sqr = 1 / np.random.gamma(shape, scale)
                #decompose for PMCMC
                gamma = np.array([np.log(sigma_eta_sqr), 0])
                xi = np.array([np.log(sigma_u_sqr), 0])
            elif model == 'B':
                delta, z = HMC().sampler(model, **kwargs)
            elif model == 'C':
                xi, gamma = HMC().sampler(model, **kwargs)
            elif model == 'D':
                delta, z, xi, gamma = HMC().sampler(model, **kwargs)

            print('beta')
            print(beta)
            print('delta')
            print(delta)
            print('xi')
            print(xi)
            print('z')
            print(z)
            print('gamma')
            print(gamma)
            print('sigma_alpha_sqr')
            print(sigma_alpha_sqr)
            print('sigma_v_sqr')
            print(sigma_v_sqr)
            print('eta_mean_std')
            print(eta.mean())
            print(eta.std())
            print('u_mean_std')
            print(u.mean())
            print(u.std())
            all_beta[i, :] = beta
            all_xi[i, :] = xi
            all_delta[i, :] = delta
            all_z[i, :] = z
            all_gamma[i, :] = gamma
            all_sigma_alpha_sqr[i] = sigma_alpha_sqr
            all_sigma_v_sqr[i] = sigma_v_sqr
            all_alpha[i, :] = alpha
            all_eta[i, :] = eta
            all_u[i, :] = u
        return all_beta, all_xi, all_delta, all_z, all_gamma, all_sigma_alpha_sqr, all_sigma_v_sqr, all_alpha, all_eta, all_u