Skip to content

Commit 663c3cf

Browse files
committed
feat: add 90 and 95 percentiles
1 parent 283e9c9 commit 663c3cf

File tree

4 files changed

+29
-78
lines changed

4 files changed

+29
-78
lines changed

experimental_env/analysis/analyze_summarizers/error_summarizer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ def calculate(self, results: list[ExperimentDescription]) -> tuple:
4444
mean = np.mean(errors)
4545
std = np.std(errors)
4646
median = np.median(errors)
47+
percentile_90 = np.percentile(errors, 90)
48+
percentile_95 = np.percentile(errors, 95)
4749

48-
return float(mean), float(std), float(median)
50+
return float(mean), float(std), float(median), float(percentile_90), float(percentile_95)
4951

5052
def analyze_method(self, results: list[ExperimentDescription], method: str):
51-
mean, deviation, median = self.calculate(results)
53+
mean, deviation, median, percentile_90, percentile_95 = self.calculate(results)
5254

5355
info_dict = {
5456
"mean": round_sig(mean, 3),
5557
"standart_deviation": round_sig(deviation, 3),
5658
"median": round_sig(median, 3),
59+
"percentile_90": round_sig(percentile_90, 3),
60+
"percentile_95": round_sig(percentile_95, 3),
5761
}
5862
yaml_path: Path = self._out_dir.joinpath("metric_info.yaml")
5963

@@ -67,16 +71,20 @@ def compare_methods(
6771
method_1: str,
6872
method_2: str,
6973
):
70-
mean_1, deviation_1, median_1 = self.calculate(results_1)
71-
mean_2, deviation_2, median_2 = self.calculate(results_2)
74+
mean_1, deviation_1, median_1, percentile_90_1, percentile_95_1 = self.calculate(results_1)
75+
mean_2, deviation_2, median_2, percentile_90_2, percentile_95_2 = self.calculate(results_2)
7276

7377
info_dict = {
7478
f"{method_1}_mean": round_sig(mean_1, 3),
7579
f"{method_1}_standart_deviation": round_sig(deviation_1, 3),
7680
f"{method_1}_median": round_sig(median_1, 3),
81+
f"{method_1}_percentile_90": round_sig(percentile_90_1, 3),
82+
f"{method_1}_percentile_95": round_sig(percentile_95_1, 3),
7783
f"{method_2}_mean": round_sig(mean_2, 3),
7884
f"{method_2}_standart_deviation": round_sig(deviation_2, 3),
7985
f"{method_2}_median": round_sig(median_2, 3),
86+
f"{method_2}_percentile_90": round_sig(percentile_90_2, 3),
87+
f"{method_2}_percentile_95": round_sig(percentile_95_2, 3),
8088
}
8189
yaml_path: Path = self._out_dir.joinpath("metric_info.yaml")
8290

script_stage_1.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,24 @@
1-
from pathlib import Path
1+
"""The script implements the first step of the experiment"""
22

3-
import numpy as np
3+
from pathlib import Path
44

55
from experimental_env.preparation.dataset_generator import (
6-
ConcreteDatasetGenerator,
76
RandomDatasetGenerator,
87
)
98
from mpest.models import ExponentialModel, GaussianModel, WeibullModelExp
109

11-
WORKING_DIR = Path("/home/danil/PycharmProjects/Projects/EM-algo-DT/experiment/stage_1")
10+
WORKING_DIR = Path(dir_stage_1)
1211
SAMPLES_SIZE = 1000
1312

14-
np.random.seed(42)
15-
16-
r_generator = RandomDatasetGenerator()
13+
r_generator = RandomDatasetGenerator(42)
1714
mixtures = [
1815
[ExponentialModel],
1916
[GaussianModel],
2017
[WeibullModelExp],
2118
[WeibullModelExp, GaussianModel],
2219
[ExponentialModel, GaussianModel],
2320
[WeibullModelExp, WeibullModelExp],
24-
[ExponentialModel, ExponentialModel]
21+
[ExponentialModel, ExponentialModel],
2522
]
2623
for models in mixtures:
2724
r_generator.generate(SAMPLES_SIZE, models, Path(WORKING_DIR), exp_count=100)
28-
29-
c_generator2 = ConcreteDatasetGenerator()
30-
models = [ExponentialModel]
31-
c_generator2.add_distribution(models[0], [1.0], 1.0)
32-
c_generator2.generate(SAMPLES_SIZE, Path(WORKING_DIR), 5)
33-
34-
c_generator3 = ConcreteDatasetGenerator()
35-
models = [GaussianModel]
36-
c_generator3.add_distribution(models[0], [0, 1.0], 1.0)
37-
c_generator3.generate(SAMPLES_SIZE, Path(WORKING_DIR), 5)
38-
39-
c_generator4 = ConcreteDatasetGenerator()
40-
models = [WeibullModelExp]
41-
c_generator4.add_distribution(models[0], [1.0, 1.0], 1.0)
42-
c_generator4.generate(SAMPLES_SIZE, Path(WORKING_DIR), 5)
43-
44-
c_generator5 = ConcreteDatasetGenerator()
45-
models = [WeibullModelExp]
46-
c_generator5.add_distribution(models[0], [1.0, 1.0], 1.0)
47-
c_generator5.generate(SAMPLES_SIZE, Path(WORKING_DIR), 5)
48-
49-
c_generator6 = ConcreteDatasetGenerator()
50-
models = [WeibullModelExp]
51-
c_generator6.add_distribution(models[0], [1.0, 0.5], 1.0)
52-
c_generator6.generate(SAMPLES_SIZE, Path(WORKING_DIR), 5)
53-
54-
c_generator7 = ConcreteDatasetGenerator()
55-
models = [GaussianModel, GaussianModel]
56-
c_generator7.add_distribution(models[0], [-1.0, 2.5], 0.3)
57-
c_generator7.add_distribution(models[1], [1.0, 0.5], 0.7)
58-
c_generator7.generate(SAMPLES_SIZE, Path(WORKING_DIR), 10)
59-
60-
c_generator8 = ConcreteDatasetGenerator()
61-
models = [GaussianModel, GaussianModel]
62-
c_generator8.add_distribution(models[0], [0.0, 1.5], 0.6)
63-
c_generator8.add_distribution(models[1], [1.0, 1.0], 0.4)
64-
c_generator8.generate(SAMPLES_SIZE, Path(WORKING_DIR), 10)

