|
| 1 | +Reproducibility and Seeds |
| 2 | +========================= |
| 3 | + |
| 4 | +SAES supports deterministic behavior for reproducible research through random seed control. |
| 5 | + |
| 6 | +Why Reproducibility Matters |
| 7 | +--------------------------- |
| 8 | + |
| 9 | +When analyzing stochastic algorithms, reproducibility is crucial for: |
| 10 | + |
| 11 | +- **Research validation**: Others can verify your results |
| 12 | +- **Debugging**: Consistent results make it easier to identify issues |
| 13 | +- **Comparisons**: Fair comparison requires consistent conditions |
| 14 | +- **Publication**: Many journals and conferences require reproducible results |
| 15 | + |
| 16 | +Functions with Random Seeds |
| 17 | +--------------------------- |
| 18 | + |
| 19 | +The following SAES functions support deterministic execution via the ``seed`` parameter: |
| 20 | + |
| 21 | +Bayesian Statistical Tests |
| 22 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | + |
| 24 | +Both Bayesian tests support the ``seed`` parameter for reproducibility: |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + from SAES.statistical_tests.bayesian import bayesian_sign_test, bayesian_signed_rank_test |
| 29 | + import pandas as pd |
| 30 | +
|
| 31 | + data = pd.DataFrame({ |
| 32 | + 'Algorithm_A': [0.9, 0.85, 0.95, 0.9, 0.92], |
| 33 | + 'Algorithm_B': [0.5, 0.6, 0.55, 0.58, 0.52] |
| 34 | + }) |
| 35 | +
|
| 36 | + # Deterministic results with seed |
| 37 | + result1, _ = bayesian_sign_test(data, sample_size=5000, seed=42) |
| 38 | + result2, _ = bayesian_sign_test(data, sample_size=5000, seed=42) |
| 39 | + # result1 and result2 will be identical |
| 40 | +
|
| 41 | + # Same for signed rank test |
| 42 | + result3, _ = bayesian_signed_rank_test(data, sample_size=1000, seed=123) |
| 43 | +
|
| 44 | +Histogram Plots |
| 45 | +~~~~~~~~~~~~~~ |
| 46 | + |
| 47 | +The HistoPlot class supports seeding for consistent jitter when handling identical values: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + from SAES.plots.histoplot import HistoPlot |
| 52 | + import pandas as pd |
| 53 | +
|
| 54 | + data = pd.read_csv("results.csv") |
| 55 | + metrics = pd.read_csv("metrics.csv") |
| 56 | +
|
| 57 | + # Create histoplot with reproducible jitter |
| 58 | + histoplot = HistoPlot(data, metrics, "Accuracy", seed=42) |
| 59 | + histoplot.save_instance("Problem1", "output.png") |
| 60 | +
|
| 61 | +Best Practices |
| 62 | +------------- |
| 63 | + |
| 64 | +1. **Always use seeds for published research**: Set explicit seeds for all random operations |
| 65 | +2. **Document your seeds**: Include seed values in your research papers and code |
| 66 | +3. **Use different seeds for different experiments**: Avoid accidentally reusing the same random sequence |
| 67 | +4. **Version control**: Include seed values in your version-controlled analysis scripts |
| 68 | + |
| 69 | +Example: Complete Reproducible Workflow |
| 70 | +--------------------------------------- |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + from SAES.statistical_tests.bayesian import bayesian_sign_test, bayesian_signed_rank_test |
| 75 | + from SAES.plots.histoplot import HistoPlot |
| 76 | + import pandas as pd |
| 77 | +
|
| 78 | + # Load data |
| 79 | + data = pd.read_csv("algorithm_results.csv") |
| 80 | + metrics = pd.read_csv("metrics.csv") |
| 81 | +
|
| 82 | + # Reproducible Bayesian analysis |
| 83 | + SEED = 42 |
| 84 | + algorithm_a = data[data['Algorithm'] == 'A']['MetricValue'] |
| 85 | + algorithm_b = data[data['Algorithm'] == 'B']['MetricValue'] |
| 86 | + |
| 87 | + comparison_data = pd.DataFrame({ |
| 88 | + 'Algorithm_A': algorithm_a.values, |
| 89 | + 'Algorithm_B': algorithm_b.values |
| 90 | + }) |
| 91 | +
|
| 92 | + # Run Bayesian test with seed |
| 93 | + result, samples = bayesian_sign_test( |
| 94 | + comparison_data, |
| 95 | + sample_size=5000, |
| 96 | + seed=SEED |
| 97 | + ) |
| 98 | +
|
| 99 | + print(f"P(A < B): {result[0]:.4f}") |
| 100 | + print(f"P(A ≈ B): {result[1]:.4f}") |
| 101 | + print(f"P(A > B): {result[2]:.4f}") |
| 102 | +
|
| 103 | + # Create reproducible visualization |
| 104 | + histoplot = HistoPlot(data, metrics, "Accuracy", seed=SEED) |
| 105 | + histoplot.save_all_instances("comparison.png") |
| 106 | +
|
| 107 | +Headless Mode for Automated Workflows |
| 108 | +------------------------------------- |
| 109 | + |
| 110 | +SAES can be run in headless mode (without display) for automated pipelines and CI/CD: |
| 111 | + |
| 112 | +.. code-block:: bash |
| 113 | +
|
| 114 | + # Set matplotlib to use non-interactive backend |
| 115 | + export MPLBACKEND=Agg |
| 116 | +
|
| 117 | + # Run SAES commands |
| 118 | + python -m SAES -ls -ds data.csv -ms metrics.csv -m HV -s friedman -op results.tex |
| 119 | + python -m SAES -bp -ds data.csv -ms metrics.csv -m HV -i Problem1 -op boxplot.png |
| 120 | + python -m SAES -cdp -ds data.csv -ms metrics.csv -m HV -op cdplot.png |
| 121 | +
|
| 122 | +For Python scripts in headless environments: |
| 123 | + |
| 124 | +.. code-block:: python |
| 125 | +
|
| 126 | + import matplotlib |
| 127 | + matplotlib.use('Agg') # Must be called before importing pyplot |
| 128 | + |
| 129 | + from SAES.plots.boxplot import Boxplot |
| 130 | + import pandas as pd |
| 131 | +
|
| 132 | + # Your analysis code here |
| 133 | + data = pd.read_csv("results.csv") |
| 134 | + metrics = pd.read_csv("metrics.csv") |
| 135 | + |
| 136 | + boxplot = Boxplot(data, metrics, "Accuracy") |
| 137 | + boxplot.save_instance("Problem1", "output.png") |
| 138 | +
|
0 commit comments