-
Hi. I'm now trying a project to analyze option data. During the project, I faced with a problem to calculate implied volatility. Here is the part of my code.
But this does not derive any solution. When I check how AD function f is doing, it just calculates 0.0, 5.0, 10.0, 0.0, 5.0, 10.0..... repeatedly until the end of the iteration. What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @luxiant, The internal implementation of the Bisection method in Peroxide is outlined below: I(a, b) => {
let f_ad = ADFn::new(|x| self.f(x));
let x = 0.5 * (a + b);
let fa = f_ad.call_stable(a);
let fx = f_ad.call_stable(x);
let fb = f_ad.call_stable(b);
if (a - b).abs() <= self.tol {
self.find = RootBool::Find;
self.root = x;
} else {
if fa * fx < 0f64 {
self.curr = I(a, x);
} else if fx * fb < 0f64 {
self.curr = I(x, b);
} else if fx == 0f64 {
self.find = RootBool::Find;
self.root = x;
} else {
self.find = RootBool::Error;
}
}
} This algorithm is based on the assumption that the root of If this assumption is not met, the Bisection method may not converge. Consider the following example: use peroxide::fuga::*;
fn main() {
let h = |x: AD| -> AD {
let y = if x.x() < 5.0 { -5.0 } else { 5.0 };
println!("y = {:>2}", y);
AD0(y)
};
let sol = bisection(h, (0.0, 10.0), 5, 0.0001);
match sol {
Ok(x) => println!("{}", x),
Err(_) => println!("No solution found"),
}
} The output of this function is as follows: y = -5 # <- h(0)
y = 5 # <- h(5)
y = 5 # <- h(10)
y = -5
y = -5
y = 5
y = -5
y = -5
y = 5
y = -5
y = -5
y = 5
y = -5
y = -5
y = 5
No solution found The first three values correspond to It seems a similar issue may be occurring in your case. Therefore, it might be necessary to verify that the solution lies within an open interval. |
Beta Was this translation helpful? Give feedback.
Hi @luxiant,
The internal implementation of the Bisection method in Peroxide is outlined below:
This alg…