Xfi = np.linspace(0, 2*np.pi, Nsa)
probs = []  # Vector with probabilities
for i in range(Nsa):
    probs.append([])
    for j in range(Nsa):
        XdataSample = [np.sin(Xthetta[i])*np.cos(Xfi[j]),
                       np.sin(Xthetta[i])*np.sin(Xfi[j]), 
                       np.cos(Xthetta[i])]
        probs[i].append(Wad.Watson_pdf(XdataSample,mu,kappa ))

probs = np.array(probs).T

## Plotting
gl.set_subplots(1,3)
## Plot it in terms of (angle, prob)
gl.plot_3D(Xthetta,Xfi, np.array(probs))
gl.plot_3D(Xthetta,Xfi, np.array(probs), project = "spher")


mu = np.random.randn(5,1);
mu = gf.normalize_module(mu.T).flatten()
## Generate samples
RandWatson = Was.randWatson(Nsampling, mu, kappa)
gl.scatter_3D(RandWatson[:,0],RandWatson[:,1], RandWatson[:,2])

mu_est = Wae.get_MLmean(RandWatson)
kappa_est = Wae.get_MLkappa(mu_est, RandWatson)


mu_est2, kappa_est2 = Wae.get_Watson_muKappa_ML(RandWatson)
print "Real: ", mu, kappa
Пример #2
0
    Xjoint = np.concatenate((X_1,X_2), axis = 1)

    
    std_K = 3
    
    Yjoint = Xjoint.dot(A) + mu
    cov = np.cov(Yjoint.T).T
    std_1, std_2 = np.sqrt(cov[i_1,i_1]),np.sqrt(cov[i_2,i_2])
    
    ################ Contour plot of the scatter plot ################
    gl.init_figure()
    std_K= 3
    xx, yy, zz = bMA.get_gaussian2D_pdf( xbins=60j, ybins=60j, mu = mu, cov = cov, 
                      std_K = std_K, x_grid = None)
    ## Plot the 3D surface
    ax3D = gl.plot_3D(xx, yy, zz, nf = 0,
                      labels = ["slices of the joint distribution","X_1","X_2"], alpha = 0.1)
    
    ## Limits of the plotting !
    xmin,xmax = [np.min(xx.flatten()), np.max(xx.flatten())]
    ymin,ymax = [np.min(yy.flatten()), np.max(yy.flatten())]
    zmin,zmax = [np.min(zz.flatten()), np.max(zz.flatten())]
    
    xmin = xmin - (xmax - xmin)*0.2
    xmax = xmax + (xmax - xmin)*0.2
    ymin = ymin - (ymax - ymin)*0.2
    ymax = ymax + (ymax - ymin)*0.2
    
    xymin, xymax = [np.min([xmin,ymin]),np.max([xmax,ymax])]
    
    # Plot the marginalization of Y
    x_1_list = np.linspace(-7,2,5)
gl.plot(lags, residuals)
##########################################################
# Plotting of the regression
plotting_3D = 1
if (plotting_3D):
    #    Y_data = np.sign(Y_data)
    gl.scatter_3D(X_data[:, 0],
                  X_data[:, 1],
                  Y_data,
                  labels=["", "", ""],
                  legend=["Pene"],
                  nf=1)

    grids = ul.get_grids(X_data)
    z = bMA.get_plane_Z(grids[0], grids[1], params)
    h = gl.plot_3D(grids[0], grids[1], z, nf=0)

###################################
##### TODO ##################3
##################################

# How it would be to take reconstruct from the diff signal

# We shift to the right to that
# v[i] = x[i] - x[i-1] and x[i]
# If we do v[j] = v[i+1] = x[i+1] - x[i]
# Then v[j] + x[i] = x[i+1]
# So then we shift to the left

#vel_shifted = np.roll(MACD_vel[:,[indx]],-1, axis = 0)
#reconstruction = vel_shifted + ul.fnp(MACD[:,indx])
Пример #4
0
    gl.plot(lags, residuals)

############## Plotting of 3D regression ##############
plotting_3D = 1
if (plotting_3D):
    # We train 3 models 
#    Y_data = np.sign(Y_data)

    mask_X = np.sum(np.isnan(X_data), axis = 1) == 0
    mask_Y = np.isnan(Y_data) == 0
    mask = mask_X & mask_Y[:,0]  # Mask without NaNs. This is done automatically in the OLS
    
    # Create linear regression object
    regr = linear_model.LinearRegression()
    # Train the model using the training sets
    X_datafiltered = X_data[:,[0,1]]
    regr.fit(X_datafiltered[mask,:], Y_data[mask,:])
     #    coeffs = np.array([regr.intercept_, regr.coef_])[0]
    coeffs = np.append(regr.intercept_, regr.coef_)
    params = np.array(coeffs)
    
    gl.scatter_3D(X_data[:,0],X_data[:,1], Y_data,
                   labels = ["","",""],
                   legend = ["Pene"],
                   nf = 1)
                   
    grids = ul.get_grids(X_data)
    z = bMA.get_plane_Z(grids[0], grids[1],params)
    h = gl.plot_3D(grids[0],grids[1],z, nf = 0)
        
    Y = A.dot(X)
