Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhawat committed May 11, 2023
1 parent 04a752c commit 5882533
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 177 deletions.
2 changes: 1 addition & 1 deletion src/mcrppy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def jaccobi_measure(x, jac_params):

# utils for monte_carlo_methods
def _find_sum_of_coef_of_cubic_term(poly, d):
"""Function used to find the sum of the coefficient of the quadratic terms in a polynomial regression of degree 2. Used to find the mean of the proposal in ``estimate_control_variate_proposal``.
"""Function used to find the sum of the coefficient of the quadratic terms in a d-dimensional polynomial of degree 2. Used to find the mean of the proposal in ``estimate_control_variate_proposal``.
_extended_summary_
Expand Down
170 changes: 0 additions & 170 deletions tests/test_monte_carlo_methods.py
Original file line number Diff line number Diff line change
@@ -1,170 +0,0 @@
import numpy as np
import pytest
from mcrppy.spatial_windows import UnitBallWindow, BoxWindow
from mcrppy.point_pattern import PointPattern
import mcrppy.monte_carlo_base as ni
from mcrppy.monte_carlo_base import monte_carlo_method, importance_sampling_mc, control_variate_mc, estimate_control_variate_parameter, estimate_control_variate_proposal
import math


def f_1(x):
#indicator ball unit window
d = x.shape[1]
window = UnitBallWindow(center=[0]*d)
return window.indicator_function(x)

def f(x):
#indicator ball unit window
return f_1(x)*1

# test monte_carlo_method
@pytest.mark.parametrize(
"points, expected",
([np.array([[0, 0], [1, 2], [0.5, 0.2], [1, 0], [2, -1]]), 3/5 ],
[np.array([[0, 0, 0], [1, 2, 0], [0.5, 0.2, -0.1], [1, 0, 0], [3, 2, -1]]), 3/5 ]
)
)
def test_monte_carlo_method(points, expected):
result = monte_carlo_method(f=f_1,points= points)
np.testing.assert_array_almost_equal(result, expected)

@pytest.mark.parametrize(
"points, expected",
([np.array([[0, 0], [1, 2], [-1, 0]]), (math.sqrt(5) + 3)/18 ],
)
)
def test_importance_sampling_mc(points, expected):
f = lambda x: np.linalg.norm(x, axis=1)
proposal = lambda x: f(x)**2 +1
result = importance_sampling_mc(points, f, proposal)
np.testing.assert_array_almost_equal(result, expected)

# test control_variate_mc
def test_control_variate_mc():
points = np.array([[0, 0], [1, 2], [-1, 0]])
f = lambda x: np.linalg.norm(x, axis=1)
proposal = lambda x: f(x)**2 +1
mean_proposal = 1
c = 2
result = control_variate_mc(points, f, proposal, mean_proposal, c)
expected = (math.sqrt(5) - 11)/3
np.testing.assert_array_almost_equal(result, expected)

# test estimate_control_variate_parameter
def test_estimate_control_variate_parameter():
points = np.array([[0, 0], [1, 2], [-1, 0]])
f = lambda x: np.linalg.norm(x, axis=1)
proposal = lambda x: f(x)**2 +1
result = estimate_control_variate_parameter(points, f, proposal)
expected = (3*math.sqrt(5) - 1)/14
np.testing.assert_array_almost_equal(result, expected)

# @pytest.mark.parametrize(
# "polydegree, expected",
# ([1, ],
# [2, ]
# )
# )
# # test estimate_control_variate_proposal
# def test_estimate_control_variate_proposal(polydegree, expected):
# points = np.array([[0, 0], [1, 2], [-1, 0]])
# if polydegree==1:
# f = lambda x: np.sum(x, axis=1) + 1
# elif polydegree==2:
# f = lambda x: np.sum(x, axis=1)**2 + 2*np.sum(x, axis=1) -1
# proposal = estimate_control_variate_proposal(points, f, polydegree)
# result = proposal(points)
# np.testing.assert_array_almost_equal(result, expected)

# test kernel
@pytest.mark.parametrize(
"x, choice, expected",
[(np.array([[0, 0]]), "DelPor", 12/(2*np.pi)),
(np.array([[0, 0, 0]]), "DelPor", 15/(2*np.pi)),
(np.array([[0, 0], [0.5, 0], [-1,2]]), "DelPor", np.array([12/(2*np.pi), 9/(4*np.pi), 0])),
(np.array([[0,0], [1, 5], [0.5,0]]), "Epanechnikov", np.array([2/np.pi, 0, 3/(2*np.pi)]))]
)
def test_kernel(x, choice, expected):
result = ni.kernel(x, choice)
np.testing.assert_array_almost_equal(result, expected)

# test leave_one_out_kernel_estimator
@pytest.mark.parametrize(
"idx_out, x, points, bandwidth, expected",
[(1, np.array([[1, 0]]), np.array([[0, 0], [1, 0]]), 1, 0),
(0, np.array([[0.5, 0]]), np.array([[0, 0], [1, 0]]), 1, 9/(4*np.pi)),
(0, np.array([[0.5, 0]]), np.array([[0, 0], [1, 0], [0.5, 0.5]]), 1, 9/(4*np.pi))]
)
def test_leave_one_out_kernel_estimator(idx_out, x, points, bandwidth, expected):
result = ni.leave_one_out_kernel_estimator(idx_out, x, points, bandwidth)
np.testing.assert_array_almost_equal(result, expected)

