Skip to content
Snippets Groups Projects
Commit f58babba authored by Helen Schottenhamml's avatar Helen Schottenhamml
Browse files

Fix flake warnings.

parent 09e7f670
1 merge request!175Welford algorithm extension
Pipeline #68741 passed with stages
in 37 minutes and 27 seconds
...@@ -14,8 +14,8 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub ...@@ -14,8 +14,8 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub
the sum of squares / sum of cubes is given. the sum of squares / sum of cubes is given.
The mean value is directly updated in the mean vector field. The mean value is directly updated in the mean vector field.
The variance / covariance must be retrieved in a post-processing step. Let :math `M_{2,n}` denote the value of the sum of The variance / covariance must be retrieved in a post-processing step. Let :math `M_{2,n}` denote the value of the
squares after the first :math `n` samples. According to Welford the biased sample variance sum of squares after the first :math `n` samples. According to Welford the biased sample variance
:math `\sigma_n^2` and the unbiased sample variance :math `s_n^2` are given by :math `\sigma_n^2` and the unbiased sample variance :math `s_n^2` are given by
.. math :: .. math ::
...@@ -78,7 +78,7 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub ...@@ -78,7 +78,7 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub
if welford_sum_of_cubes_field is not None: if welford_sum_of_cubes_field is not None:
assert welford_sum_of_squares_field is not None assert welford_sum_of_squares_field is not None
### actual assignments # actual assignments
counter = sp.Symbol('counter') counter = sp.Symbol('counter')
delta = sp.symbols(f"delta_:{dim}") delta = sp.symbols(f"delta_:{dim}")
...@@ -108,11 +108,12 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub ...@@ -108,11 +108,12 @@ def welford_assignments(field, mean_field, sum_of_squares_field=None, sum_of_cub
for k in range(dim): for k in range(dim):
idx = (i * dim + j) * dim + k idx = (i * dim + j) * dim + k
main_assignments.append(ps.Assignment( main_assignments.append(ps.Assignment(
welford_sum_of_cubes_field.at_index(idx), welford_sum_of_cubes_field.at_index(idx) welford_sum_of_cubes_field.at_index(idx),
- delta[k] / counter * welford_sum_of_squares_field(i * dim + j) welford_sum_of_cubes_field.at_index(idx)
- delta[j] / counter * welford_sum_of_squares_field(k * dim + i) - delta[k] / counter * welford_sum_of_squares_field(i * dim + j)
- delta[i] / counter * welford_sum_of_squares_field(j * dim + k) - delta[j] / counter * welford_sum_of_squares_field(k * dim + i)
+ delta2[i] * (2 * delta[j] - delta2[j]) * delta[k] - delta[i] / counter * welford_sum_of_squares_field(j * dim + k)
+ delta2[i] * (2 * delta[j] - delta2[j]) * delta[k]
)) ))
return main_assignments return main_assignments
...@@ -40,7 +40,7 @@ def test_welford(order, dim): ...@@ -40,7 +40,7 @@ def test_welford(order, dim):
# set random seed # set random seed
np.random.seed(42) np.random.seed(42)
n = int(1e4) n = int(1e4)
x = np.random.normal(size=n*dim).reshape(n, dim) x = np.random.normal(size=n * dim).reshape(n, dim)
analytical_mean = np.zeros(dim) analytical_mean = np.zeros(dim)
analytical_covariance = np.zeros(dim**2) analytical_covariance = np.zeros(dim**2)
...@@ -53,13 +53,17 @@ def test_welford(order, dim): ...@@ -53,13 +53,17 @@ def test_welford(order, dim):
# calculate analytical covariances # calculate analytical covariances
for i in range(dim): for i in range(dim):
for j in range(dim): for j in range(dim):
analytical_covariance[i * dim + j] = (np.sum((x[:, i] - analytical_mean[i]) * (x[:, j] - analytical_mean[j]))) / n analytical_covariance[i * dim + j] \
= (np.sum((x[:, i] - analytical_mean[i]) * (x[:, j] - analytical_mean[j]))) / n
# calculate analytical third-order central moments # calculate analytical third-order central moments
for i in range(dim): for i in range(dim):
for j in range(dim): for j in range(dim):
for k in range(dim): for k in range(dim):
analytical_third_order_moments[(i * dim + j) * dim + k] = (np.sum((x[:, i] - analytical_mean[i]) * (x[:, j] - analytical_mean[j]) * (x[:, k] - analytical_mean[k]))) / n analytical_third_order_moments[(i * dim + j) * dim + k] \
= (np.sum((x[:, i] - analytical_mean[i])
* (x[:, j] - analytical_mean[j])
* (x[:, k] - analytical_mean[k]))) / n
# Time loop # Time loop
counter = 1 counter = 1
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment