Skip to content

Commit efe36a6

Browse files
committed
Add docstrings everywhere
1 parent 009ee8a commit efe36a6

File tree

5 files changed

+113
-20
lines changed

5 files changed

+113
-20
lines changed

src/delta_methods.jl

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
1-
"""A method for delta calculation"""
1+
"""A method for delta calculation."""
22
abstract type AbstractDeltaMethod end
33

4-
"""A method for delta calculation analytically using black scholes"""
4+
"""A method for delta calculation analytically using the Black-Scholes model."""
55
struct BlackScholesAnalyticalDelta <: AbstractDeltaMethod end
66

7-
"""Delta calculator"""
7+
"""A delta calculator that computes the sensitivity of an option's price to changes in the underlying asset's price.
8+
9+
# Type Parameters
10+
- `D <: AbstractDeltaMethod`: The method used for delta calculation.
11+
- `P <: AbstractPayoff`: The type of payoff being priced.
12+
- `I <: AbstractMarketInputs`: The type of market data inputs required for pricing.
13+
- `S <: AbstractPricingMethod`: The pricing method used.
14+
15+
# Fields
16+
- `pricer::Pricer{P, I, S}`: The pricer used to compute the option price.
17+
- `deltaMethod::D`: The delta calculation method.
18+
19+
A `DeltaCalculator` is a callable struct that computes delta using the specified method.
20+
"""
821
struct DeltaCalculator{D<:AbstractDeltaMethod, P<:AbstractPayoff, I<:AbstractMarketInputs, S<:AbstractPricingMethod}
922
pricer::Pricer{P, I, S}
1023
deltaMethod::D
1124
end
1225

