-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from MeasureTransport/282-construct-triangula…
…rmap-with-coefficients-from-its-given-components Adding KeepCoeffs option to TriangularMap and ComposedMap
- Loading branch information
Showing
14 changed files
with
1,131 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Test methods of ComposedMap object | ||
import mpart | ||
import numpy as np | ||
print("made map") | ||
opts = mpart.MapOptions() | ||
|
||
num_maps = 4 | ||
dim = 2 | ||
maxOrder = 1 | ||
|
||
maps = [] | ||
coeffs_ = [] | ||
numCoeffs = 0 | ||
for i in range(num_maps): | ||
map = mpart.CreateTriangular(dim,dim,maxOrder,opts) | ||
coeffs = np.random.randn(map.numCoeffs) | ||
map.SetCoeffs(coeffs) | ||
maps.append(map) | ||
coeffs_.append(coeffs) | ||
numCoeffs += map.numCoeffs | ||
|
||
|
||
moveCoeffs = True | ||
composed_map = mpart.ComposedMap(maps, moveCoeffs, num_maps) | ||
all_coeffs = np.hstack(coeffs_).flatten() | ||
|
||
num_samples = 100 | ||
x = np.random.randn(dim, num_samples) | ||
|
||
def test_numCoeffs(): | ||
assert composed_map.numCoeffs == numCoeffs | ||
|
||
def test_CoeffsMap(): | ||
assert np.all(composed_map.CoeffMap() == all_coeffs) | ||
|
||
|
||
def test_Evaluate(): | ||
assert composed_map.Evaluate(x).shape == (dim, num_samples) | ||
x_ = x.copy() | ||
for map_ in maps: | ||
x_ = map_.Evaluate(x_) | ||
assert np.all(composed_map.Evaluate(x) == x_) | ||
|
||
|
||
def test_LogDeterminant(): | ||
assert composed_map.LogDeterminant(x).shape == (num_samples,) | ||
|
||
def test_Inverse(): | ||
|
||
y = composed_map.Evaluate(x) | ||
x_ = composed_map.Inverse(np.zeros((1,num_samples)),y) | ||
assert np.allclose(x_, x, atol=1E-3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Test methods of TriangularMap object | ||
import mpart | ||
import numpy as np | ||
|
||
opts = mpart.MapOptions() | ||
|
||
multis_1 = np.array([[0],[1]]) # linear | ||
multis_2 = np.array([[0,0],[0,1],[2,0]]) # quadratic in x_1, linear in x_2, matches form of target | ||
|
||
mset_1 = mpart.MultiIndexSet(multis_1).fix(True) | ||
mset_2 = mpart.MultiIndexSet(multis_2).fix(True) | ||
|
||
map_1 = mpart.CreateComponent(mset_1, opts) | ||
coeffs_1 = np.random.randn(map_1.numCoeffs) | ||
map_1.SetCoeffs(coeffs_1) | ||
|
||
map_2 = mpart.CreateComponent(mset_2, opts) | ||
coeffs_2 = np.random.randn(map_2.numCoeffs) | ||
map_2.SetCoeffs(coeffs_2) | ||
|
||
triangular = mpart.TriangularMap([map_1,map_2], moveCoeffs=True) | ||
num_samples = 100 | ||
x = np.random.randn(2,num_samples) | ||
|
||
|
||
def test_numCoeffs(): | ||
assert triangular.numCoeffs == 2 + 3 | ||
|
||
|
||
def test_Evaluate(): | ||
assert triangular.Evaluate(x).shape == (2,num_samples) | ||
|
||
|
||
def test_LogDeterminant(): | ||
assert triangular.LogDeterminant(x).shape == (num_samples,) | ||
|
||
def test_Inverse(): | ||
# coeffs = np.random.randn(triangular.numCoeffs) | ||
# triangular.SetCoeffs(coeffs) | ||
y = triangular.Evaluate(x) | ||
x_ = triangular.Inverse(np.zeros((1,num_samples)),y) | ||
assert np.allclose(x_, x, atol=1E-3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.