Ejemplo n.º 1
0
def test_estimate_for_monte_carlo(num_models, num_samples,
                                  mc_sample_allocation):
    mc_outputs = [np.empty(0) for _ in range(num_models)]
    mc_outputs[0] = np.random.random(num_samples)

    covariance = np.eye(num_models)
    est = Estimator(mc_sample_allocation, covariance)

    expected_estimate = np.mean(mc_outputs[0])
    assert est.get_estimate(mc_outputs) == pytest.approx(expected_estimate)
Ejemplo n.º 2
0
def test_two_model_estimate():
    compressed_allocation = np.array([[1, 1, 1, 1], [5, 1, 1, 0],
                                      [10, 0, 0, 1]])
    allocation = MLMCSampleAllocation(compressed_allocation)
    model_outputs = [np.arange(1, 7), np.arange(1, 17)]
    covariance = np.array([[1, 0.5], [0.5, 1]])

    est = Estimator(allocation, covariance)

    expected_estimate = 10.545454545454547
    assert est.get_estimate(model_outputs) == pytest.approx(expected_estimate)
Ejemplo n.º 3
0
def test_three_model_approximate_variance():
    compressed_allocation = np.array([[1, 1, 1, 1, 0, 0], [5, 0, 1, 1, 1, 1],
                                      [10, 0, 0, 0, 1, 1]])
    sample_allocation = MLMCSampleAllocation(compressed_allocation)
    covariance = np.array([[1, 0.5, 0.25], [0.5, 1, 0.5], [0.25, 0.5, 1]])
    est = Estimator(sample_allocation, covariance)

    assert est.approximate_variance == pytest.approx(1)
Ejemplo n.º 4
0
def test_two_model_approximate_variance():
    compressed_allocation = np.array([[3, 1, 1, 1], [57, 0, 0, 1]], dtype=int)
    allocation = MLMCSampleAllocation(compressed_allocation)
    covariance = np.array([[1, 0.5], [0.5, 1]])

    est = Estimator(allocation, covariance)

    assert est.approximate_variance == pytest.approx(1 / 3.)
Ejemplo n.º 5
0
def _assert_opt_result_is_consistent(covariance, model_costs, opt_result):
    sample_allocation = opt_result.allocation
    estimator = Estimator(sample_allocation, covariance)
    estimator_approx_variance = estimator.approximate_variance
    optimizer_approx_variance = opt_result.variance
    assert estimator_approx_variance \
        == pytest.approx(optimizer_approx_variance)
    actual_cost = _calculate_costs_from_allocation(opt_result.allocation,
                                                   model_costs)
    assert opt_result.cost == pytest.approx(actual_cost)
Ejemplo n.º 6
0
def test_decreases_variance(two_model_compressed_allocation,
                            two_model_costs):

    # Ignore warning regarding variance in BaseSampleAllocation.
    warnings.filterwarnings(action="ignore", category=UserWarning)

    # This allocation and model costs have a total cost of 200.
    covariance = np.array([[1., 0.3],
                           [0.3, 1.]])
    target_cost = 215

    base_allocation = MLMCSampleAllocation(two_model_compressed_allocation)
    base_estimate = Estimator(base_allocation, covariance)
    base_variance = base_estimate._get_approximate_variance()
    adjusted_allocation = adjust_sample_allocation_to_cost(base_allocation,
                                                           target_cost,
                                                           two_model_costs,
                                                           covariance)

    adjusted_estimate = Estimator(adjusted_allocation, covariance)
    adjusted_variance = adjusted_estimate._get_approximate_variance()

    assert adjusted_variance < base_variance
Ejemplo n.º 7
0
def _get_estimator_variance(sample_allocation, covariance):

    estimator = Estimator(sample_allocation, covariance)
    return estimator._get_approximate_variance()