#    ax3D = gl.scatter_3D(Y[0,:], Y[1,:], Y[2,:], nf = 0,
#                      labels = ["slices of the joint distribution","X_1","X_2"], alpha = 0.6)
    

    ######## DRAWING THE HYPERPLANE #########
    v = R[0,:]
    
    nx = 1
    grid_x = np.linspace(-nx,nx,20)
    grid_y = np.linspace(-nx,nx,20)

    params = [0, -v[0]/v[2], -v[1]/v[2]]
    zz = bMA.get_plane_Z(grid_x, grid_y, params)
    xx, yy = np.meshgrid(grid_x, grid_y, sparse=True)
    ax3D = gl.plot_3D(xx, yy, zz, nf = 0,
                      labels = ["slices of the joint distribution","X_1","X_2"], alpha = 0.8)

    #### Plotting the vectors
    nx = 5
    v = R[0,:]
    ax3D.plot([-nx*v[0], nx*v[0]],[-nx*v[1], nx*v[1]],[-nx*v[2], nx*v[2]], lw = 3)
    v = R[1,:]
    ax3D.plot([-nx*v[0], nx*v[0]],[-nx*v[1], nx*v[1]],[-nx*v[2], nx*v[2]], lw = 3)
    v = R[2,:]
    ax3D.plot([-nx*v[0], nx*v[0]],[-nx*v[1], nx*v[1]],[-nx*v[2], nx*v[2]], lw = 3)
    
#    ax3D.axis('equal')
#    ax.plot_wireframe(x, y, z,  rstride=4, cstride=4, color='b', alpha=0.2)
    
    ## Limits of the plotting !
    xmin,xmax = [np.min(xx.flatten()), np.max(xx.flatten())]
Пример #6
0
    
    xx, yy, zz = bMA.kde2D(ret1,ret2, bandwidth = np.std(ret1)/kde_K,
                           xbins=n_grids*1j, ybins=n_grids*1j)
    ax1.contour(xx, yy, zz)
    
    ax1.axis('equal')
    
    gl.savefig(folder_images +'KDEHistogramCLOSE.png', 
               dpi = 100, sizeInches = [18, 14])
               
if(distribution_graph3D):
    ################ Contour plot of the scatter plot ################
    xx, yy, zz = bMA.kde2D(ret1,ret2, bandwidth = np.std(ret1)/kde_K,
                           xbins=n_grids*1j, ybins=n_grids*1j)
    ## Plot the 3D surface
    ax3D = gl.plot_3D(xx, yy, zz, nf = 1)
    
    ## Limits of the plotting !
    xmin,xmax = [np.min(xx.flatten()), np.max(xx.flatten())]
    ymin,ymax = [np.min(yy.flatten()), np.max(yy.flatten())]
    zmin,zmax = [np.min(zz.flatten()), np.max(zz.flatten())]
    
    # Plot the marginalization of X
    x_grid = np.linspace(min(ret1),max(ret1),n_grids)
    y_val = bMA.kde_sklearn(ret1, x_grid, bandwidth=np.std(ret1)/kde_K)  
    ax3D.plot(x_grid,y_val,ymax, zdir='y')
    ax3D.add_collection3d(plt.fill_between(x_grid,y_val, 0, color='k', alpha=0.3),ymax, zdir='y')
  
    # Plot the marginalization of Y
    x_grid = np.linspace(min(ret2),max(ret2),n_grids)
    y_val = bMA.kde_sklearn(ret2, x_grid, bandwidth=np.std(ret2)/kde_K)  
    cov = np.cov(np.concatenate((X_1, X_2), axis=1).T).T
    std_K = 3

    ################ Contour plot of the scatter plot ################
    gl.init_figure()
    std_K = 3
    xx, yy, zz = bMA.get_gaussian2D_pdf(xbins=30j,
                                        ybins=30j,
                                        mu=mu,
                                        cov=cov,
                                        std_K=std_K,
                                        x_grid=None)
    ## Plot the 3D surface
    ax3D = gl.plot_3D(xx,
                      yy,
                      zz,
                      nf=0,
                      labels=["2D Gaussian pdf", "X_1", "X_2"])

    ## Limits of the plotting !
    xmin, xmax = [np.min(xx.flatten()), np.max(xx.flatten())]
    ymin, ymax = [np.min(yy.flatten()), np.max(yy.flatten())]
    zmin, zmax = [np.min(zz.flatten()), np.max(zz.flatten())]

    xmin = xmin - (xmax - xmin) * 0.2
    xmax = xmax + (xmax - xmin) * 0.2
    ymin = ymin - (ymax - ymin) * 0.2
    ymax = ymax + (ymax - ymin) * 0.2

    xymin, xymax = [np.min([xmin, ymin]), np.max([xmax, ymax])]
Пример #8
0
    ax1.axis('equal')

    gl.savefig(folder_images + 'KDEHistogramCLOSE.png',
               dpi=100,
               sizeInches=[18, 14])

if (distribution_graph3D):
    ################ Contour plot of the scatter plot ################
    xx, yy, zz = bMA.kde2D(ret1,
                           ret2,
                           bandwidth=np.std(ret1) / kde_K,
                           xbins=n_grids * 1j,
                           ybins=n_grids * 1j)
    ## Plot the 3D surface
    ax3D = gl.plot_3D(xx, yy, zz, nf=1)

    ## Limits of the plotting !
    xmin, xmax = [np.min(xx.flatten()), np.max(xx.flatten())]
    ymin, ymax = [np.min(yy.flatten()), np.max(yy.flatten())]
    zmin, zmax = [np.min(zz.flatten()), np.max(zz.flatten())]

    # Plot the marginalization of X
    x_grid = np.linspace(min(ret1), max(ret1), n_grids)
    y_val = bMA.kde_sklearn(ret1, x_grid, bandwidth=np.std(ret1) / kde_K)
    ax3D.plot(x_grid, y_val, ymax, zdir='y')
    ax3D.add_collection3d(plt.fill_between(x_grid,
                                           y_val,
                                           0,
                                           color='k',
                                           alpha=0.3),