Skip to content

Commit 2228cab

Browse files
committed
Added centered_l2_discrepancy
1 parent 4f6d1a5 commit 2228cab

19 files changed

+993
-34
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/other.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 443 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.DS_Store

-2 KB
Binary file not shown.

pysample/__init__.py

Whitespace-only changes.

pysample/indicators.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from pysample.util import cdist
2+
3+
import numpy as np
4+
5+
6+
def minimum_distance(X):
7+
D = cdist(X, X)
8+
D = D + 1e12 * np.eye(X.shape[0])
9+
return - np.min(D)
10+
11+
12+
def correlation(X):
13+
M = np.corrcoef(X.T, rowvar=True)
14+
return np.sum(np.tril(M, -1) ** 2)
15+
16+
17+
def centered_l2_discrepancy_slow(X, check_bounds=True):
18+
if check_bounds and (np.any(np.min(X) < 0 or np.max(X) > 1)):
19+
raise Exception("Make sure that all X are between 0 and 1.")
20+
21+
n_points, n_dim = X.shape
22+
23+
acmh = np.abs(X - 0.5)
24+
25+
val = 0
26+
27+
for k in range(n_points):
28+
temp = 1
29+
for i in range(n_dim):
30+
temp = temp * (1 + 0.5 * (acmh[:, i] + acmh[k, i] - np.abs(X[:, i] - X[k, i])))
31+
val += np.sum(temp)
32+
33+
val = ((13 / 12) ** n_dim - 2 / n_points * np.sum(
34+
np.prod(1 + 0.5 * (acmh - acmh ** 2), axis=1)) + val / n_points ** 2) ** 0.5
35+
36+
return val
37+
38+
39+
def centered_l2_discrepancy(X, check_bounds=True):
40+
if check_bounds and (np.any(np.min(X) < 0 or np.max(X) > 1)):
41+
raise Exception("Make sure that all X are between 0 and 1.")
42+
43+
n_points, n_dim = X.shape
44+
45+
acmh = np.abs(X - 0.5)
46+
47+
_X = np.abs(X.T[..., None] - X.T[:, None, :])
48+
_acmh = np.abs(acmh.T[..., None] + acmh.T[:, None, :])
49+
50+
val = np.sum(np.prod((1 + 0.5 * (_acmh - _X)), axis=0))
51+
val = ((13 / 12) ** n_dim - 2 / n_points * np.sum(
52+
np.prod(1 + 0.5 * (acmh - acmh ** 2), axis=1)) + val / n_points ** 2) ** 0.5
53+
54+
return val

pysample/methods/__init__.py

Whitespace-only changes.

pysample/methods/lhs.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import numpy as np
2+
3+
from pysample.methods.sampling import Sampling
4+
from pysample.indicators import minimum_distance, correlation
5+
from pysample.optimization import random_search
6+
7+
8+
class LatinHypercubeSampling(Sampling):
9+
10+
def __init__(self, criterion="maxmin", optimizer="random_search", iterations=1000) -> None:
11+
super().__init__()
12+
self.criterion = criterion
13+
self.iterations = iterations
14+
self.optimizer = optimizer
15+
16+
if criterion == "maxmin":
17+
self.fun_obj = minimum_distance
18+
elif criterion == "correlation":
19+
self.fun_obj = correlation
20+
else:
21+
raise Exception("Criterion not known.")
22+
23+
def _sample(self, n_samples, n_dim):
24+
25+
if self.optimizer == "random_trials":
26+
fun_sample = lambda: _sample(n_samples, n_dim)
27+
X = random_search(fun_sample, self.fun_obj, self.iterations)
28+
29+
elif self.optimizer == "simulated_annealing":
30+
31+
pass
32+
33+
elif self.optimizer == "ga":
34+
35+
n_generations = 100
36+
pop_size = 20
37+
38+
39+
40+
41+
else:
42+
X = _sample(n_samples, n_dim)
43+
44+
return X
45+
46+
47+
def _sample(n_samples, n_var, smooth=True):
48+
X = np.random.random(size=(n_samples, n_var))
49+
val = X.argsort(axis=0) + 1
50+
51+
if smooth:
52+
val = val - np.random.random(val.shape)
53+
else:
54+
val = val - 0.5
55+
val /= n_samples
56+
return val
57+
58+

pysample/methods/random.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
from pysample.methods.sampling import Sampling
4+
5+
6+
class RandomSampling(Sampling):
7+
8+
def _sample(self, n_samples, n_dim):
9+
return np.random.random((n_samples, n_dim))

pysample/methods/sampling.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Sampling:
2+
3+
def sample(self, n_samples, n_dim):
4+
return self._sample(n_samples, n_dim)
5+
6+
def _sample(self, n_samples, n_dim):
7+
pass

0 commit comments

Comments
 (0)