13-
"""Callable struct: Computes delta when called, using black scholes on a call."""
26+
"""Computes delta analytically for a vanilla European call option using the Black-Scholes model.
27+
28+
# Arguments
29+
- `delta_calc::DeltaCalculator{BlackScholesAnalyticalDelta, VanillaEuropeanCall, BlackScholesInputs, BlackScholesMethod}`:
30+
A `DeltaCalculator` configured for Black-Scholes analytical delta calculation.
31+
32+
# Returns
33+
- The computed delta value.
34+
35+
The Black-Scholes delta formula for a call option is:
36+
```
37+
d1 = (log(S / K) + (r + 0.5 * σ^2) * T) / (σ * sqrt(T))
38+
delta = Φ(d1)
39+
```
40+
where `Φ` is the CDF of the standard normal distribution.
41+
"""
1442
function (delta_calc::DeltaCalculator{BlackScholesAnalyticalDelta, VanillaEuropeanCall, BlackScholesInputs, BlackScholesMethod})()
1543
S = delta_calc.pricer.marketInputs.spot
1644
K = delta_calc.pricer.payoff.strike
@@ -21,10 +49,20 @@ function (delta_calc::DeltaCalculator{BlackScholesAnalyticalDelta, VanillaEurope
2149
return cdf(Normal(), d1) # Black-Scholes delta for calls
2250
end
2351

24-
"""Delta with AD"""
52+
"""A method for computing delta using automatic differentiation (AD)."""
2553
struct ADDelta <: AbstractDeltaMethod end
2654

27-
"""Delta with AD callable"""
55+
"""Computes delta using automatic differentiation (AD).
56+
57+
# Arguments
58+
- `delta_calc::DeltaCalculator{ADDelta, P, BlackScholesInputs, S}`:
59+
A `DeltaCalculator` configured for AD-based delta calculation.
60+
61+
# Returns
62+
- The computed delta value using AD.
63+
64+
This method uses `ForwardDiff.derivative` to compute the delta by differentiating the option price with respect to the spot price.
65+
"""
2866
function (delta_calc::DeltaCalculator{ADDelta, P, BlackScholesInputs, S})() where {P,S}
2967
pricer = delta_calc.pricer
3068
return ForwardDiff.derivative(

src/market_inputs.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
"""market data inputs for pricers"""
1+
"""An abstract type representing market data inputs required for pricers."""
22
abstract type AbstractMarketInputs end
33

4-
"""Inputs for black scholes model"""
4+
"""Market data inputs for the Black-Scholes model.
5+
6+
# Fields
7+
- `rate`: The risk-free interest rate.
8+
- `spot`: The current spot price of the underlying asset.
9+
- `sigma`: The volatility of the underlying asset.
10+
11+
This struct encapsulates the necessary inputs for pricing derivatives under the Black-Scholes model.
12+
"""
513
struct BlackScholesInputs <: AbstractMarketInputs
614
rate
715
spot
816
sigma
9-
end
17+
end

src/payoffs.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
"""A payoff, such as a vanilla european option or an asian option or a forward."""
1+
"""An abstract type representing a financial payoff, such as a vanilla European option, an Asian option, or a forward."""
22
abstract type AbstractPayoff end
33

4-
"""vanilla european call payoff"""
4+
"""A vanilla European call option payoff.
5+
6+
# Fields
7+
- `strike`: The strike price of the option.
8+
- `time`: The time to maturity of the option.
9+
10+
This struct represents a European call option, which provides a payoff of `max(spot - strike, 0.0)`.
11+
"""
512
struct VanillaEuropeanCall <: AbstractPayoff
613
strike
714
time
815
end
916

10-
"""vanilla european option callable to get the payoff given a spot price."""
17+
"""Computes the payoff of a vanilla European call option given a spot price.
18+
19+
# Arguments
20+
- `payoff::VanillaEuropeanCall`: The call option payoff structure.
21+
- `spot`: The current spot price of the underlying asset.
22+
23+
# Returns
24+
- The payoff value, calculated as `max(spot - payoff.strike, 0.0)`.
25+
"""
1126
function (payoff::VanillaEuropeanCall)(spot)
1227
return max(spot - payoff.strike, 0.0)
13-
end
28+
end

src/pricing_methods.jl

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
1-
"""A pricing method"""
1+
"""An abstract type representing a pricing method."""
22
abstract type AbstractPricingMethod end
33

4-
"""Black scholes method"""
4+
"""The Black-Scholes pricing method.
5+
6+
This struct represents the Black-Scholes pricing model for option pricing.
7+
"""
58
struct BlackScholesMethod <: AbstractPricingMethod end
69

7-
"""# the whole algorithm to price a specific derivative, using specific market inputs and a pricing method.
8-
# it should be a callable made up of all the ingredients: a payoff, market data, a pricing model"""
9-
struct Pricer{P <: AbstractPayoff, M <: AbstractMarketInputs, S<:AbstractPricingMethod}
10+
"""A pricer that calculates the price of a derivative using a given payoff, market data, and a pricing model.
11+
12+
# Type Parameters
13+
- `P <: AbstractPayoff`: The type of payoff being priced.
14+
- `M <: AbstractMarketInputs`: The type of market data inputs required for pricing.
15+
- `S <: AbstractPricingMethod`: The pricing method used.
16+
17+
# Fields
18+
- `marketInputs::M`: The market data inputs used for pricing.
19+
- `payoff::P`: The derivative payoff.
20+
- `pricingMethod::S`: The pricing model used for valuation.
21+
22+
A `Pricer` is a callable struct that computes the price of the derivative using the specified pricing method.
23+
"""
24+
struct Pricer{P <: AbstractPayoff, M <: AbstractMarketInputs, S <: AbstractPricingMethod}
1025
marketInputs::M
1126
payoff::P
1227
pricingMethod::S
1328
end
1429

15-
"""Dispatch of pricer for call black scholes pricing"""
16-
function (pricer::Pricer{VanillaEuropeanCall, BlackScholesInputs, BlackScholesMethod})()
30+
"""Computes the price of a vanilla European call option using the Black-Scholes model.
31+
32+
# Arguments
33+
- `pricer::Pricer{VanillaEuropeanCall, BlackScholesInputs, BlackScholesMethod}`:
34+
A `Pricer` configured for Black-Scholes pricing of a vanilla European call.
35+
36+
# Returns
37+
- The computed Black-Scholes price of the option.
38+
39+
The Black-Scholes formula used is:
40+
```
41+
d1 = (log(S / K) + (r + 0.5 * σ^2) * T) / (σ * sqrt(T))
42+
d2 = d1 - σ * sqrt(T)
43+
price = S * Φ(d1) - K * exp(-r * T) * Φ(d2)
44+
```
45+
where `Φ` is the CDF of the standard normal distribution.
46+
"""
47+
function (pricer::Pricer{VanillaEuropeanCall, BlackScholesInputs, BlackScholesMethod})()
1748
S = pricer.marketInputs.spot
1849
K = pricer.payoff.strike
1950
r = pricer.marketInputs.rate

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ using Test
44
@testset "Hedgehog2.jl" begin
55
# Write your tests here.
66
end
7+

0 commit comments

Comments
 (0)