Exemplo n.º 1
0
def test_multiple_measures(mocker):
    day = [
        [
            [{
                '$type': 'RiskVector',
                'asset': [0.01, 0.015],
                'points': [
                    {'type': 'IR Vol', 'asset': 'USD-LIBOR-BBA', 'class_': 'Swap', 'point': '1y'},
                    {'type': 'IR Vol', 'asset': 'USD-LIBOR-BBA', 'class_': 'Swap', 'point': '2y'}
                ]
            }]
        ],
        [
            [{
                '$type': 'RiskVector',
                'asset': [0.01, 0.015],
                'points': [
                    {'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '1y'},
                    {'type': 'IR', 'asset': 'USD', 'class_': 'Swap', 'point': '2y'}
                ]
            }],
        ],
        [
            [{'$type': 'Risk', 'val': 0.01}]
        ]
    ]

    mocker.return_value = [day, day, day]

    set_session()

    ir_swaption = IRSwaption('Pay', '10y', 'USD')

    dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9))
    with HistoricalPricingContext(dates=dates, use_cache=True):
        ir_swaption.price()
        ir_swaption.calc(risk.IRDelta)
        ir_swaption.calc(risk.IRVega)

    # make sure all the risk measures got cached correctly
    for date in dates:
        with PricingContext(pricing_date=date) as pc:
            for risk_measure in (risk.Price, risk.IRDelta, risk.IRVega):
                val = PricingCache.get(pc._PricingContext__risk_key(risk_measure, ir_swaption.provider), ir_swaption)
                assert val is not None

    with PricingContext(pricing_date=dt.date(2019, 10, 11)) as pc:
        for risk_measure in (risk.Price, risk.IRDelta, risk.IRVega):
            val = PricingCache.get(pc._PricingContext__risk_key(risk_measure, ir_swaption.provider), ir_swaption)
            assert val is None
Exemplo n.º 2
0
def test_multiple_measures(mocker):
    values = [[[{
        'date': '2019-10-07',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '1y',
        'value': 0.01
    }, {
        'date': '2019-10-07',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '2y',
        'value': 0.015
    }, {
        'date': '2019-10-08',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '1y',
        'value': 0.01
    }, {
        'date': '2019-10-08',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '2y',
        'value': 0.015
    }, {
        'date': '2019-10-09',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '1y',
        'value': 0.01
    }, {
        'date': '2019-10-09',
        'marketDataType': 'IR Vol',
        'assetId': 'USD-LIBOR-BBA',
        'pointClass': 'Swap',
        'point': '2y',
        'value': 0.015
    }]],
              [[{
                  'date': '2019-10-07',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '1y',
                  'value': 0.01
              }, {
                  'date': '2019-10-07',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '2y',
                  'value': 0.015
              }, {
                  'date': '2019-10-08',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '1y',
                  'value': 0.01
              }, {
                  'date': '2019-10-08',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '2y',
                  'value': 0.015
              }, {
                  'date': '2019-10-09',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '1y',
                  'value': 0.01
              }, {
                  'date': '2019-10-09',
                  'marketDataType': 'IR',
                  'assetId': 'USD',
                  'pointClass': 'Swap',
                  'point': '2y',
                  'value': 0.015
              }]],
              [[{
                  'date': '2019-10-07',
                  'value': 0.01
              }, {
                  'date': '2019-10-08',
                  'value': 0.01
              }, {
                  'date': '2019-10-09',
                  'value': 0.01
              }]]]
    mocker.return_value = values

    set_session()

    ir_swaption = IRSwaption('Pay', '10y', 'USD')

    dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9))
    with HistoricalPricingContext(dates=dates, use_cache=True):
        market_data_location = PricingContext.current.market_data_location
        ir_swaption.price()
        ir_swaption.calc(risk.IRDelta)
        ir_swaption.calc(risk.IRVega)

    # make sure all the risk measures got cached correctly
    cached = PricingCache.get(ir_swaption, market_data_location, risk.Price,
                              dates)
    assert len(cached) == len(dates)

    cached1 = PricingCache.get(ir_swaption, market_data_location, risk.IRDelta,
                               dates)
    assert len(cached1.index.unique()) == len(dates)

    cached2 = PricingCache.get(ir_swaption, market_data_location, risk.IRVega,
                               dates)
    assert len(cached2.index.unique()) == len(dates)

    # date not in cache
    cached3 = PricingCache.get(ir_swaption, market_data_location, risk.IRVega,
                               dt.date(2019, 10, 11))
    assert cached3 is None

    # subset from cache
    subset = dates[0:2]
    cached4 = PricingCache.get(ir_swaption, market_data_location, risk.Price,
                               subset)
    assert len(cached4) == len(subset)

    # intersection
    subset += (dt.date(2019, 10, 2), )
    cached5 = PricingCache.get(ir_swaption, market_data_location, risk.Price,
                               subset)
    assert len(cached5) == len(subset) - 1
