Skip to content

Experiments and statistics

An experiment runs several algorithms on several problems, many times each, and compares them with tables, statistical tests and figures. ypea.Experiment runs and stores the runs, and ypea.Report analyzes them.

Statistical tests need SciPy (pip install "ypea[stats]") and figures need matplotlib (pip install "ypea[plot]"). Without SciPy, the tables still have the statistics and the Friedman ranks.

Running an experiment

This example compares DE, PSO and L-SHADE on three CEC 2017 functions in 10 dimensions (see Benchmarks), with 5 runs of 3000 evaluations each. An evaluation is one call of the objective function on one solution. The runs are saved in the folder results/cec2017:

import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5, 11])
algorithms = {
    "DE": ypea.DE(),
    "PSO": ypea.PSO(),
    "L-SHADE": ypea.LSHADE(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=5,
    max_nfe=3_000,
    seed=2026,
    output="results/cec2017",
)
exp.run()

Differs from Python: algorithms go in braces, with their labels in labels=.

problems = ypea.benchmarks.cec2017(10, functions=[1 5 11]);
algorithms = {
    ypea.DE()
    ypea.PSO()
    ypea.LSHADE()
    };
labels = ["DE", "PSO", "L-SHADE"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=5, ...
    max_nfe=3000, ...
    seed=2026, ...
    output="results/cec2017");
exp.run();
Argument Meaning
algorithms a dict from label to algorithm, or a list (labeled by their short names)
problems a list of problems with unique names
runs runs of each algorithm on each problem (30 by default)
max_nfe, max_iter the budget of every run. If either is given, it replaces the limits of every algorithm (on a copy).
seed the base seed
seeds "numpy" (default) or "studio", see below
n_jobs worker processes. The results are the same for any value.
output a folder for the run files, or None to keep the runs in memory
checkpoints fractions of max_nfe at which the best value and the error are recorded. The default is the CEC 2017 rule: 0.01, 0.02, 0.03, 0.05, 0.1, 0.2, ..., 1.0.

With n_jobs > 1 the problems must be picklable. Every ready-made problem is, and your own objectives must be top-level functions, not lambdas. On Windows and macOS, put the script under if __name__ == "__main__":.

Seeds

Each run has its own seed, so a result does not depend on n_jobs or on which algorithms and problems are included:

  • seeds="numpy": every run gets an independent random stream, derived from the seed, the label, the problem name and the run number.
  • seeds="studio": run i uses the Studio generator with the seed ypea.derive_seed(seed, i), so run i gets the same seed for every algorithm and problem.

Resuming

The output folder holds metadata.json and one file per run, runs/<label>/<problem>/run_<i>.json. run() skips every run already stored with the same settings, problem, budget and seeds. So an interrupted experiment continues where it stopped, added runs or problems run alone, and a run whose settings changed runs again. After the first example, this call runs only the sixth run of each algorithm and problem:

import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5, 11])
algorithms = {
    "DE": ypea.DE(),
    "PSO": ypea.PSO(),
    "L-SHADE": ypea.LSHADE(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=6,
    max_nfe=3_000,
    seed=2026,
    output="results/cec2017",
)
exp.run()  # runs 0 to 4 are read from the folder, run 5 is new
problems = ypea.benchmarks.cec2017(10, functions=[1 5 11]);
algorithms = {
    ypea.DE()
    ypea.PSO()
    ypea.LSHADE()
    };
labels = ["DE", "PSO", "L-SHADE"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=6, ...
    max_nfe=3000, ...
    seed=2026, ...
    output="results/cec2017");
exp.run();  % runs 0 to 4 are read from the folder, run 5 is new

Reports

exp.report() gives a Report of the runs, and ypea.Report.load(folder) reads the same report later from the output folder. report.table() compares the algorithms. This example repeats the experiment of the first example (the stored runs are read, not run again) and prints its table:

import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5, 11])
algorithms = {
    "DE": ypea.DE(),
    "PSO": ypea.PSO(),
    "L-SHADE": ypea.LSHADE(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=5,
    max_nfe=3_000,
    seed=2026,
    output="results/cec2017",
)
exp.run()

report = ypea.Report.load("results/cec2017")
report.zero_below = 1e-8  # the CEC rule: errors below 1e-8 count as 0
table = report.table(stat="mean", baseline="L-SHADE")
print(table)
| Problem | Statistic | DE | PSO | L-SHADE |
|:---|:---|---:|---:|---:|
| cec2017_f1_d10 | Mean | 6.765e+08 − | 2.399e+07 − | **1.344e+07** |
|  | Std | 1.487e+08 | 6.998e+06 | 5.013e+06 |
|  | Best | 5.494e+08 | 1.513e+07 | **6.283e+06** |
|  | Worst | 9.195e+08 | 3.380e+07 | **1.988e+07** |
|  | Median | 6.469e+08 | 2.494e+07 | **1.480e+07** |
| cec2017_f5_d10 | Mean | 6.063e+01 − | 4.160e+01 = | **2.816e+01** |
|  | Std | 4.985e+00 | 9.776e+00 | 1.081e+01 |
|  | Best | 5.570e+01 | 3.198e+01 | **1.229e+01** |
|  | Worst | 6.706e+01 | 5.712e+01 | **4.158e+01** |
|  | Median | 5.916e+01 | 3.804e+01 | **3.035e+01** |
| cec2017_f11_d10 | Mean | 1.137e+02 − | 7.046e+01 = | **3.391e+01** |
|  | Std | 7.575e+00 | 8.080e+01 | 6.973e+00 |
|  | Best | 1.037e+02 | **2.388e+01** | 2.904e+01 |
|  | Worst | 1.240e+02 | 2.138e+02 | **4.532e+01** |
|  | Median | 1.125e+02 | 3.940e+01 | **2.981e+01** |
| +/=/− | vs L-SHADE | 0/0/3 | 0/2/1 | control |
| Friedman | Avg. rank | 3.00 | 2.00 | **1.00** |
|  | Holm APV | 0.0286 | 0.221 | control |

Wilcoxon rank sum test against L-SHADE at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean error over 3 problems: chi2 = 6, p = 0.0498. Post hoc z test against L-SHADE with Holm adjusted p values (Derrac et al. 2011).

Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's.

problems = ypea.benchmarks.cec2017(10, functions=[1 5 11]);
algorithms = {
    ypea.DE()
    ypea.PSO()
    ypea.LSHADE()
    };
labels = ["DE", "PSO", "L-SHADE"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=5, ...
    max_nfe=3000, ...
    seed=2026, ...
    output="results/cec2017");
exp.run();

report = ypea.experiment.Report.load("results/cec2017");
report.zero_below = 1e-8;  % the CEC rule: errors below 1e-8 count as 0
table = report.table(stat="mean", baseline="L-SHADE");
disp(table)
| Problem | Statistic | DE | PSO | L-SHADE |
|:---|:---|---:|---:|---:|
| cec2017_f1_d10 | Mean | 5.971e+08 − | 1.886e+07 − | **7.861e+06** |
|  | Std | 1.645e+08 | 7.106e+06 | 3.502e+06 |
|  | Best | 3.226e+08 | 1.043e+07 | **3.972e+06** |
|  | Worst | 7.396e+08 | 2.886e+07 | **1.172e+07** |
|  | Median | 6.366e+08 | 1.618e+07 | **8.768e+06** |
| cec2017_f5_d10 | Mean | 5.842e+01 − | 4.260e+01 = | **3.362e+01** |
|  | Std | 1.094e+01 | 1.246e+01 | 6.718e+00 |
|  | Best | 4.285e+01 | **2.542e+01** | 2.715e+01 |
|  | Worst | 6.926e+01 | 5.626e+01 | **4.350e+01** |
|  | Median | 5.650e+01 | 4.789e+01 | **3.375e+01** |
| cec2017_f11_d10 | Mean | 1.140e+02 − | 5.664e+01 − | **2.908e+01** |
|  | Std | 1.483e+01 | 2.522e+01 | 8.400e+00 |
|  | Best | 9.926e+01 | 3.164e+01 | **1.899e+01** |
|  | Worst | 1.343e+02 | 9.777e+01 | **3.728e+01** |
|  | Median | 1.106e+02 | 4.669e+01 | **3.156e+01** |
| +/=/− | vs L-SHADE | 0/0/3 | 0/1/2 | control |
| Friedman | Avg. rank | 3.00 | 2.00 | **1.00** |
|  | Holm APV | 0.0286 | 0.221 | control |

Wilcoxon rank sum test against L-SHADE at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean error over 3 problems: chi2 = 6, p = 0.0498. Post hoc z test against L-SHADE with Holm adjusted p values (Derrac et al. 2011).

The table is Markdown text. For every problem, it has the mean, standard deviation, best, worst and median of the final error over the runs, with the best of each row in bold. The marks after the means and the last three rows come from the statistical tests explained below. Here L-SHADE has the lowest mean error on all three problems, so it ranks first. This tiny example only shows the format: a real study needs more runs and the full budget.

The metric is the error abs(f - optimum) by default (metric="error"), or the objective value on problems without a known optimum. metric="obj_value" always uses the objective value.

Statistical tests

Random algorithms give a different result in every run, so a lower mean can be luck. The tests check whether a difference is larger than chance would explain. They follow Derrac, Garcia, Molina and Herrera (2011):

  • Wilcoxon rank sum test of each algorithm against the control on each problem, at alpha=0.05. It compares the final values of the runs of two algorithms. + means significantly better than the control, = no significant difference, − significantly worse, and the +/=/− row counts them. The control is baseline, or else the algorithm with the best Friedman rank. test=None turns the test off.
  • Friedman test over the problems on the chosen statistic (stat, the mean by default). On each problem the algorithms are ranked 1, 2, 3 and so on, and the Friedman rank of an algorithm is its average rank over the problems (lower is better). The test gives a p value (the notes also give the Iman and Davenport version when it is defined): a small p value means the algorithms really differ.
  • Post hoc test: each algorithm against the control, with Holm adjusted p values (APV). An APV below alpha means the algorithm differs significantly from the control over all problems.

table.data holds the numbers, for example table.data["friedman"]["ranks"]. report.values(label, problem) gives the final values of the runs, and report.curves(label, problem) the best value of every run along the evaluations.

Exporting tables

Each table can be saved as CSV, Markdown, LaTeX or HTML:

import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5, 11])
algorithms = {
    "DE": ypea.DE(),
    "PSO": ypea.PSO(),
    "L-SHADE": ypea.LSHADE(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=5,
    max_nfe=3_000,
    seed=2026,
    output="results/cec2017",
)
exp.run()
report = exp.report()
table = report.table(baseline="L-SHADE")

table.to_csv("results/cec2017/table.csv")
table.to_markdown("results/cec2017/table.md")
table.to_latex(
    "results/cec2017/table.tex",
    caption="Mean error on CEC 2017, D = 10",
    label="tab:cec",
)
table.to_html("results/cec2017/table.html")
problems = ypea.benchmarks.cec2017(10, functions=[1 5 11]);
algorithms = {
    ypea.DE()
    ypea.PSO()
    ypea.LSHADE()
    };
labels = ["DE", "PSO", "L-SHADE"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=5, ...
    max_nfe=3000, ...
    seed=2026, ...
    output="results/cec2017");
exp.run();
report = exp.report();
table = report.table(baseline="L-SHADE");

table.to_csv("results/cec2017/table.csv");
table.to_markdown("results/cec2017/table.md");
table.to_latex("results/cec2017/table.tex", ...
    caption="Mean error on CEC 2017, D = 10", ...
    label="tab:cec");
table.to_html("results/cec2017/table.html");

Each method also returns the text. The LaTeX table needs \usepackage{booktabs}, and to_dataframe() gives a pandas DataFrame if pandas is installed.

Figures and the HTML report

The report draws three figures: the convergence of each algorithm on one problem, a box plot of the final errors, and the Friedman ranks. save_html writes one self-contained page with the settings, the table and every figure:

import matplotlib.pyplot as plt
import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5, 11])
algorithms = {
    "DE": ypea.DE(),
    "PSO": ypea.PSO(),
    "L-SHADE": ypea.LSHADE(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=5,
    max_nfe=3_000,
    seed=2026,
    output="results/cec2017",
)
exp.run()
report = exp.report()

ax = report.plot_convergence("cec2017_f5_d10")  # median and 25 to 75 percent band
ax.figure.savefig("results/cec2017/convergence_f5.pdf")
plt.close(ax.figure)

ax = report.plot_box("cec2017_f5_d10")  # final errors
ax = report.plot_ranks()  # Friedman ranks
plt.close("all")

path = report.save_html()  # results/cec2017/report.html

Differs from Python: figures are saved with exportgraphics.

problems = ypea.benchmarks.cec2017(10, functions=[1 5 11]);
algorithms = {
    ypea.DE()
    ypea.PSO()
    ypea.LSHADE()
    };
labels = ["DE", "PSO", "L-SHADE"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=5, ...
    max_nfe=3000, ...
    seed=2026, ...
    output="results/cec2017");
exp.run();
report = exp.report();

ax = report.plot_convergence("cec2017_f5_d10");  % median and 25 to 75 percent band
exportgraphics(ax.Parent, "results/cec2017/convergence_f5.pdf");
close(ax.Parent);

ax = report.plot_box("cec2017_f5_d10");  % final errors
ax = report.plot_ranks();  % Friedman ranks
close all

path = report.save_html();  % results/cec2017/report.html

The figures fit one column of a paper (3.5 by 2.6 inches, 8 point text).

Convergence band, box plot and rank bars of a small experiment
The three report figures for a small experiment (DE, PSO and CMA-ES with default settings, shifted Rastrigin and Rosenbrock in 10 dimensions, 10 runs of 10 000 evaluations): plot_convergence and plot_box for Rastrigin (top), and plot_ranks over both problems (bottom). The ranks use each algorithm's mean final error on each problem, so PSO and CMA-ES can tie overall.

Parameter sweeps

Experiment.sweep runs one algorithm with every combination of the given values. Each combination becomes one algorithm of the experiment, labeled by its settings. Here DE runs with two crossover probabilities and two values of beta_max, which gives four settings:

import ypea
from ypea.experiment import Experiment

problems = ypea.benchmarks.cec2017(10, functions=[1, 5])
values = {
    "crossover_prob": [0.1, 0.9],
    "beta_max": [0.7, 0.9],
}

sweep = Experiment.sweep(
    ypea.DE,
    values,
    problems,
    runs=3,
    max_nfe=2_000,
)
print(list(sweep.algorithms))

sweep.run()
report = sweep.report()
['DE(crossover_prob=0.1, beta_max=0.7)', 'DE(crossover_prob=0.1, beta_max=0.9)', 'DE(crossover_prob=0.9, beta_max=0.7)', 'DE(crossover_prob=0.9, beta_max=0.9)']

Differs from Python: the values of each setting are fields of a struct.

problems = ypea.benchmarks.cec2017(10, functions=[1 5]);
values = struct( ...
    "crossover_prob", [0.1 0.9], ...
    "beta_max", [0.7 0.9]);

sweep = ypea.experiment.Experiment.sweep( ...
    @ypea.DE, ...
    values, ...
    problems, ...
    runs=3, ...
    max_nfe=2000);
disp(sweep.labels')

sweep.run();
report = sweep.report();
    "DE(crossover_prob=0.1, beta_max=0.7)"
    "DE(crossover_prob=0.1, beta_max=0.9)"
    "DE(crossover_prob=0.9, beta_max=0.7)"
    "DE(crossover_prob=0.9, beta_max=0.9)"

For automatic tuning with racing, see Tuning.

Several objectives

Experiments also compare multi-objective algorithms (see Multiple objectives). Each run then stores its final front and two quality indicators, IGD and hypervolume (explained below), at the end and at every checkpoint. The benchmarks of ypea.benchmarks.multi_problem carry their true fronts, so they work as they are. This example compares three algorithms on three problems and prints the mean of each indicator. It takes about a minute:

import ypea
from ypea.benchmarks import multi_problem
from ypea.experiment import Experiment

problems = [
    multi_problem("zdt1"),
    multi_problem("dtlz2", m=3),
    multi_problem("wfg4", m=3),
]
algorithms = {
    "NSGA-II": ypea.NSGAII(),
    "MOEA/D": ypea.MOEAD(),
    "SPEA2": ypea.SPEA2(),
}

exp = Experiment(
    algorithms,
    problems,
    runs=5,
    max_nfe=5_000,
    seed=2026,
    output="results/multi",
)
exp.run()
report = exp.report()

print(report.table(metric="igd", stats=["mean"]))  # lower is better
print(report.table(metric="hypervolume", stats=["mean"]))  # higher is better
report.plot_convergence("zdt1_m2_d30", metric="igd")
| Problem | Statistic | NSGA-II | MOEA/D | SPEA2 |
|:---|:---|---:|---:|---:|
| zdt1_m2_d30 | Mean | 1.490e-01 = | **1.109e-01** | 1.553e-01 = |
| dtlz2_m3_d12 | Mean | 3.546e-02 − | **2.676e-02** | 3.907e-02 − |
| wfg4_m3_d24 | Mean | 2.491e-01 − | **1.622e-01** | 2.354e-01 − |
| +/=/− | vs MOEA/D | 0/1/2 | control | 0/1/2 |
| Friedman | Avg. rank | 2.33 | **1.00** | 2.67 |
|  | Holm APV | 0.102 | control | 0.0825 |

Wilcoxon rank sum test against MOEA/D at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean IGD over 3 problems: chi2 = 4.667, p = 0.097. Iman and Davenport: F = 7, p = 0.0494. Post hoc z test against MOEA/D with Holm adjusted p values (Derrac et al. 2011).

| Problem | Statistic | NSGA-II | MOEA/D | SPEA2 |
|:---|:---|---:|---:|---:|
| zdt1_m2_d30 | Mean | 6.514e-01 = | **7.434e-01** | 6.357e-01 − |
| dtlz2_m3_d12 | Mean | 7.494e-01 − | **7.702e-01** | 7.445e-01 − |
| wfg4_m3_d24 | Mean | 6.149e-01 − | **6.940e-01** | 6.275e-01 − |
| +/=/− | vs MOEA/D | 0/1/2 | control | 0/0/3 |
| Friedman | Avg. rank | 2.33 | **1.00** | 2.67 |
|  | Holm APV | 0.102 | control | 0.0825 |

Wilcoxon rank sum test against MOEA/D at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean hypervolume over 3 problems: chi2 = 4.667, p = 0.097. Iman and Davenport: F = 7, p = 0.0494. Post hoc z test against MOEA/D with Holm adjusted p values (Derrac et al. 2011).

Differs from Python: MATLAB draws its own random numbers, so the values differ from Python's; this block takes 2 to 3 minutes.

problems = {
    ypea.benchmarks.multi_problem("zdt1")
    ypea.benchmarks.multi_problem("dtlz2", m=3)
    ypea.benchmarks.multi_problem("wfg4", m=3)
    };
algorithms = {
    ypea.NSGAII()
    ypea.MOEAD()
    ypea.SPEA2()
    };
labels = ["NSGA-II", "MOEA/D", "SPEA2"];

exp = ypea.experiment.Experiment( ...
    algorithms, ...
    problems, ...
    labels=labels, ...
    runs=5, ...
    max_nfe=5000, ...
    seed=2026, ...
    output="results/multi");
exp.run();
report = exp.report();

disp(report.table(metric="igd", stats="mean"))  % lower is better
disp(report.table(metric="hypervolume", stats="mean"))  % higher is better
report.plot_convergence("zdt1_m2_d30", metric="igd");
| Problem | Statistic | NSGA-II | MOEA/D | SPEA2 |
|:---|:---|---:|---:|---:|
| zdt1_m2_d30 | Mean | **1.379e-01** = | 1.652e-01 | 1.396e-01 = |
| dtlz2_m3_d12 | Mean | 3.709e-02 − | **2.632e-02** | 3.939e-02 − |
| wfg4_m3_d24 | Mean | 2.450e-01 − | **1.875e-01** | 2.404e-01 − |
| +/=/− | vs MOEA/D | 0/1/2 | control | 0/1/2 |
| Friedman | Avg. rank | 2.00 | **1.67** | 2.33 |
|  | Holm APV | 0.828 | control | 0.828 |

Wilcoxon rank sum test against MOEA/D at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean IGD over 3 problems: chi2 = 0.6667, p = 0.717. Iman and Davenport: F = 0.25, p = 0.79. Post hoc z test against MOEA/D with Holm adjusted p values (Derrac et al. 2011).
| Problem | Statistic | NSGA-II | MOEA/D | SPEA2 |
|:---|:---|---:|---:|---:|
| zdt1_m2_d30 | Mean | 6.633e-01 = | **6.810e-01** | 6.500e-01 = |
| dtlz2_m3_d12 | Mean | 7.466e-01 − | **7.716e-01** | 7.443e-01 − |
| wfg4_m3_d24 | Mean | 6.198e-01 − | **6.720e-01** | 6.233e-01 − |
| +/=/− | vs MOEA/D | 0/1/2 | control | 0/1/2 |
| Friedman | Avg. rank | 2.33 | **1.00** | 2.67 |
|  | Holm APV | 0.102 | control | 0.0825 |

Wilcoxon rank sum test against MOEA/D at alpha = 0.05: + better, = no significant difference, − worse.
Friedman test on the mean hypervolume over 3 problems: chi2 = 4.667, p = 0.097. Iman and Davenport: F = 7, p = 0.0494. Post hoc z test against MOEA/D with Holm adjusted p values (Derrac et al. 2011).

MATLAB figure

The tables have the same statistics and tests as with one objective, and the default metric="error" means IGD. report.values(label, problem, "igd") gives the final values, and report.curves the values at the checkpoints.

Both indicators use only the feasible members of the front, with every objective turned into minimization:

  • IGD (inverted generational distance) is the average distance from the points of the true front to the nearest point of the found front, so lower is better. It uses the raw objective values, as most papers report it, and the sampled true front in problem.info["pareto_front"] (1000 points by default). Objectives with a larger range weigh more: on WFG, whose objectives have the scales 2, 4, 6 and so on, the later objectives dominate.
  • Hypervolume is the volume of objective space that the found front dominates, up to a reference point, so higher is better. It normalizes each objective by the ideal point z and the nadir point n of the true front, f' = (f - z) / (n - z), and uses the reference point 1.1 in every objective. Values of different problems thus share one scale, with a maximum below 1.1^m. It is exact for up to three objectives and a seeded Monte Carlo estimate above.

For your own problem, give the true front as info={"pareto_front": F} (shape (n, m), in the problem's own objective values), or a reference point as info={"ref_point": r}. With only a reference point, the report has the hypervolume of the raw values and no IGD. A reference point given with a front replaces the 1.1 point and is normalized like the values. A problem with neither raises an error.

Runs without max_nfe have no checkpoints, so no convergence plots. ypea.tune accepts only single-objective problems, but ypea.tuning.sweep works with several objectives and returns this report.

examples/experiment_cec2017.py is a complete script with all of these steps.