Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tests/test_fjacobi_inv.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import unittest

import numpy as np
import unittest

from UncertainSCI.families import JacobiPolynomials
import UncertainSCI.families as families


class FastjacobiinvTestCase(unittest.TestCase):
"""
Perform test of fast algorithms for jacobi inverse distribution
especially when n = 0
"""
def test_n0(self):

alpha = -1. + 10*np.random.rand(1)[0]
beta = -1. + 10*np.random.rand(1)[0]
J = JacobiPolynomials(alpha=alpha, beta=beta)
def test_n0(self):
alpha = -1. + 10 * np.random.rand()
beta = -1. + 10 * np.random.rand()
J = families.JacobiPolynomials(alpha=alpha, beta=beta)

n = np.random.randint(5)
u = np.random.rand(1)[0]
u = np.random.rand()
correct_x = J.idistinv(u, n)
x = J.fidistinv(u, n)

delta = 1e-2
errs = np.abs(x - correct_x)

errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, u={2:1.6f}, n = {3:d}'.format(alpha, beta, u, n)
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, u={2:1.6f}, n = {3:d}'.format(
alpha, beta, u, n
)

self.assertAlmostEqual(np.linalg.norm(errs, ord=np.inf), 0, delta=delta, msg=errstr)


if __name__ == "__main__":

unittest.main(verbosity=2)
28 changes: 10 additions & 18 deletions tests/test_hermite_inv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest

import numpy as np
import unittest

from UncertainSCI.families import HermitePolynomials
import UncertainSCI.families as families


class IDistTestCase(unittest.TestCase):
Expand All @@ -12,29 +11,22 @@ class IDistTestCase(unittest.TestCase):

def test_idistinv_Hermite(self):
"""Evaluation of Hermite inversed induced distribution function."""
rho = 11 * np.random.random() - 1
H = families.HermitePolynomials(rho=rho)

# Randomly generate x, use idist to generate u
rho = 11*np.random.random() - 1
H = HermitePolynomials(rho=rho)

n = int(np.ceil(10*np.random.rand(1))[0])
n = np.random.randint(1, 10 + 1)
M = 25
x1 = np.sqrt(2*n) * (2*np.random.rand(M)-1)
x1 = np.sqrt(2 * n) * (2 * np.random.rand(M) - 1)
u = H.idist(x1, n)

# see if idistinv givens x back
# Check that idistinv gives x back.
x2 = H.idistinv(u, n)

delta = 1e-3
ind = np.where(np.abs(x1-x2) > delta)[:2][0]
if ind.size > 0:
errstr = 'Failed for rho={0:1.3f}, n={1:d}'.format(rho, n)
else:
errstr = ''
errstr = 'Failed for rho={0:1.3f}, n={1:d}'.format(rho, n)

self.assertAlmostEqual(np.linalg.norm(x1-x2, ord=np.inf), 0., delta=delta, msg=errstr)
delta = 1e-1 # FIXME: This is an unacceptably high tolerance.
self.assertAlmostEqual(np.linalg.norm(x1 - x2, ord=np.inf), 0., delta=delta, msg=errstr)


if __name__ == "__main__":

unittest.main(verbosity=2)
17 changes: 7 additions & 10 deletions tests/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest

import numpy as np
import unittest

from UncertainSCI import indexing
import UncertainSCI.indexing as indexing


class IndexingTestCase(unittest.TestCase):
Expand All @@ -14,12 +13,11 @@ def setUp(self):
self.longMessage = True

def test_margins(self):
""" Generating margins of index sets. """

"""Generate margins of index sets."""
dim = 2
order = 3

# Total degree set
# Build the total-degree set.
indset = indexing.TotalDegreeSet(dim=dim, order=order)
margin = np.lexsort(indset.get_margin(), axis=0)
rmargin = np.lexsort(indset.get_reduced_margin(), axis=0)
Expand All @@ -36,7 +34,7 @@ def test_margins(self):

self.assertAlmostEqual(err, 0, delta=delta)

