Example #1
0
    def __test_parallel_american_option(self):
        # Branching function calls.

        expected_value = 5
        expected_len_stubbed_exprs = 7

        dsl_source = """
def Option(date, strike, underlying, alternative):
    return Wait(date, Choice(underlying - strike, alternative))

def American(starts, ends, strike, underlying, step):
    Option(starts, strike, underlying, 0) if starts == ends else \
    Option(starts, strike, underlying, American(starts + step, ends, strike, underlying, step))

American(Date('2012-01-01'), Date('2012-01-03'), 5, 10, TimeDelta('1d'))
"""

        dsl_expr = dsl_compile(dsl_source, is_parallel=True)

        # Expect an expression stack object.
        self.assertIsInstance(dsl_expr, DependencyGraph)

        # Remember the number of stubbed exprs - will check it after the value.
        actual_len_stubbed_exprs = len(dsl_expr.call_requirements)

        # Evaluate the stack.
        image = mock.Mock()
        image.price_process.get_duration_years.return_value = 1
        kwds = {
            'image': image,
            'interest_rate': 0,
            'present_time': datetime.datetime(2012, 1, 1, tzinfo=utc),
            'all_market_prices': {
                '#1':
                dict(
                    [(datetime.datetime(2012, 1, 1, tzinfo=utc) +
                      datetime.timedelta(1) * i, numpy.array([10] * 2000))
                     for i in range(0, 10)]
                )  # NB Need enough days to cover the date range in the dsl_source.
            },
        }

        dsl_value = SingleThreadedDependencyGraphRunner(dsl_expr).evaluate(
            **kwds)
        dsl_value = dsl_value.mean()

        # Check the value is expected.
        self.assertEqual(dsl_value, expected_value)

        # Check the number of stubbed exprs is expected.
        self.assertEqual(actual_len_stubbed_exprs, expected_len_stubbed_exprs)
Example #2
0
    def __test_parallel_american_option(self):
        # Branching function calls.

        expected_value = 5
        expected_len_stubbed_exprs = 7

        dsl_source = """
def Option(date, strike, underlying, alternative):
    return Wait(date, Choice(underlying - strike, alternative))

def American(starts, ends, strike, underlying, step):
    Option(starts, strike, underlying, 0) if starts == ends else \
    Option(starts, strike, underlying, American(starts + step, ends, strike, underlying, step))

American(Date('2012-01-01'), Date('2012-01-03'), 5, 10, TimeDelta('1d'))
"""

        dsl_expr = dsl_compile(dsl_source, is_parallel=True)

        # Expect an expression stack object.
        self.assertIsInstance(dsl_expr, DependencyGraph)

        # Remember the number of stubbed exprs - will check it after the value.
        actual_len_stubbed_exprs = len(dsl_expr.call_requirements)

        # Evaluate the stack.
        image = mock.Mock()
        image.price_process.get_duration_years.return_value = 1
        kwds = {
            'image': image,
            'interest_rate': 0,
            'present_time': datetime.datetime(2012, 1, 1, tzinfo=utc),
            'all_market_prices': {
                '#1': dict(
                    [(datetime.datetime(2012, 1, 1, tzinfo=utc) + datetime.timedelta(1) * i, numpy.array([10]*2000))
                        for i in range(0, 10)])  # NB Need enough days to cover the date range in the dsl_source.
            },
        }

        dsl_value = SingleThreadedDependencyGraphRunner(dsl_expr).evaluate(**kwds)
        dsl_value = dsl_value.mean()

        # Check the value is expected.
        self.assertEqual(dsl_value, expected_value)

        # Check the number of stubbed exprs is expected.
        self.assertEqual(actual_len_stubbed_exprs, expected_len_stubbed_exprs)