From cf5060c218d3a77c2cf1c9d8d0b6e7d460ca252c Mon Sep 17 00:00:00 2001 From: Carlos-Alegre Date: Wed, 8 Jan 2025 10:23:08 +0100 Subject: [PATCH 1/2] Modified CDF expectation and added test --- src/GridCalEngine/basic_structures.py | 5 +++-- src/tests/test_CDF.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_CDF.py diff --git a/src/GridCalEngine/basic_structures.py b/src/GridCalEngine/basic_structures.py index c94784a1e..8e34c7d57 100644 --- a/src/GridCalEngine/basic_structures.py +++ b/src/GridCalEngine/basic_structures.py @@ -141,10 +141,11 @@ def expectation(self) -> float | complex: Returns the CDF expected value (AKA the mean) :return: expectation """ + n = len(self.arr) if self.iscomplex: - return np.sum(self.arr * self.prob) + return np.sum(self.arr) * (1 / n) else: - return np.sum(self.arr * self.prob) + return np.sum(self.arr) * (1 / n) def plot(self, plt, LINEWIDTH: int, ax=None): """ diff --git a/src/tests/test_CDF.py b/src/tests/test_CDF.py new file mode 100644 index 000000000..3315a65b0 --- /dev/null +++ b/src/tests/test_CDF.py @@ -0,0 +1,10 @@ +import numpy as np +from numpy import random +import GridCalEngine as gce + +def test_CDF_expectation(): + + x = random.normal(4, 1, size=20) + + assert np.allclose(x.mean(), gce.CDF(x).expectation(), atol=1e-5) + From 59e84462f4e0758535a32503b53f8f634daa8ee2 Mon Sep 17 00:00:00 2001 From: santi Date: Wed, 8 Jan 2025 09:26:37 +0000 Subject: [PATCH 2/2] Small modifications to add randomness to the CDF test and to simplify the cdf.expectation code --- .idea/workspace.xml | 72 ++++++++----------- .../pf_generalized_formulation.py | 20 +++--- src/GridCalEngine/basic_structures.py | 5 +- src/tests/test_CDF.py | 3 + 4 files changed, 43 insertions(+), 57 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1cf911e21..ed83cb2d1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -28,9 +28,9 @@