コード例 #1
0
def mcmc(samp,z,init,cov,prior_mu,samples=1000,proposal_width=0.1):
    
    posterior = np.empty((0,len(init)))
    
    prior_std = cov
    
    var_current = []
    for var in init:
        var_current.append(var)
    
    for sample in range(samples):
        
        post = []
        
        for i in range(len(init)):
            
            
            var_proposed = norm(var_current[i],proposal_width).rvs()
            
            params_prop = [a for a in var_current]
            params_prop[i] = var_proposed
            
            
            #Assuming sigma=1 for the simplest case
            
            
            likelihood_current = mv(mean=var_current,cov=cov).pdf(samp)
            likelihood_proposed = mv(mean=params_prop,cov=cov).pdf(samp)
            
            prior_current = mv(mean=prior_mu,cov=prior_std).pdf(var_current)
            prior_proposed = mv(mean=prior_mu,cov=prior_std).pdf(params_prop)
            
            p_current = np.sum([np.log(a) for a in likelihood_current])*prior_current
            p_proposed = np.sum([np.log(a) for a in likelihood_proposed])*prior_proposed
            #print(p_proposed/p_current)
            accept = p_proposed / p_current > np.random.rand()
            
            if accept:
                #print(f"{i}  |  {var_current}  ->  {var_proposed}")
                var_current[i] = var_proposed
            post.append(var_current[i])
        posterior = np.r_[posterior,[post]]
    return posterior
コード例 #2
0
# In[1]:

import seaborn as sb
from scipy.stats import multivariate_normal as mv
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
from numpy import *
# q1
# create the target distribution
# sample at .1 interval
tmp = arange(-2, 2, .1)
x, y = meshgrid(tmp, tmp)

mu = [0,0]
sigma = eye(len(mu)) * 2/5
dist = mv(mu, sigma)

X = vstack((x.flatten(), y.flatten())).T
Y = dist.pdf(X).reshape(x.shape)  * 3
targets = array(Y.flatten(), ndmin = 2).T

fig, ax = subplots(1, 1, subplot_kw = {'projection': '3d'})
ax.plot_surface(x, y, targets.reshape(x.shape))
ax.set_xlabel('$x_1$', labelpad = 20)
ax.set_ylabel('$x_2$', labelpad = 20)
ax.set_zlabel('pdf', labelpad =20)
sb.set_context('poster')
sb.set_style('white')
show()

コード例 #3
0
#plt.legend()
if(save_plt):
    plt.savefig("goal_init.pdf",bbox_inches='tight')
    
    
#==============================================================================
# Prior Samples
#==============================================================================
ker_mimic = GPy.kern.Matern52(variance = 1.0, lengthscale = 0.147, input_dim =1)
nb_points_path = 100
nb_func = 50
X_kernel = np.linspace(0.,1., nb_points_path)[:,None]
mu_kernel = np.zeros((nb_points_path))
C_kernel = ker_mimic.K(X_kernel,X_kernel)
sample_paths = np.squeeze(np.random.multivariate_normal(mu_kernel, C_kernel, nb_func))
mv0 = mv(mean = mu_kernel, cov = C_kernel, allow_singular=True)
log_p = mv0.logpdf(sample_paths)
tmp =  np.power(log_p / np.sum(log_p), 8)
scale = 2 /np.max(tmp)
p_norm = scale * tmp
color_tmp = get_plot_infos(p_norm, cm.Blues)
index_tmp = np.argsort(p_norm)

for n in index_tmp:
    plt.plot(X_kernel[:], sample_paths[n], color = color_tmp[n], linewidth = p_norm[n])
plt.ylabel(r"$f(x)$")
plt.xlabel(r"$x$")
if(save_plt):
    plt.savefig("prior_samples.pdf",bbox_inches='tight')

コード例 #4
0
 def f(self, x, y):
     self.bivariate = mv(
         [self.mu1, self.mu2],
         [[self.sig1**2, self.sig12], [self.sig12, self.sig2**2]])
     return self.bivariate.pdf((x, y))
