Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,8 @@ def simulate_convergence(
-------
confidence_interval_history : list of float
History of confidence interval widths, one value per batch of simulations.
A batch that leaves fewer than two results contributes no width, since a
confidence interval cannot be estimated from a single observation.
The last element corresponds to the width when the simulation stopped for
either meeting the tolerance or reaching the maximum number of simulations.
"""
Expand Down Expand Up @@ -1027,6 +1029,12 @@ def simulate_convergence(

self.import_outputs(self.filename.with_suffix(".outputs.txt"))

# scipy.stats.bootstrap needs two or more observations, so the first
# batch of a fresh study with batch_size=1 cannot be summarised yet.
# Keep simulating until the logs hold two rows.
if self.num_of_loaded_sims < 2:
continue

ci = self.estimate_confidence_interval(
attribute=target_attribute,
confidence_level=target_confidence,
Expand Down
29 changes: 29 additions & 0 deletions tests/integration/simulation/test_monte_carlo_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ def test_convergence_reads_the_checkpoint_once(release_study, monkeypatch):
]


def test_convergence_with_a_batch_size_of_one_waits_for_a_second_result(
release_study,
):
"""A fresh study with ``batch_size=1`` must not bootstrap a lone result.

The first batch leaves a single row, and ``scipy.stats.bootstrap`` needs at
least two, so the study has to keep running instead of raising on its very
first batch.
"""
history = release_study.simulate_convergence(
target_attribute="max_speed",
tolerance=1e-12,
max_simulations=4,
batch_size=1,
random_seed=1,
)

assert release_study.num_of_loaded_sims == 4
# One width per batch that had two results to bootstrap: the batches that
# ended at 2, 3 and 4 rows.
assert len(history) == 3
assert sorted(row["index"] for row in _rows(release_study.input_file)) == [
0,
1,
2,
3,
]


def test_a_log_changed_after_the_run_is_read_again(release_study):
"""The shortcut only holds while the logs are what the study wrote."""
release_study.simulate(2, random_seed=42, include_function_data=False)
Expand Down