script_stage_2.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import random
2-
from pathlib import Path
1+
"""The script implements the second step of the experiment"""
32

4-
import numpy as np
3+
from pathlib import Path
54

65
from experimental_env.experiment.estimators import (
76
LikelihoodEstimator,
@@ -10,27 +9,22 @@
109
from experimental_env.experiment.experiment_executors.random_executor import (
1110
RandomExperimentExecutor,
1211
)
13-
from experimental_env.experiment.experiment_executors.standart_executor import (
14-
StandartExperimentExecutor,
15-
)
1612
from experimental_env.preparation.dataset_parser import SamplesDatasetParser
1713
from mpest.em.breakpointers import StepCountBreakpointer
1814
from mpest.em.distribution_checkers import (
1915
FiniteChecker,
2016
PriorProbabilityThresholdChecker,
2117
)
2218

23-
SOURCE_DIR = Path("/home/danil/PycharmProjects/Projects/EM-algo-DT/experiment/stage_1")
24-
WORKING_DIR = Path("/home/danil/PycharmProjects/Projects/EM-algo-DT/experiment/stage_2")
25-
26-
random.seed(42)
19+
SOURCE_DIR = Path(dir_stage_1)
20+
WORKING_DIR = Path(dir_stage_2)
2721

2822
# Parse stage 1
2923
parser = SamplesDatasetParser()
3024
datasets = parser.parse(SOURCE_DIR)
3125

3226
# Execute stage 2
33-
executor = RandomExperimentExecutor(WORKING_DIR, 5)
27+
executor = RandomExperimentExecutor(WORKING_DIR, 5, 43)
3428
executor.execute(
3529
datasets,
3630
LMomentsEstimator(
@@ -39,7 +33,7 @@
3933
),
4034
)
4135

42-
executor = RandomExperimentExecutor(WORKING_DIR, 5)
36+
executor = RandomExperimentExecutor(WORKING_DIR, 5, 43)
4337
executor.execute(
4438
datasets,
4539
LikelihoodEstimator(

script_stage_3.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""The script implements the third step of the experiment"""
2+
13
from pathlib import Path
24

35
from experimental_env.analysis.analysis import Analysis
@@ -13,28 +15,15 @@
1315
from experimental_env.analysis.metrics import SquaredError
1416
from experimental_env.experiment.experiment_parser import ExperimentParser
1517

16-
EXPERIMENT_DIR = "experiment"
17-
WORKING_DIR = Path(
18-
f"/home/danil/PycharmProjects/Projects/EM-algo-DT/{EXPERIMENT_DIR}/stage_3"
19-
)
20-
18+
WORKING_DIR = Path(dir_stage_2)
2119

22-
# Compare results
23-
LMOMENTS_DIR = Path(
24-
f"/home/danil/PycharmProjects/Projects/EM-algo-DT/{EXPERIMENT_DIR}/stage_2/LM-EM"
25-
)
26-
LIKELIHOOD_DIR = Path(
27-
f"/home/danil/PycharmProjects/Projects/EM-algo-DT/{EXPERIMENT_DIR}/stage_2/MLE-EM"
28-
)
20+
LIKELIHOOD_DIR = Path(dir_EM_results)
21+
LMOMENTS_DIR = Path(dir_ELM_results)
2922

3023
results_1 = ExperimentParser().parse(LMOMENTS_DIR)
3124
results_2 = ExperimentParser().parse(LIKELIHOOD_DIR)
3225

3326
analyze_actions = [DensityPlot(), TimePlot(), ErrorConvergence(SquaredError())]
3427
analyze_summarizers = [ErrorSummarizer(SquaredError()), TimeSummarizer()]
3528

36-
Analysis(WORKING_DIR, analyze_actions, analyze_summarizers).analyze(results_1, "LM-EM")
37-
Analysis(WORKING_DIR, analyze_actions, analyze_summarizers).analyze(results_2, "MLE-EM")
38-
Analysis(WORKING_DIR, analyze_actions, analyze_summarizers).compare(
39-
results_1, results_2, "LM-EM", "MLE-EM"
40-
)
29+
Analysis(WORKING_DIR, analyze_actions, analyze_summarizers).compare(results_1, results_2, "ELM", "MLE-EM")

0 commit comments

Comments
 (0)