コード例 #1
0
 def randomSGCD(cls,lam,tau,rho):
     newPPP = cls.randomHomog(lam)
     newGP = GP(zeroMean,expCov(1,1))
     marks = newGP.rGP(newPPP.loc)
     index = np.array(np.greater(expit(marks),random.uniform(size=marks.shape)))
     newPPP.loc = newPPP.loc[np.squeeze(index)]
     return(newPPP)
コード例 #2
0


lam=500
tau=1/4
rho=5

def expit(x):
    return(np.exp(x)/(1+np.exp(x)))




locs = PPP.randomHomog(lam).loc

newGP = GP(zeroMean,expCov(tau,rho))
GPvals = newGP.rGP(locs)

locProb  = expit(GPvals)
index = np.array(np.greater(locProb,random.uniform(size=locProb.shape)))
locObs = locs[np.squeeze(index)]


fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')

# plt.plot(pointpo.loc[:,0],pointpo.loc[:,1], 'o')
plt.plot(locObs[:,0],locObs[:,1], 'o')
plt.xlim(0,1)
plt.ylim(0,1)
コード例 #3
0
def MCMCadams(size, lam_init, rho_init, T_init, thismtPP, nInsDelMov, kappa,
              delta, L, mu_lam, sigma2, p, a, b, n, V, mu_init, mean_mu,
              var_mu, diagnostics, res, thin, GP_mom_scale, range_mom_scale):

    ### independent type prior mean

    ### initialize GP
    thisGP = GP(zeroMean, expCov(1, rho_init))

    ### location container initialization
    K = thismtPP.K
    totLocInit = np.concatenate(
        (thismtPP.locs, PPP.randomHomog(lam=int(lam_init // (K + 1))).loc), 0)
    nObs = thismtPP.nObs

    locations = bdmatrix(int(10 * lam_init), totLocInit, nObs,
                         "locations")  # initial size is a bit of black magic

    ### cov matrix initialization

    Rmat = dsymatrix(int(10 * lam_init), thisGP.covMatrix(totLocInit), nObs)

    ### GP values container initialization

    # ### try to initiate GP in logical position

    # mean_init = np.zeros(shape=(locations.nThin+nObs,K))

    # mean_init[:nObs,:] = np.transpose(np.linalg.cholesky(np.linalg.inv(T_init))@thismtPP.typeMatrix)

    # ####

    values = bdmatrix(
        int(10 * lam_init),
        matrix_normal.rvs(rowcov=Rmat.sliceMatrix(),
                          colcov=np.linalg.inv(T_init)) + mu_init, nObs,
        "values")

    ### parameters containers
    lams = np.empty(shape=(size))
    rhos = np.empty(shape=(size))

    Ts = np.empty(shape=(size, K, K))
    mus = np.empty(shape=(size, 1, K))
    Nthins = np.empty(shape=(size))

    ### independent type prior mean
    Vm1 = np.linalg.inv(V)

    ###
    lams[0] = lam_init
    rhos[0] = rho_init
    Ts[0] = T_init
    mus[0] = mu_init
    Nthins[0] = locations.nThin

    ### instantiate containers for diagnostics
    if diagnostics:
        danceLocs = np.empty(shape=(int(size // thin), int(10 * lam_init), 2))
        GPvaluesAtObs = np.empty(shape=(int(size // thin), nObs, K))
        fieldsGrid = np.empty(shape=(int(size // thin), res**2, K + 1))

        danceLocs[0, :int(nObs + Nthins[0])] = locations.totLoc()
        GPvaluesAtObs[0] = values.obsLoc()

        gridLoc = makeGrid([0, 1], [0, 1], res)

        s_11 = thisGP.cov(gridLoc, gridLoc)
        S_21 = thisGP.cov(locations.totLoc(), gridLoc)

        S_12S_22m1 = np.dot(np.transpose(S_21), Rmat.inver)

        muGrid = np.dot(S_12S_22m1, values.totLoc() - mus[0]) + mus[0]
        spatSig = s_11 - np.dot(S_12S_22m1, S_21)

        A = np.linalg.cholesky(Ts[0])

        Am = sp.linalg.solve_triangular(A, np.identity(K), lower=True)

        newVal = np.linalg.cholesky(spatSig) @ np.random.normal(
            size=(res**2, K)) @ Am + muGrid

        fieldsGrid[0] = lams[0] * np.array([multExpit(val) for val in newVal])

    i = 1
    diagnb = 1
    while i < size:

        j = 0
        while j < nInsDelMov:
            birthDeathMove(lams[i - 1], kappa, thisGP, locations, values, Rmat,
                           Ts[i - 1], mus[i - 1])
            j += 1

        Nthins[i] = locations.nThin

        # # locTot_prime = np.concatenate((locThin_prime,thisPPP.loc))
        # valTot_prime = np.concatenate((valThin_prime,obsVal[i-1]))

        # nthin = locThin_prime.shape[0]

        # # Sigma = thisGP.covMatrix(locTot_prime)
        # A = np.linalg.cholesky(Sigmas[i])
        # ntot = A.shape[0]

        # whiteVal_prime = sp.linalg.solve_triangular(A,np.identity(ntot),lower=True)@valTot_prime

        # functionSampler(delta,L,values,Sigma)

        rhos[i] = functionRangeSampler(delta, L, values, Rmat, rhos[i - 1],
                                       Ts[i - 1], mus[i - 1],
                                       thismtPP.typeMatrix, a, b, GP_mom_scale,
                                       range_mom_scale)
        Ts[i] = typePrecisionSampler(n, Vm1, values, Rmat, mus[i - 1])
        mus[i] = typeMeanSampler(values, Rmat, Ts[i], mean_mu, var_mu)
        thisGP = GP(zeroMean, expCov(1, rhos[i]))

        # valTot_prime = A @ whiteVal_prime

        # thinLoc[i] = locThin_prime
        # thinVal[i] = valTot_prime[:nthin,:]
        # obsVal[i] = valTot_prime[nthin:,:]

        # ntot = valTot_prime.shape[0]

        lams[i] = intensitySampler(mu_lam, sigma2, values.nThin + values.nObs)

        if diagnostics and i % thin == 0:

            danceLocs[diagnb, :int(nObs + Nthins[i])] = locations.totLoc()
            GPvaluesAtObs[diagnb] = values.obsLoc()

            s_11 = thisGP.cov(gridLoc, gridLoc)
            S_21 = thisGP.cov(locations.totLoc(), gridLoc)

            S_12S_22m1 = np.dot(np.transpose(S_21), Rmat.inver)

            muGrid = np.dot(S_12S_22m1, values.totLoc() - mus[i]) + mus[i]
            spatSig = s_11 - np.dot(S_12S_22m1, S_21)

            A = np.linalg.cholesky(Ts[i])

            Am = sp.linalg.solve_triangular(A, np.identity(K), lower=True)

            newVal = np.linalg.cholesky(spatSig) @ np.random.normal(
                size=(res**2, K)) @ Am + muGrid

            fieldsGrid[diagnb] = lams[i] * np.array(
                [multExpit(val) for val in newVal])

            diagnb += 1

        if p:
            ### next sample
            locations.nextSamp()
            values.nextSamp()

        print(i)
        i += 1

    if diagnostics:

        ### dancing locations plot
        mpdf = PdfPages('0thinLocs.pdf')

        diagnb = 0
        while (diagnb < int(size // thin)):

            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.set_aspect('equal')

            plt.plot(danceLocs[diagnb, nObs:int(nObs + Nthins[diagnb * thin]),
                               0],
                     danceLocs[diagnb, nObs:int(nObs + Nthins[diagnb * thin]),
                               1],
                     'o',
                     c=(0.75, 0.75, 0.75))

            for pp in thismtPP.pps:
                plt.plot(pp.loc[:, 0], pp.loc[:, 1], 'o')
            plt.xlim(0, 1)
            plt.ylim(0, 1)

            # plt.show()
            mpdf.savefig(bbox_inches='tight')
            plt.close(fig)

            diagnb += 1

        mpdf.close()

        ### GP traces at observerd locations

        fig, axs = plt.subplots(nObs, figsize=(10, 1.5 * nObs))

        obsNB = 0
        colNB = 0
        while (obsNB < nObs):
            colNB = 0
            while (colNB < K):

                if thismtPP.typeMatrix[colNB, obsNB] == 1:

                    axs[obsNB].plot(GPvaluesAtObs[:, obsNB, colNB],
                                    linewidth=2)

                else:
                    axs[obsNB].plot(GPvaluesAtObs[:, obsNB, colNB],
                                    linestyle="dashed")

                colNB += 1

            obsNB += 1

        # plt.show()
        fig.savefig("0GPtraces.pdf", bbox_inches='tight')
        plt.close(fig)

        ### mean intensities

        meanFields = np.mean(fieldsGrid, axis=0, dtype=np.float32)

        maxi = np.max(meanFields)
        mini = np.min(meanFields)

        k = 0
        while k < K + 1:

            fig = plt.figure()
            ax = fig.add_subplot(111)
            ax.set_aspect('equal')

            plt.xlim(0, 1)
            plt.ylim(0, 1)

            imGP = np.transpose(meanFields[:, k].reshape(res, res))

            x = np.linspace(0, 1, res + 1)
            y = np.linspace(0, 1, res + 1)
            X, Y = np.meshgrid(x, y)

            # fig = plt.figure()
            # axs[k] = fig.add_subplot(111)
            ax.set_aspect('equal')

            ff = ax.pcolormesh(X, Y, imGP, cmap='gray', vmin=mini, vmax=maxi)

            fig.colorbar(ff)

            for pp in thismtPP.pps:
                ax.plot(pp.loc[:, 0], pp.loc[:, 1], 'o', c="tab:orange")

            # plt.scatter(pointpo.loc[:,0],pointpo.loc[:,1], color= "black", s=1)

            # plt.show()

            fig.savefig("0IntFields" + str(k) + ".pdf", bbox_inches='tight')
            plt.close(fig)

            k += 1

        # fig.savefig("0IntFields.pdf", bbox_inches='tight')
        # plt.close(fig)

    return (lams, rhos, Ts, mus, Nthins)
コード例 #4
0
    i = 0
    for x in xs:
        j = 0
        for y in ys:
            grid[i * res + j, :] = [x, y]
            j += 1
        i += 1
    return (grid)


res = 50
gridLoc = makeGrid([0, 1], [0, 1], res)

locs = PPP.randomHomog(lam).loc

newGP = GP(zeroMean, expCov(tau, rho))
GPvals = newGP.rGP(np.concatenate((locs, gridLoc)))

gridInt = lam * expit(GPvals[locs.shape[0]:, :])

locProb = expit(GPvals[:locs.shape[0], :])
index = np.array(np.greater(locProb, random.uniform(size=locProb.shape)))
locObs = locs[np.squeeze(index)]
locThin = locs[np.logical_not(np.squeeze(index))]

### cox process

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
コード例 #5
0
lam=400
rho=2

locs = PPP.randomHomog(lam).loc

### using function

def fct(x):
    return(1-(np.exp(-np.minimum((x[:,0]-0.5)**2,(x[:,1]-0.5)**2)/0.007)))

locs = PPP.randomNonHomog(lam,fct).loc
###


newGP = GP(zeroMean,expCov(1,rho))

U = newGP.covMatrix(locs)

n=5
eps=1/n
X = np.array([[0,eps,1],[0,-eps,1],[eps,0,-1]])
V = [email protected](X)

# ## 2D case
# V = np.array([[1,0.99],[0.99,1]])


X = matrix_normal.rvs(rowcov=U, colcov=V)

def multExpit(x):