avhz / RustQuant

Rust library for quantitative finance.
https://docs.rs/RustQuant
Apache License 2.0
969 stars 105 forks source link

Finite difference pricer adjustments #221

Closed yfnaji closed 1 week ago

yfnaji commented 2 months ago

This branch is still a work in progress

This PR is for addressing the issues raised by @avhz in this comment - the code in question was initially merged from this PR to address issue 98.

There were some errors in implementation, namely:

Although the above amendments have been made, there are still issues with convergence in some places. For brevity, I have limited the cases to European call options.

The cases below were run with the following configuration:

strike_price:  10.0;
risk_free_rate:  0.05;
volatility: 0.5;
evaluation_date: Some(date!(2024 - 01 - 01));
expiration_date: date!(2025 - 01 - 01);
time_steps: 10000; // increased 10-fold for stability - see note below
price_steps: 100;
type_flag: TypeFlag::Call;
exercise_flag: ExerciseFlag::European;

with a distinct value for initial_price in each case:

At the money

initial_price: 10.0 Target: 2.179260421286683 Method Value Error
Explicit 2.1752124177257035 0.004048003560979563
Implicit 2.1751386801265697 0.004121741160113324
Crank-Nicolson 2.175175548933553 0.004084872353129931

In the money

initial_price: 15.0 Target: 6.064426504411616

Method Value Error
Explicit 6.064451145206732 0.00002464079511632633
Implicit 6.06441080128941 0.00001570312220611214
Crank-Nicolson 6.064430972254538 0.000004467842922295517

Out the money

initial_price: 1.0 Target: 1.0140475395001201e-6

Method Value Error
Explicit -1.0119200252625027 1.0119210393100422
Implicit -1.011847275245909 1.0118482892934484
Crank-Nicolson -1.011883645221771 1.0118846592693105

There is some level of convergence for the at/in the money cases, although I think there is room for improvement for accuracy. Whilst experimenting with different time step sizes, it seems that there is an issue with consistency i.e. $\Delta t \rightarrow 0 \Rightarrow \text{Error}\rightarrow 0$ - this suggests there is still an issue with implementation - Note that there is convergence, but just not to the target value. The out the money case further confirms that there is something wrong.

Stability issues

It was noticed that explicit() was blowing up in some configurations. It is likely that this indicates an issue with stability, which can be resolved by having a larger time steps, which in turn provides smaller $\Delta t$. Hence, in the config above, I changed the time step from 1000 to 10000 (though 5000 suffices for stability).

avhz commented 3 weeks ago

Hi, is this still a work in progress?

yfnaji commented 3 weeks ago

Hi @avhz - yes this is still a work in progress, I will push something out before the end of this week. I have made some changes to the methodology and code which I will document in this PR.

avhz commented 3 weeks ago

Just checking, thanks for the update !

codecov[bot] commented 2 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 57.62%. Comparing base (f14fa18) to head (b0471f2). Report is 65 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #221 +/- ## ========================================== + Coverage 56.09% 57.62% +1.52% ========================================== Files 148 154 +6 Lines 4929 5170 +241 ========================================== + Hits 2765 2979 +214 - Misses 2164 2191 +27 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.