# test delyon_portier_mc
@pytest.mark.parametrize(
"points, expected",
[(np.array([[0, 0], [0.5, 0], [0, 0.5]]), (8*np.pi)/(9*(11/2 - 5 /np.sqrt(2))))]
)
def test_delyon_portier_mc(points, expected):
window = BoxWindow([[-1,1]]*2)
point_pattern = PointPattern(points, window)
f = lambda x: 2*np.linalg.norm(x, axis=1)
bandwidth=1
result = ni.delyon_portier_mc(f, point_pattern, bandwidth)
np.testing.assert_array_almost_equal(result, expected)

# test bandwidth_0_delyon_portier
def test_bandwidth_0_delyon_portier():
points= np.array([[0, 1], [1, 1], [-1,-1]])
expected =(2**9/5)**(1/6)
result = ni.bandwidth_0_delyon_portier(points)
np.testing.assert_equal(result, expected)

# test variance_kernel
def test_variance_kernel():
points = np.array([[0, 0], [0.25, 0], [0.5, 0.5]])
x = np.array([[0, 0.5]])
idx_out=1
bandwidth = 2
result = ni.variance_kernel(idx_out, x, points, bandwidth)
expected = 0
np.testing.assert_almost_equal(result, expected)

# test integrand_estimatore_core
@pytest.mark.parametrize(
"points, bandwidth, h_0, expected",
[(np.array([[0, 0], [0, 1/4], [0, 1/2], [-1, 0]]),
1/2,
1/5,
(np.array([[0, 0], [0, 1/4], [0, 1/2]]),
np.array([1,1,1]),
np.array([
9/(2*math.pi), 9/math.pi, 9/(2*math.pi)]))
)]
)
def test_integrand_estimate_core( points, bandwidth, h_0, expected):
window = BoxWindow([[-1,1]]*2)
point_pattern = PointPattern(points, window)
result = ni._integrand_estimate_core(f, point_pattern, np.array([bandwidth]), h_0)
for i in range(3):
np.testing.assert_almost_equal(result[i], expected[i])

# test integrand_estimate
@pytest.mark.parametrize(
"x, points, bandwidth, h_0, expected",
[(np.array([[0, 1/4]]), np.array([[0, 0], [0, 1/4], [0, 1/2], [-1, 0]]), 1/2, 1/5, 5**2*2/(3**3))]
)
def test_integrand_estimate(x, points, bandwidth, h_0, expected):
window = BoxWindow([[-1,1]]*2)
point_pattern = PointPattern(points, window)
result = ni.integrand_estimate(x, f, point_pattern, np.array([bandwidth]), h_0)
np.testing.assert_almost_equal(result, expected)

@pytest.mark.parametrize(
" points, bandwidth, h_0, expected",
[(np.array([[0, 0], [0, 1/4], [0, 1/2], [-1, 0]]), 1/2, 1/5, 5*math.pi/(3**3))]
)
def test_integrand_estimate( points, bandwidth, h_0, expected):
window = BoxWindow([[-1,1]]*2)
point_pattern = PointPattern(points, window)
result = ni.integral_integrand_estimate( f, point_pattern, np.array([bandwidth]), h_0)
np.testing.assert_almost_equal(result, expected)
49 changes: 43 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import numpy as np
from mcrppy.utils import sort_points_by_increasing_distance, reshape_output_repelled_point, jaccobi_measure, volume_unit_ball
from mcrppy.utils import sort_points_by_increasing_distance, reshape_output_repelled_point, jaccobi_measure, volume_unit_ball, _find_sum_of_coef_of_cubic_term, error, mse, regression_line


def test_sort_points_by_inc_dist():
Expand All @@ -9,6 +9,11 @@ def test_sort_points_by_inc_dist():
result = sort_points_by_increasing_distance(points)
np.testing.assert_array_almost_equal(result, expected)

def test_volume_unit_ball_window():
d=2
expected = np.pi
result = volume_unit_ball(d)
np.testing.assert_equal(expected, result)

def test_reshape_output_repelled_point():
x = [np.array([[ 1.18, -0.39, 0.871],
Expand All @@ -29,8 +34,40 @@ def test_jaccobi_measure():
result = jaccobi_measure(x, jac_params)
np.testing.assert_array_almost_equal(result, expected)

def test_volume_unit_ball_window():
d=2
expected = np.pi
result = volume_unit_ball(d)
np.testing.assert_equal(expected, result)
def test_find_sum_of_coefficient_of_cubic_term():
poly = lambda x: 2*x[:,0]**2 - 4*x[:,1]**2 + 1.23*x[:,2]**2 + 3*x[:,0] -2
d=3
expected = np.array([2 -4 + 1.23])
result = _find_sum_of_coef_of_cubic_term(poly,d)
np.testing.assert_almost_equal(expected, result)

@pytest.mark.parametrize(
"approx, exact, expected",
( [[1, 2, 3, 0], 0, [1, 2, 3, 0]],
[ [1, 2, 3, 0], 1, [0, 1, 2, -1]],
[1, None, "NAN"],)
)
def test_error(approx, exact, expected):
result = error(approx,exact)
np.testing.assert_equal(result, expected)

@pytest.mark.parametrize(
"exact, expected",
[
(None, "NAN"),
(0, np.array([2, 4, 10, 0])),
(1, np.array([1, 1, 17, 1])),
]
)
def test_mse(exact, expected):
mean = [1, 2, -3, 0]
std = [1, 0, 1, 0]
result = mse(mean, std, exact)
np.testing.assert_equal(result, expected)

def test_regression_line():
x = np.random.rand(100)*5
y = -2.5*x + 1.5
_, slope, _ = regression_line(x,y, log=False)
expected = -2.5
np.testing.assert_almost_equal(expected, slope)

0 comments on commit 5882533

Please sign in to comment.