Exemplo n.º 3
0
def test_multiple_measures(mocker):
    values = [[[{
        '$type':
        'RiskVector',
        'asset': [0.01, 0.015],
        'points': [{
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '1y'
        }, {
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '2y'
        }]
    }, {
        '$type':
        'RiskVector',
        'asset': [0.01, 0.015],
        'points': [{
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '1y'
        }, {
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '2y'
        }]
    }, {
        '$type':
        'RiskVector',
        'asset': [0.01, 0.015],
        'points': [{
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '1y'
        }, {
            'type': 'IR Vol',
            'asset': 'USD-LIBOR-BBA',
            'class_': 'Swap',
            'point': '2y'
        }]
    }]],
              [[{
                  '$type':
                  'RiskVector',
                  'asset': [0.01, 0.015],
                  'points': [{
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '1y'
                  }, {
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '2y'
                  }]
              }, {
                  '$type':
                  'RiskVector',
                  'asset': [0.01, 0.015],
                  'points': [{
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '1y'
                  }, {
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '2y'
                  }]
              }, {
                  '$type':
                  'RiskVector',
                  'asset': [0.01, 0.015],
                  'points': [{
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '1y'
                  }, {
                      'type': 'IR',
                      'asset': 'USD',
                      'class_': 'Swap',
                      'point': '2y'
                  }]
              }]],
              [[{
                  '$type': 'Risk',
                  'val': 0.01
              }, {
                  '$type': 'Risk',
                  'val': 0.01
              }, {
                  '$type': 'Risk',
                  'val': 0.01
              }]]]
    mocker.return_value = [values]

    set_session()

    ir_swaption = IRSwaption('Pay', '10y', 'USD')

    dates = (dt.date(2019, 10, 7), dt.date(2019, 10, 8), dt.date(2019, 10, 9))
    with HistoricalPricingContext(dates=dates, use_cache=True) as hpc:
        pricing_key = hpc.pricing_key
        ir_swaption.price()
        ir_swaption.calc(risk.IRDelta)
        ir_swaption.calc(risk.IRVega)

    # make sure all the risk measures got cached correctly
    cached = PricingCache.get(ir_swaption, risk.Price, pricing_key)
    assert len(cached) == len(dates)

    cached1 = PricingCache.get(ir_swaption, risk.IRDelta, pricing_key)
    assert len(cached1.index.unique()) == len(dates)

    cached2 = PricingCache.get(ir_swaption, risk.IRVega, pricing_key)
    assert len(cached2.index.unique()) == len(dates)

    # date not in cache
    cached3 = PricingCache.get(
        ir_swaption, risk.IRVega,
        PricingContext(pricing_date=dt.date(2019, 10, 11)).pricing_key)
    assert cached3 is None

    # subset from cache
    subset_key = HistoricalPricingContext(dates=dates[0:2]).pricing_key
    cached4 = PricingCache.get(ir_swaption, risk.Price, subset_key)
    assert len(cached4) == 2

    # intersection
    subset_key = HistoricalPricingContext(dates=dates[0:2] +
                                          (dt.date(2019, 10, 2), )).pricing_key
    cached5 = PricingCache.get(ir_swaption,
                               risk.Price,
                               subset_key,
                               return_partial=True)
    assert len(cached5) == 2