コード例 #5
0
then the corresponding eigenvalue of C will be λi + β−1.
It is therefore sufficient that the kernel matrix k(xn,xm) be
positive semidefinite for any pair of points xn and xm, so that λi ? 0,
because any eigenvalue λi that is zero will still give rise to a positive eigenvalue for
C because β> 0. [Bisschop 308]
'''

L = chol(K)
# this gives an error. is something wrong with the kernel?
l = rank(L)
# it would be surprising, if this was >= 0

# q4
mu = np.zeros(len(x))
# for some reason it errors on singularity?
prior = mv(mu, K, allow_singular=1)
samples = prior.rvs(5)

fig, ax = subplots(1, 1)
for i in samples:
    ax.plot(x, i)
savefig('../Figures/ex_1_test')

# q5
thetas = np.array([ [ 1, 4, 0, 0 ], [ 9, 4, 0, 0 ], [ 1, 64, 0, 0 ],
                    [ 1, 0.25, 0, 0 ], [ 1, 4, 10, 0 ], [ 1, 4, 0, 5 ] ])

# i want at most 3 rows
nRows = 3
# get the the number of columns
nCols = thetas.shape[ 0 ] // nRows
コード例 #6
0
def overlay(i):

    print(i)
    tag = "{:0>4}".format(i)
    global counter

    imgname = "{}_a.png".format(tag)
    imgpath = os.path.join(abspath,imgname)
    image = cv2.imread(imgpath)

    maskpath = os.path.join(abspath,"{}_masks.png".format(tag))

    depthpath = os.path.join(abspath,"{}_npdepth.npy".format(tag))
    depthmap = np.load(depthpath,allow_pickle=False)

    projmat = fu.matrix_from_string(data["projection"])

    masks = separate(maskpath)
    verts = []
    p=False

    #totalimg = np.zeros((256,256)).astype(np.uint8)

    outimg = np.zeros((256,256)).astype(np.uint8)


    for hue in masks:

        objname = getObjFromHue(hue)
        
        # if objname:
        #     objclass = objname.split(".")[0]
        #     studs = fu.get_object_studs(objclass)
        #     for stud in studs:
        #         stud.append(1.0)
        # else:
        #     continue

        if not objname:
            continue

        #studmask = np.zeros((512,512),dtype=np.uint8)
        #maskedimg = np.zeros((512,512,3),dtype=np.uint8)
        inds={0:30,1:60,2:90,3:120,4:150}
        if "WingR" in objname:
            studs = [[-.94,1.86,0.0,1.0], [-.94,-1.86,0.0,1.0], [-0.0,-1.86,0.0,1.0], [.94,1.86,0.0,1.0]]#[.9,.9,0.0,1.0], [.9,1.8,0.0,1.0]]
        elif "WingL" in objname:
            studs = [[-.94,-1.86,0.03,1.0] , [-.94,1.86,0.03,1.0], [-0.0,1.86,0.03,1.0], [.94,-1.86,0.03,1.0]]#[.9,-.9,0.032,1.0], [.9,-1.8,0.032,1.0]]
        elif 
        """if "Slope1" in objname:
            studs = [[0.0,0.0,0.95487,1.0]]
        elif "Slope3" in objname:
            studs = [[0.0,0.579934,1.17411,1.0],[0.0,-0.579934,1.17411,1.0]]
        else:
            studs = [[-0.579934,-0.579934,0.579934,1.0],[-0.579934,0.579934,0.579934,1.0],[0.579934,-0.579934,0.579934,1.0],[0.579934,0.579934,0.579934,1.0]]
        """
        studs = np.array(studs,dtype=np.float32)
        #studs[0:3] = .95 * studs[0:3]

        modelmat = fu.matrix_from_string(data["objects"][objname]["modelmat"])
        viewmat = fu.matrix_from_string(data["viewmats"][i])

        screenverts = fu.verts_to_screen(modelmat, viewmat, projmat, studs, filter=True)

        if screenverts is None:
            continue

        screenverts[:,0:2] = fu.toNDC(screenverts[:,0:2], (512,512))
        visibleverts = screenverts# [v for v in screenverts if depthmap[int(v[1]),int(v[0])] - abs(v[2]) > -0.05]

        #maskedimg = cv2.bitwise_and(image,image,mask=masks[hue])
        #cv2.imwrite(os.path.join(write_path,"{}_{}_masked.png".format(tag,counter)), cv2.resize(maskedimg, (256,256), interpolation=cv2.INTER_LINEAR))

        #outimg = np.zeros((256,256,3)).astype(np.uint8)
        #outimg = np.zeros((256,256)).astype(np.uint8)

        finalverts = []

        for v in visibleverts:
            x= int(v[0])
            y=int(v[1])
            val = masks[hue][y,x]

            x= int(v[0]/2)
            y=int(v[1]/2)
            if val > 0:
                pos = np.dstack(np.mgrid[0:256:1, 0:256:1])
                rv = mv(mean=[y,x], cov=7)
                pd = rv.pdf(pos)
                
                img = pd/np.amax(pd)
                gimg = np.rint(255 * img)
                gimg = gimg.astype(np.uint8)

                #cv2.circle(outimg, (x,y), 3, (255,255,255),-1)
                #outimg[y,x] = 255
                outimg = outimg + gimg
                #totalimg = totalimg + gimg
            #coord = np.array([x,y],dtype=np.float32)
            #else:
            #    coord = np.array([np.nan,np.nan])
            #finalverts.append(coord)

        #f = np.array(finalverts)
        #print(f.shape)
        #np.save(os.path.join(write_path,"{}_kpts.npy".format(counter)), f)

        #cv2.imwrite(os.path.join(write_path,"{}_{}_kpts.png".format(tag,counter)), outimg)
        #counter += 1
    cv2.imwrite(os.path.join(write_path,"{}_img.png".format(tag)), image)
    cv2.imwrite(os.path.join(write_path,"{}_kpts.png".format(tag)), outimg)




def iterOverlay(indices):
    for ind in indices:
        overlay(ind)


indices = np.arange(data["runs"]) if args.num is None else [args.num] 
cores = mp.cpu_count()
num_procs = 1 if len(indices) < cores else cores
indices_lists = np.array_split(indices, num_procs)
#print(indices_lists)

processes = []

counter = 0
iterOverlay(indices)


'''
コード例 #7
0
ファイル: ex1_try.py プロジェクト: cvanelteren/SML
x = linspace(-1, 1, N)
K = kernel(x, x, theta)
print(K.shape)
# q3
'''
K = N x N = 101 x 101
We can show semipositive definite by showing that all the eigenvalues are >= 0
'''
eigval, eigvec = linalg.eig(K)
# linalg returns complex values, but the complex part is zero
print(real(eigval))

