def test_error_raised_when_redundant_contracts_allow_redundancy_default_false( self): input_contracts = [ (month(2019, 1), 68.64), (month(2019, 2), 59.01), (month(2019, 3), 55.48), (quarter(2019, 1), 62.64), ] with self.assertRaises( ArgumentException): # TODO assert against exception message bootstrap_results = bootstrap_contracts(input_contracts, freq='M')
def test_bootstrap_contracts_averages_back_to_inputs_monthly(self): input_contracts = [(month(2019, 1), 12.35), (month(2019, 2), 13.20), (quarter(2019, 1), 12.85)] piecewise_curve, bootstrapped_contracts = bootstrap_contracts( input_contracts, freq='M') for input_contract in input_contracts: (period, contract_price) = deconstruct_contract(input_contract) output_weighted_average_price = weighted_average_slice_curve( piecewise_curve, 'D', period) self.assertAlmostEqual(output_weighted_average_price, contract_price, delta=1E-10)
def test_error_not_raised_when_redundant_contracts_allow_redundancy_default_false( self): input_contracts = [ (month(2019, 1), 68.64), (month(2019, 2), 59.01), (month(2019, 3), 55.48), (quarter(2019, 1), 62.64), ] piecewise_curve, bootstrapped_contracts = bootstrap_contracts( input_contracts, freq='M', allow_redundancy=True) self.assertEqual(len(piecewise_curve), 3) self.assertEqual(len(bootstrapped_contracts), 3)
def test_average_weight_called_as_expected(self): input_contracts = [(month(2019, 1), 58.64), (month(2019, 2), 58.64), (month(2019, 3), 58.64)] weight_arg_values = [] def average_weight(period): weight_arg_values.append(period) return 1.0 bootstrap_results = bootstrap_contracts(input_contracts, freq='D', average_weight=average_weight) expected_first_arg = pd.Period(year=2019, month=1, day=1, freq='D') expected_arg_values = [expected_first_arg + i for i in range(0, 90)] * 2 self.assertListEqual(expected_arg_values, weight_arg_values)
def test_bootstrap_contracts_averages_back_to_inputs_halfhourly(self): input_contracts = [ (pd.Period('2019-06-07 00:00', freq='30min'), 47.854), (datetime(2019, 6, 7, hour=0, minute=30), 47.705), (datetime(2019, 6, 7, hour=0, minute=30), datetime(2019, 6, 7, hour=1, minute=30), 46.625), ] piecewise_curve, bootstrapped_contracts = bootstrap_contracts( input_contracts, freq='30min') for input_contract in input_contracts: (period, contract_price) = deconstruct_contract(input_contract) output_weighted_average_price = weighted_average_slice_curve( piecewise_curve, '30min', period) self.assertAlmostEqual(output_weighted_average_price, contract_price, delta=1E-10)
def test_bootstrap_contracts_averages_back_to_inputs_daily(self): input_contracts = [ ((date(2019, 1, 1), date(2019, 1, 2)), 32.7), # manually specified contract period ((date(2019, 1, 1), date(2019, 1, 7)), 29.3), # manually specified contract period as nested tuple (quarter(2019, 1), 22.1), (datetime(2019, 1, 1), datetime(2019, 1, 31), 25.5), # January 2019 (month(2019, 2), 23.3), (quarter(2019, 2), 18.3), (quarter(2019, 3), 17.1), (quarter(2019, 4), 20.1), (winter(2019), 22.4), (summer(2020), 19.9), (winter(2020), 21.8), (gas_year(2020), 20.01) ] ratios = [(quarter(2021, 1), quarter(2020, 4), 1.09), (quarter(2020, 3), quarter(2020, 2), 1.005)] spreads = [ (month(2020, 1), month(2020, 2), 0.5), ] def peakload_weight(period): if period.dayofweek < 5: return 1.0 else: return 0.0 piecewise_curve, bootstrapped_contracts = bootstrap_contracts( input_contracts, freq='D', shaping_ratios=ratios, shaping_spreads=spreads, average_weight=peakload_weight) self.assertEqual(16, len(bootstrapped_contracts)) self.assertEqual(1004, len(piecewise_curve)) for input_contract in input_contracts: (period, input_contract_price) = deconstruct_contract(input_contract) output_weighted_average_price = weighted_average_slice_curve( piecewise_curve, 'D', period, peakload_weight) self.assertAlmostEqual(output_weighted_average_price, input_contract_price, delta=1E-10) for bootstrapped_contract in bootstrapped_contracts: output_weighted_average_price = weighted_average_slice_curve( piecewise_curve, 'D', (bootstrapped_contract.start, bootstrapped_contract.end), peakload_weight) self.assertAlmostEqual(output_weighted_average_price, bootstrapped_contract.price, delta=1E-10)