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)
Example #3
0
    def __test_parallel_fib(self):
        # Branching function calls.

        fib_index = 6
        expected_value = 13
        expected_len_stubbed_exprs = fib_index + 1

        dsl_source = """
def fib(n): fib(n-1) + fib(n-2) if n > 2 else n
fib(%d)
""" % fib_index

        # # Check the source works as a serial operation.
        # dsl_expr = dsl_parse(dsl_source, inParallel=False)
        # self.assertIsInstance(dsl_expr, Add)
        # dsl_value = dsl_expr.evaluate()
        # self.assertEqual(dsl_value, expected_value)

        # Check the source works as a parallel operation.
        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.
        runner = SingleThreadedDependencyGraphRunner(dsl_expr)
        dsl_value = runner.evaluate()

        # 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)

        # Also check the runner call count is the same.
        self.assertEqual(runner.call_count, expected_len_stubbed_exprs)
Example #4
0
    def __test_parallel_fib(self):
        # Branching function calls.

        fib_index = 6
        expected_value = 13
        expected_len_stubbed_exprs = fib_index + 1

        dsl_source = """
def fib(n): fib(n-1) + fib(n-2) if n > 2 else n
fib(%d)
""" % fib_index

        # # Check the source works as a serial operation.
        # dsl_expr = dsl_parse(dsl_source, inParallel=False)
        # self.assertIsInstance(dsl_expr, Add)
        # dsl_value = dsl_expr.evaluate()
        # self.assertEqual(dsl_value, expected_value)

        # Check the source works as a parallel operation.
        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.
        runner = SingleThreadedDependencyGraphRunner(dsl_expr)
        dsl_value = runner.evaluate()

        # 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)

        # Also check the runner call count is the same.
        self.assertEqual(runner.call_count, expected_len_stubbed_exprs)
Example #5
0
    def __test_parallel_swing_option(self):
        # Branching function calls.

        expected_value = 20
        expected_len_stubbed_exprs = 7

        dsl_source = """
def Swing(starts, ends, underlying, quantity):
    if (quantity != 0) and (starts < ends):
        return Max(
            Swing(starts + TimeDelta('1d'), ends, underlying, quantity-1) \
            + Fixing(starts, underlying),
            Swing(starts + TimeDelta('1d'), ends, underlying, quantity)
        )
    else:
        return 0
Swing(Date('2011-01-01'), Date('2011-01-03'), 10, 5)
"""

        dsl_expr = dsl_compile(dsl_source, is_parallel=True)

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

        # 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(2011, 1, 1),
        }

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

        # 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)