# q4
from scipy.stats import multivariate_normal as mv
mu = zeros(len(x))
prior = mv(mu, K, allow_singular=1)
print(linalg.matrix_rank(K))

samples = prior.rvs(5)

fig, ax = subplots(1, 1)
for i in samples:
    ax.plot(x, i)
savefig('../Figures/ex_1_test')

# q5
thetas = np.array([[1, 4, 0, 0], [9, 4, 0, 0], [1, 64, 0, 0], [1, 0.25, 0, 0],
                   [1, 4, 10, 0], [1, 4, 0, 5]])

# i want at most 3 rows
nRows = 3
コード例 #8
0
plt.rc('text', usetex=True)
#==============================================================================
# ***GOAL Present basis of GP 
#==============================================================================
nb_points_path = 50
nb_func = 50

l = 0.2
k = GPy.kern.RBF(input_dim=1,lengthscale = l, variance = 1.0)
X = np.linspace(0.,1., nb_points_path)
X = X[:,None]
mu = np.zeros((nb_points_path))
C = k.K(X,X)
Z = np.squeeze(np.random.multivariate_normal(mu, C, nb_func))
mv0 = mv(mean = mu, cov = C , allow_singular=True)
log_p = mv0.logpdf(Z)
tmp =  np.power(log_p / np.sum(log_p), 8)
scale = 1.2 /np.max(tmp)
p_norm = scale * tmp
for n, zz in enumerate(Z):
    plt.plot(X[:], zz, color = 'blue', linewidth = p_norm[n])
plt.savefig('prior_more_3.pdf')
#plot one func = 
nb_1 = 3
x_fin = X[-1,0]

x_lim = [0, 1.35]
y_lim = [-2, 2]

arbitray_labels = [r"p(f)=0.1", r"p(f)=0.3", r"p(f)=0.6"]
コード例 #9
0
            errors.append(np.mean((self.targets - outputs[-1])**2))
            # if idx % 1000:
            #     # self.eta = np.random.randn() *2
            #     print(errors[-1])
            if idx > maxiter:
                alpha = False
                return errors, idx
            idx += 1


close('all')
# q1
tmp = np.arange(-2, 2, .1)
x, y = np.meshgrid(tmp, tmp)

dist = mv([0, 0], np.eye(2) * 2 / 5. * 3)

X = np.vstack((x.flatten(), y.flatten()))
Y = dist.pdf(X.T).reshape(x.shape)
fig, ax = subplots(2, 1, subplot_kw={'projection': '3d'})

ax[0].plot_surface(x, y, Y, cmap='viridis')
# show()
# print(Y.shape); assert 0

tmp = state = [X.T, np.array(Y.flatten(), ndmin=2).T]
net = mlp(M=8, eta=1e-1, n_patterns=4, state=tmp)
errors, iters = net.train(threshold=1e-1, maxiter=int(1000))
out = net.forward_pass(net.patterns)
# print(errors[-1])
print((out[-1] - net.targets)**2, iters)
コード例 #10
0
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal as mv
from scipy.stats import norm

func = mv(mean=[1,9],cov=[[3,0],[0,2]])

x, y = np.mgrid[-2.0:4.0:100j, 6.0:12.0:100j]
xy = np.column_stack([x.flat,y.flat])
z = func.pdf(xy)
z = z.reshape(x.shape)

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.plot_surface(x,y,z,alpha=0.8)
ax.set_title('Base Function')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('MV Gaussian')
plt.savefig('pdf.pdf')


def mcmc(samp,z,init,cov,prior_mu,samples=1000,proposal_width=0.1):
    
    posterior = np.empty((0,len(init)))
    
    prior_std = cov
    
コード例 #11
0
ファイル: p3.py プロジェクト: NPSDC/Codes
def gaussian_multivariate(mu, cov):
    return mv(mean = mu, cov = cov)