|
| 1 | +# tests/test_mc.py |
| 2 | +import re |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pytest import approx |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | + |
| 9 | +def test_optimize_df_for_students_valid_case(mc_students): |
| 10 | + result = mc_students.optimize_df_for_students(var_level=5) |
| 11 | + assert isinstance(result, float), "The returned optimized degrees of freedom should be a float." |
| 12 | + assert result == approx(3.04, rel=1e-2) |
| 13 | + |
| 14 | + |
| 15 | +def test_optimize_df_for_students_invalid_var_level_high(mc_students): |
| 16 | + with pytest.raises(ValueError, match=re.escape("var_level must be in [1, 99]")): |
| 17 | + mc_students.optimize_df_for_students(var_level=100) |
| 18 | + |
| 19 | + |
| 20 | +# moved from core |
| 21 | +def test_monte_carlo_returns_ts(mc_normal_small): |
| 22 | + df = mc_normal_small.monte_carlo_returns_ts |
| 23 | + assert df.shape == (12, 10) |
| 24 | + assert df.iloc[-1, :].mean() == approx(0.0156, abs=1e-1) |
| 25 | + |
| 26 | + |
| 27 | +def test_forecast_monte_carlo_cagr(mc_students): |
| 28 | + dic = mc_students.percentile_distribution_cagr(percentiles=[50]) |
| 29 | + assert dic[50] == approx(0.2275, abs=1e-1) |
| 30 | + |
| 31 | + |
| 32 | +def test_skewness(mc_normal_small): |
| 33 | + assert mc_normal_small.skewness.iloc[-1] == approx(-0.6448, abs=1e-2) |
| 34 | + |
| 35 | + |
| 36 | +def test_rolling_skewness(mc_normal_small): |
| 37 | + assert mc_normal_small.skewness_rolling(window=24).iloc[-1] == approx(0.1449, abs=1e-1) |
| 38 | + |
| 39 | + |
| 40 | +def test_kurtosis(mc_normal_small): |
| 41 | + assert mc_normal_small.kurtosis.iloc[-1] == approx(2.7960, rel=1e-2) |
| 42 | + |
| 43 | + |
| 44 | +def test_kurtosis_rolling(mc_normal_small): |
| 45 | + assert mc_normal_small.kurtosis_rolling(window=24).iloc[-1] == approx(-0.1149, rel=1e-1) |
| 46 | + |
| 47 | + |
| 48 | +def test_jarque_bera(mc_normal_small): |
| 49 | + assert mc_normal_small.jarque_bera["statistic"] == approx(66.765, rel=1e-1) |
| 50 | + |
| 51 | + |
| 52 | +# New tests to extend coverage of MonteCarlo |
| 53 | + |
| 54 | +def test_percentile_inverse_cagr_range(mc_students): |
| 55 | + # Should return a percentile between 0 and 100 |
| 56 | + p = mc_students.percentile_inverse_cagr(score=0) |
| 57 | + assert isinstance(p, float) |
| 58 | + assert 0.0 <= p <= 100.0 |
| 59 | + |
| 60 | + |
| 61 | +def test_kstest_structure(mc_students): |
| 62 | + res = mc_students.kstest |
| 63 | + assert set(res.keys()) == {"statistic", "p-value"} |
| 64 | + assert isinstance(res["statistic"], float) |
| 65 | + assert isinstance(res["p-value"], float) |
| 66 | + assert 0.0 <= res["p-value"] <= 1.0 |
| 67 | + |
| 68 | + |
| 69 | +def test_kstest_for_all_distributions(mc_students): |
| 70 | + df = mc_students.kstest_for_all_distributions |
| 71 | + assert isinstance(df, pd.DataFrame) |
| 72 | + # Expect rows for all configured distributions |
| 73 | + assert len(df.index) >= 3 |
| 74 | + for col in ("statistic", "p-value"): |
| 75 | + assert col in df.columns |
| 76 | + |
| 77 | + |
| 78 | +def test_model_risk_structure(mc_students): |
| 79 | + res = mc_students.model_risk(var_level=5) |
| 80 | + assert set(res.keys()) == {"delta_arithmetic_mean", "delta_var", "delta_cvar"} |
| 81 | + for k in res: |
| 82 | + assert isinstance(res[k], float) |
| 83 | + |
| 84 | + |
| 85 | +# Tests for get_parameters_for_distribution |
| 86 | + |
| 87 | +def test_get_parameters_for_distribution_norm_defaults(mc_normal_small): |
| 88 | + # With None parameters, should use historical mean and std |
| 89 | + mc_normal_small.distribution_parameters = (None, None) |
| 90 | + mu, sigma = mc_normal_small.get_parameters_for_distribution() |
| 91 | + assert isinstance(mu, float) and isinstance(sigma, float) |
| 92 | + assert mu == approx(float(mc_normal_small.ror.mean()), rel=1e-12, abs=0) |
| 93 | + assert sigma == approx(float(mc_normal_small.ror.std()), rel=1e-12, abs=0) |
| 94 | + |
| 95 | + |
| 96 | +def test_get_parameters_for_distribution_norm_partial_override(mc_normal_small): |
| 97 | + # Override only mu, keep sigma from data |
| 98 | + mc_normal_small.distribution_parameters = (0.01, None) |
| 99 | + mu, sigma = mc_normal_small.get_parameters_for_distribution() |
| 100 | + assert mu == approx(0.01, rel=0, abs=0) |
| 101 | + assert sigma == approx(float(mc_normal_small.ror.std()), rel=1e-12, abs=0) |
| 102 | + |
| 103 | + |
| 104 | +def test_get_parameters_for_distribution_norm_full_override(mc_normal_small): |
| 105 | + # Full pass-through when both params provided |
| 106 | + mc_normal_small.distribution_parameters = (0.02, 0.05) |
| 107 | + mu, sigma = mc_normal_small.get_parameters_for_distribution() |
| 108 | + assert mu == approx(0.02) |
| 109 | + assert sigma == approx(0.05) |
| 110 | + |
| 111 | + |
| 112 | +def test_get_parameters_for_distribution_lognorm_defaults(mc_lognormal_small): |
| 113 | + # With None parameters, should fit with loc fixed at -1.0 |
| 114 | + mc_lognormal_small.distribution_parameters = (None, None, None) |
| 115 | + shape, loc, scale = mc_lognormal_small.get_parameters_for_distribution() |
| 116 | + assert isinstance(shape, float) and isinstance(loc, float) and isinstance(scale, float) |
| 117 | + assert loc == approx(-1.0, rel=0, abs=0) |
| 118 | + assert shape == approx(0.07, abs=1e-02) |
| 119 | + assert scale == approx(1.012, abs=1e-02) |
| 120 | + |
| 121 | + |
| 122 | +def test_get_parameters_for_distribution_lognorm_full_override(mc_lognormal_small): |
| 123 | + # Full pass-through for lognormal; returned loc must be preserved |
| 124 | + mc_lognormal_small.distribution_parameters = (0.4, -1.0, 0.1) |
| 125 | + shape, loc, scale = mc_lognormal_small.get_parameters_for_distribution() |
| 126 | + assert shape == approx(0.4) |
| 127 | + assert loc == approx(-1.0, rel=0, abs=0) |
| 128 | + assert scale == approx(0.1) |
| 129 | + |
| 130 | + |
| 131 | +def test_get_parameters_for_distribution_t_defaults(mc_students): |
| 132 | + # With None parameters, should fit t distribution |
| 133 | + mc_students.distribution_parameters = (None, None, None) |
| 134 | + df, loc, scale = mc_students.get_parameters_for_distribution() |
| 135 | + assert isinstance(df, float) and isinstance(loc, float) and isinstance(scale, float) |
| 136 | + assert df > 2 # df must be > 2 for finite variance |
| 137 | + assert scale > 0 |
| 138 | + |
| 139 | + |
| 140 | +def test_get_parameters_for_distribution_t_full_override(mc_students): |
| 141 | + mc_students.distribution_parameters = (5.0, 0.0, 0.02) |
| 142 | + df, loc, scale = mc_students.get_parameters_for_distribution() |
| 143 | + assert df == approx(5.0) |
| 144 | + assert loc == approx(0.0) |
| 145 | + assert scale == approx(0.02) |
| 146 | + |
| 147 | +def test_get_parameters_for_distribution_lognormal_defaults(mc_lognormal_small): |
| 148 | + mc_lognormal_small.distribution_parameters = (None, None, None) |
| 149 | + shape, loc, scale = mc_lognormal_small.get_parameters_for_distribution() |
0 commit comments