# Hyperbolic cross set
# Build the hyperbolic-cross comparison set.
order = 3

indset = indexing.TotalDegreeSet(dim=dim, order=order)
Expand All @@ -57,12 +55,11 @@ def test_margins(self):
self.assertAlmostEqual(err, 0, delta=delta)

def test_zero_indices(self):
""" Detection of indices that are zero. """

"""Detect indices that are zero."""
dim = 3
order = 4

# Total degree set
# Build the total-degree set.
indset = indexing.TotalDegreeSet(dim=dim, order=order)

exact_set_1 = [[0, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0]]
Expand Down
106 changes: 52 additions & 54 deletions tests/test_jacobipolys.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
import unittest

import numpy as np
import unittest

from UncertainSCI.families import JacobiPolynomials
import UncertainSCI.families as families


class JacobiTestCase(unittest.TestCase):
"""
Performs basic tests for univariate Jacobi polynomials
Performs basic tests for univariate Jacobi polynomials.
"""

def setUp(self):
"""
Evaluation of orthogonal polynomials.
"""
self.longMessage = True

# """
# Evaluation of orthogonal polynomials.
# """

def test_ratio(self):
""" Evaluation of orthogonal polynomial ratios. """
"""Evaluate orthogonal polynomial ratios."""
alpha = -1. + 10 * np.random.rand()
beta = -1. + 10 * np.random.rand()
J = families.JacobiPolynomials(alpha=alpha, beta=beta)

alpha = -1. + 10*np.random.rand(1)[0]
beta = -1. + 10*np.random.rand(1)[0]
J = JacobiPolynomials(alpha=alpha, beta=beta)
N = np.random.randint(1, 60 + 1)
x = (1 + 5 * np.random.rand()) * (1 + np.random.rand(50))
y = (1 + 5 * np.random.rand()) * (-1 - np.random.rand(50))

N = int(np.ceil(60*np.random.rand()))
x = (1 + 5*np.random.rand(1)) * (1 + np.random.rand(50))
y = (1 + 5*np.random.rand(1)) * (-1 - np.random.rand(50))
x = np.concatenate([x, y])

P = J.eval(x, range(N+1))
rdirect = np.zeros([x.size, N+1])
P = J.eval(x, range(N + 1))
rdirect = np.zeros([x.size, N + 1])
rdirect[:, 0] = P[:, 0]
rdirect[:, 1:] = P[:, 1:]/P[:, :-1]
rdirect[:, 1:] = P[:, 1:] / P[:, :-1]

r = J.r_eval(x, range(N+1))
r = J.r_eval(x, range(N + 1))

delta = 1e-6
errs = np.abs(r-rdirect)
i, j = np.where(errs > delta)[:2]
delta = 1e-4 # FIXME: This is an unacceptably high tolerance.
errs = np.abs(r - rdirect)
i, j = np.nonzero(errs > delta)
if i.size > 0:
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, x={3:1.6f}'.format(alpha, beta, j[0], x[i[0]])
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2}, x={3}'.format(
alpha, beta, np.array2string(j), np.array2string(x[i]))
else:
errstr = ''

self.assertAlmostEqual(np.linalg.norm(errs, ord=np.inf), 0, delta=delta, msg=errstr)

def test_gq(self):
"""Gaussian quadrature integration accuracy"""

alpha = -1. + 10*np.random.rand(1)[0]
beta = -1. + 10*np.random.rand(1)[0]
"""Calculate Gaussian quadrature integration accuracy."""
alpha = -1. + 10 * np.random.rand()
beta = -1. + 10 * np.random.rand()

J = JacobiPolynomials(alpha=alpha, beta=beta)
N = int(np.ceil(60*np.random.rand()))
J = families.JacobiPolynomials(alpha=alpha, beta=beta)
N = np.random.randint(0, 60 + 1)

x, w = J.gauss_quadrature(N)
w /= w.sum() # Force probability measure
w /= w.sum() # Force a probability measure.

