Пример #1
0
def test_gaussian_kde_scipy_vs_cocos(points: np.ndarray,
                                     xi: np.ndarray):
    gkde_cocos = gaussian_kde(points)
    gkde_scipy = ss.kde.gaussian_kde(points)

    density_estimate_cocos = gkde_cocos.evaluate(xi)
    density_estimate_scipy = gkde_scipy.evaluate(xi)

    assert np.allclose(density_estimate_cocos,
                       density_estimate_scipy)
Пример #2
0
def test_gaussian_kde_scipy_vs_cocos_gpu(points: np.ndarray,
                                         xi: np.ndarray):
    gkde_cocos = gaussian_kde(cn.array(points.squeeze()),
                              gpu=True)
    gkde_scipy = ss.kde.gaussian_kde(points)

    density_estimate_cocos = gkde_cocos.evaluate(xi)
    density_estimate_scipy = gkde_scipy.evaluate(xi)

    assert np.allclose(density_estimate_cocos,
                       density_estimate_scipy)
Пример #3
0
    # generate grid at which to evaluate the sample
    x_grid = np.linspace(-5.0, 5.0, grid_size)
    y_grid = np.linspace(-5.0, 5.0, grid_size)
    xy_grid_x, xy_grid_y = np.meshgrid(x_grid, y_grid)
    xy_grid = np.hstack((xy_grid_x.reshape((-1, 1)), xy_grid_y.reshape(
        (-1, 1)))).T
    # print(f'xy_grid.shape = {xy_grid.shape}')

    # construct and evaluate scipy gaussian kde object
    gaussian_kde_scipy = ss.kde.gaussian_kde(points_xy)
    density_estimate_scipy = gaussian_kde_scipy.evaluate(xy_grid)
    # print(f'density_estimate_scipy.shape = {density_estimate_scipy.shape}')

    # construct and evaluate cocos gaussian kde object using gpu evaluation
    gaussian_kde_cocos = gaussian_kde(cn.array(points_xy), gpu=True)
    density_estimate_cocos = np.array(gaussian_kde_cocos.evaluate(xy_grid))
    # print(f'density_estimate_cocos.shape = {density_estimate_cocos.shape}')

    # evaluate cocos gaussian kde object using gpu evaluation in batches
    batched_density_estimate_cocos = \
        gaussian_kde_cocos.evaluate_in_batches(xy_grid, 10 * n * n)
    # print(f'batched_density_estimate_cocos.shape = {batched_density_estimate_cocos.shape}')

    # verify that results are numerically close
    print(
        f'maximum absolute difference between results gpu using Cocos and cpu using SciPy: '
        f'{np.max(abs(density_estimate_cocos - density_estimate_scipy))}')

    if np.allclose(density_estimate_cocos, density_estimate_scipy):
        print('estimates from cocos and scipy are numerically close')