def test_compute_mean_std_from_runs(mock_model, iteration_results):
    vol = BaseVolatilityEstimator(mock_model)
    results = vol._compute_mean_std_from_runs(
        iteration_results[iteration_results['metric_name'] == 'roc_auc'])
    expected_results = [0.8, 0.08164, 0.7, 0.08164, 0.1, 0]
    for idx, item in enumerate(results):
        assert pytest.approx(item, 0.01) == expected_results[idx]
def test_compute_stats_tests_values(mock_model, iteration_results):
    vol = BaseVolatilityEstimator(mock_model, stats_tests_to_apply=['KS'])

    with patch.object(DistributionStatistics,
                      'compute',
                      return_value=(0.1, 0.05)):
        stats = vol._compute_stats_tests_values(iteration_results)

    assert stats[0] == 0.1
    assert stats[1] == 0.05
def test_get_samples_to_plot(mock_model, iteration_results, iterations_train,
                             iterations_test, iterations_delta):
    vol = BaseVolatilityEstimator(mock_model)
    vol.fitted = True
    vol.iterations_results = iteration_results

    train, test, delta = vol._get_samples_to_plot(metric_name='roc_auc')
    pd.testing.assert_series_equal(train, iterations_train)
    pd.testing.assert_series_equal(test, iterations_test)
    pd.testing.assert_series_equal(delta, iterations_delta)
def test_base_fit(mock_model, X_df, y_series):
    vol = BaseVolatilityEstimator(mock_model, random_state=1)

    with patch('numpy.random.seed') as mock_seed:
        vol.fit(X_df, y_series)
        mock_seed.assert_called_with(1)

    assert vol.iterations_results is None
    assert vol.report is None
    assert vol.fitted is True
def test_fit_compute(mock_model, report, X_df, y_series):
    vol = BaseVolatilityEstimator(mock_model)

    with patch.object(BaseVolatilityEstimator, 'fit') as mock_fit:
        with patch.object(BaseVolatilityEstimator,
                          'compute',
                          return_value=report) as mock_compute:
            result = vol.fit_compute(X_df, y_series)

            mock_fit.assert_called_with(X_df, y_series)
            mock_compute.assert_called_with()

    pd.testing.assert_frame_equal(result, report)
def test_plot(report, mock_model, iterations_train, iterations_test,
              iterations_delta):
    with patch.object(BaseVolatilityEstimator,
                      'compute',
                      return_value=report.loc[['roc_auc']]) as mock_compute:
        with patch.object(BaseVolatilityEstimator,
                          '_get_samples_to_plot',
                          return_value=(iterations_train, iterations_test,
                                        iterations_delta)) as mock_get_samples:

            vol = BaseVolatilityEstimator(mock_model)
            vol.fitted = True

            vol.plot(metrics='roc_auc')
            mock_compute.assert_called_with(metrics='roc_auc')
            mock_get_samples.assert_called_with(metric_name='roc_auc')
Ejemplo n.º 7
0
def test_plot(report, mock_model, iterations_train, iterations_test,
              iterations_delta):
    """
    Test.
    """
    with patch.object(BaseVolatilityEstimator,
                      "compute",
                      return_value=report.loc[["roc_auc"]]) as mock_compute:
        with patch.object(
                BaseVolatilityEstimator,
                "_get_samples_to_plot",
                return_value=(iterations_train, iterations_test,
                              iterations_delta),
        ) as mock_get_samples:

            vol = BaseVolatilityEstimator(mock_model)
            vol.fitted = True

            vol.plot(metrics="roc_auc")
            mock_compute.assert_called_with(metrics="roc_auc")
            mock_get_samples.assert_called_with(metric_name="roc_auc")
def test_create_report(mock_model, iteration_results, report):
    vol = BaseVolatilityEstimator(mock_model)
    vol.fitted = True
    vol.iterations_results = iteration_results

    vol._create_report()
    pd.testing.assert_frame_equal(vol.report, report, check_less_precise=3)
Ejemplo n.º 9
0
def test_compute(report, mock_model):
    """
    Test.
    """
    vol = BaseVolatilityEstimator(mock_model)

    with pytest.raises(NotFittedError):
        vol.compute()

    vol.fit()
    with pytest.raises(ValueError):
        vol.compute()

    vol.report = report

    pd.testing.assert_frame_equal(vol.compute(), report)
    pd.testing.assert_frame_equal(vol.compute(metrics=["roc_auc"]),
                                  report.loc[["roc_auc"]])
    pd.testing.assert_frame_equal(vol.compute(metrics="roc_auc"),
                                  report.loc[["roc_auc"]])