def setup_parameters(): nb_periods = 126 mean, std = 6e-4, 1.04e-2 div_periods = np.array([53, 117, 181, 242]) div_periods = div_periods[div_periods <= nb_periods] div_yield = 5e-3 * np.ones_like(div_periods) rate, s0 = 4e-5, 64.94 omega = np.random.choice([-1, 1], nb_periods) tree = BinomialTree(nb_periods, mean, std, rate, s0, div_yield=div_yield, div_periods=div_periods) tree.generate() return tree, omega, s0
def remove_min_node(self): min_tree = self.get_min_tree() if not min_tree: return self.trees.remove(min_tree) for root_child in min_tree.root.children: new_tree = BinomialTree(root=root_child) self.trees.append(new_tree) self._fix_heap()
def test_is_discounted_asset_price_martingale(): nb_periods = 126 mean, std = 6e-4, 1.04e-2 div_periods = np.array([53, 117, 181, 242]) div_periods = div_periods[div_periods <= nb_periods] div_yield = 5e-3 * np.ones_like(div_periods) rate, s0 = 4e-5, 64.94 tree = BinomialTree(nb_periods, mean, std, rate, s0, div_yield=div_yield, div_periods=div_periods) omega = np.random.choice([-1, 1], nb_periods) tree.generate() assert np.isclose( tree_paths.extract(tree.tree, omega)[:-1], tree.calculate_expectation(omega)).all()
def test_binomial_tree_upper_population(self): btree = BinomialTree(self.volatility, self.rates) calculated_rates = [ ra * (math.e**(1 * (en + 1) * (self.volatility / 100))) for en, ra in enumerate(self.rates[1:]) ] calculated_rates.insert(0, self.rates[0]) binomial_rates = [btree.root_node.value] next = btree.root_node.up_child while next is not None: binomial_rates.append(next.value) next = next.up_child self.assertEqual(binomial_rates, calculated_rates)
def add_elem(self, key): tree = BinomialTree(initial_key=key) self.trees.append(tree) self._fix_heap()
def __init__(self, tree: BinomialTree, payoff_func, **option_params): self.tree = tree self.payoff_tree = tree.calculate_payoff_tree(payoff_func, **option_params) self.envelope_tree = tree.calculate_envelope_tree(self.payoff_tree)