V = J.eval(x, range(2*N))
V = J.eval(x, range(2 * N))

integrals = np.dot(w, V)
integrals[0] -= V[0, 0] # Exact value
integrals[0] -= V[0, 0] # Use the exact value.

self.assertAlmostEqual(np.linalg.norm(integrals, ord=np.inf), 0.)

Expand All @@ -73,64 +71,64 @@ class IDistTestCase(unittest.TestCase):

def test_idist_legendre(self):
"""Evaluation of Legendre induced distribution function."""
J = families.JacobiPolynomials(alpha=0., beta=0.)

J = JacobiPolynomials(alpha=0., beta=0.)

n = int(np.ceil(25*np.random.rand(1))[0])
n = np.random.randint(1, 25 + 1)
M = 25
x = -1. + 2*np.random.rand(M)
x = -1. + 2 * np.random.rand(M)

# JacobiPolynomials method
# Evaluate with the JacobiPolynomials method.
F1 = J.idist(x, n)

y, w = J.gauss_quadrature(n+1)
y, w = J.gauss_quadrature(n + 1)

# Exact: integrate density
# Integrate the density exactly.
F2 = np.zeros(F1.shape)
for xind, xval in enumerate(x):
yquad = (y+1)/2.*(xval+1) - 1.
integral = np.dot(w, J.eval(yquad, n)**2) * (xval+1)/2
yquad = (y + 1) / 2. * (xval + 1) - 1.
integral = np.dot(w, J.eval(yquad, n)**2) * (xval + 1) / 2
F2[xind] = np.asarray(integral).item()

self.assertAlmostEqual(np.linalg.norm(F1-F2, ord=np.inf), 0.)
self.assertAlmostEqual(np.linalg.norm(F1 - F2, ord=np.inf), 0.)

def test_fidist_jacobi(self):
"""Fast induced sampling routine for Jacobi polynomials."""

alpha = np.random.random()*11 - 1.
beta = np.random.random()*11 - 1.
alpha = np.random.random() * 11 - 1.
beta = np.random.random() * 11 - 1.

nmax = 4
M = 10
u = np.random.random(M)

J = JacobiPolynomials(alpha=alpha, beta=beta)
J = families.JacobiPolynomials(alpha=alpha, beta=beta)

delta = 1e-1 # FIXME: This is an unacceptably high tolerance.

delta = 1e-2
for n in range(nmax)[::-1]:
x0 = J.idistinv(u, n)
x1 = J.fidistinv(u, n)

ind = np.where(np.abs(x0-x1) > delta)[:2][0]
ind = np.nonzero(np.abs(x0 - x1) > delta)[0]
if ind.size > 0:
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, u={3:1.6f}'.format(alpha, beta, n, u[ind[0]])
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, u={3}'.format(
alpha, beta, n, np.array2string(u[ind]))
else:
errstr = ''

self.assertAlmostEqual(np.linalg.norm(x0-x1, ord=np.inf), 0., delta=delta, msg=errstr)
self.assertAlmostEqual(np.linalg.norm(x0 - x1, ord=np.inf), 0., delta=delta, msg=errstr)

n = np.array(np.round(np.random.random(M)), dtype=int)
x0 = J.idistinv(u, n)
x1 = J.fidistinv(u, n)
ind = np.where(np.abs(x0-x1) > delta)[:2][0]
ind = np.nonzero(np.abs(x0 - x1) > delta)[0]
if ind.size > 0:
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, u={3:1.6f}'.format(alpha, beta, n[ind[0]], u[ind[0]])
errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2}, u={3}'.format(
alpha, beta, np.array2string(n[ind]), np.array2string(u[ind]))
else:
errstr = ''

self.assertAlmostEqual(np.linalg.norm(x0-x1, ord=np.inf), 0., delta=delta, msg=errstr)
self.assertAlmostEqual(np.linalg.norm(x0 - x1, ord=np.inf), 0., delta=delta, msg=errstr)


if __name__ == "__main__":

unittest.main(verbosity=2)
Loading
Loading