Skip to content

Commit

Permalink
Asserts updated
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Sep 30, 2024
1 parent 6d3985b commit c42ebbc
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 332 deletions.
24 changes: 6 additions & 18 deletions financepy/products/equity/equity_fixed_lookback_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class EquityFixedLookbackOption(EquityOption):
the value of the stock price used to determine the payoff is the maximum
in the case of a call option, and a minimum in the case of a put option."""

def __init__(
self, expiry_dt: Date, option_type: OptionTypes, strike_price: float
):
def __init__(self, expiry_dt: Date, option_type: OptionTypes, strike_price: float):
"""Create the FixedLookbackOption by specifying the expiry date, the
option type and the option strike."""

Expand Down Expand Up @@ -142,9 +140,7 @@ def value(

else:

e1 = (
(np.log(s0 / s_max) + (r - q + v * v / 2) * t) / v / sqrt_t
)
e1 = (np.log(s0 / s_max) + (r - q + v * v / 2) * t) / v / sqrt_t
e2 = e1 - v * sqrt_t

if s0 == s_max:
Expand Down Expand Up @@ -203,9 +199,7 @@ def value(
v = k * df * N(-d2) - s0 * dq * N(-d1) + s0 * df * u * term

else:
raise FinError(
"Unknown lookback option type:" + str(self.option_type)
)
raise FinError("Unknown lookback option type:" + str(self.option_type))

return v

Expand Down Expand Up @@ -244,22 +238,16 @@ def value_mc(
if self.option_type == OptionTypes.EUROPEAN_CALL:
s_max = stock_min_max
if s_max < stock_price:
raise FinError(
"Smax must be greater than or equal to the stock price."
)
raise FinError("Smax must be greater than or equal to the stock price.")
elif self.option_type == OptionTypes.EUROPEAN_PUT:
s_min = stock_min_max
if s_min > stock_price:
raise FinError(
"Smin must be less than or equal to the stock price."
)
raise FinError("Smin must be less than or equal to the stock price.")

s_all = get_paths_times(
t_all, s_all = get_paths_times(
num_paths, num_time_steps, t, mu, stock_price, volatility, seed
)

# Due to antithetics we have doubled the number of paths
num_paths = 2 * num_paths
payoff = np.zeros(num_paths)

if option_type == OptionTypes.EUROPEAN_CALL:
Expand Down
23 changes: 6 additions & 17 deletions financepy/products/equity/equity_float_lookback_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,11 @@ def value(
if self.option_type == OptionTypes.EUROPEAN_CALL:
smin = stock_min_max
if smin > s0:
raise FinError(
"Smin must be less than or equal to the stock price."
)
raise FinError("Smin must be less than or equal to the stock price.")
elif self.option_type == OptionTypes.EUROPEAN_PUT:
smax = stock_min_max
if smax < s0:
raise FinError(
"Smax must be greater than or equal to the stock price."
)
raise FinError("Smax must be greater than or equal to the stock price.")

if abs(r - q) < g_small:
q = r + g_small
Expand Down Expand Up @@ -153,9 +149,7 @@ def value(
v = smax * df * N(-b2) - s0 * dq * N(-b1) + s0 * df * u * term

else:
raise FinError(
"Unknown lookback option type:" + str(self.option_type)
)
raise FinError("Unknown lookback option type:" + str(self.option_type))

return v

Expand Down Expand Up @@ -191,22 +185,17 @@ def value_mc(
if self.option_type == OptionTypes.EUROPEAN_CALL:
smin = stock_min_max
if smin > stock_price:
raise FinError(
"Smin must be less than or equal to the stock price."
)
raise FinError("Smin must be less than or equal to the stock price.")
elif self.option_type == OptionTypes.EUROPEAN_PUT:
smax = stock_min_max
if smax < stock_price:
raise FinError(
"Smax must be greater than or equal to the stock price."
)
raise FinError("Smax must be greater than or equal to the stock price.")

s_all = get_paths_times(
t_all, s_all = get_paths_times(
num_paths, num_time_steps, t, mu, stock_price, volatility, seed
)

# Due to antithetics we have doubled the number of paths
num_paths = 2 * num_paths
payoff = np.zeros(num_paths)

if option_type == OptionTypes.EUROPEAN_CALL:
Expand Down
42 changes: 27 additions & 15 deletions unit_tests/test_FinEquityBasketOption.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ def test_homogeneous_call():
dividend_curves.append(dividend_curve)

call_option = EquityBasketOption(
expiry_dt, 100.0, OptionTypes.EUROPEAN_CALL, num_assets)
expiry_dt, 100.0, OptionTypes.EUROPEAN_CALL, num_assets
)
value = call_option.value(
value_dt,
stock_prices,
discount_curve,
dividend_curves,
volatilities,
corr_matrix)
corr_matrix,
)

assert round(value, 4) == 13.6164

Expand All @@ -51,7 +53,8 @@ def test_homogeneous_call():
dividend_curves,
volatilities,
corr_matrix,
num_paths)
num_paths,
)

assert round(value_mc, 4) == 13.5338

Expand All @@ -67,14 +70,16 @@ def test_homogeneous_put():
dividend_curves.append(dividend_curve)

call_option = EquityBasketOption(
expiry_dt, 100.0, OptionTypes.EUROPEAN_PUT, num_assets)
expiry_dt, 100.0, OptionTypes.EUROPEAN_PUT, num_assets
)
value = call_option.value(
value_dt,
stock_prices,
discount_curve,
dividend_curves,
volatilities,
corr_matrix)
corr_matrix,
)

assert round(value, 4) == 9.7344

Expand All @@ -85,9 +90,10 @@ def test_homogeneous_put():
dividend_curves,
volatilities,
corr_matrix,
num_paths)
num_paths,
)

assert round(value_mc, 4) == 9.6986
assert round(value_mc, 2) == 9.69


def test_inhomogeneous_call():
Expand All @@ -101,14 +107,16 @@ def test_inhomogeneous_call():
dividend_curves.append(dividend_curve)

call_option = EquityBasketOption(
expiry_dt, 100.0, OptionTypes.EUROPEAN_CALL, num_assets)
expiry_dt, 100.0, OptionTypes.EUROPEAN_CALL, num_assets
)
value = call_option.value(
value_dt,
stock_prices,
discount_curve,
dividend_curves,
volatilities,
corr_matrix)
corr_matrix,
)

assert round(value, 4) == 13.6783

Expand All @@ -119,9 +127,10 @@ def test_inhomogeneous_call():
dividend_curves,
volatilities,
corr_matrix,
num_paths)
num_paths,
)

assert round(value_mc, 4) == 13.5460
assert round(value_mc, 4) == 13.5088


def test_inhomogeneous_put():
Expand All @@ -135,14 +144,16 @@ def test_inhomogeneous_put():
dividend_curves.append(dividend_curve)

call_option = EquityBasketOption(
expiry_dt, 100.0, OptionTypes.EUROPEAN_PUT, num_assets)
expiry_dt, 100.0, OptionTypes.EUROPEAN_PUT, num_assets
)
value = call_option.value(
value_dt,
stock_prices,
discount_curve,
dividend_curves,
volatilities,
corr_matrix)
corr_matrix,
)

assert round(value, 4) == 7.9126

Expand All @@ -153,6 +164,7 @@ def test_inhomogeneous_put():
dividend_curves,
volatilities,
corr_matrix,
num_paths)
num_paths,
)

assert round(value_mc, 4) == 7.8216
assert round(value_mc, 4) == 7.8185
Loading

0 comments on commit c42ebbc

Please sign in to comment.