Hi all,
I am trying to calculate the implied volatility of this problem:
>> T = 10;
>> CDS_bps = 337.5;
>> sigma_start = 1;
>> r = 0.03;
>> R_rel = 0.4
sigma_opt(sigma_start, CDS_bps, R_rel, T, r);
This is the function I wrote:
function [sigma,fval] = sigma_opt(sigma_start, CDS_bps, R_rel, T, r)
[sigma,fval] = fsolve(@nested_sigma, sigma_start);
% Nested function that computes the objective function
function prices = nested_sigma(sigma)
Lambda = CDS_bps/10000/(1-R_rel);
Ts = [1:T];
Default_probs = [0;1- exp(-Lambda .* Ts')];
partial_Nominator = zeros(1, T+1);
for i = 1:T
partial_Nominator(i) = (1-R_rel)*exp(-r*i)*(Default_probs(i+1) - ...
Default_probs(i));
end
Nominator = sum(partial_Nominator,2);
prices(1) = Nominator/(1-R_rel);
d1 = (log(100/5)+(r+sigma^2/2*T))/(sigma*sqrt(T));
d2 = d1 - sigma *sqrt(T);
prices(2) = exp(-r*T) * normcdf(-d2);
end
end
and this is the error I get:
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
default value of the function tolerance.
<stopping criteria details>
fsolve stopped because the sum of squared function values, r, has gradient with relative
norm 6.524979e-19; this is less than 1e-4*options.OptimalityTolerance = 1.000000e-10.
However, r = 1.377972e-01, exceeds sqrt(options.FunctionTolerance) = 1.000000e-03.
Optimization Metric Options
norm(grad r) = 6.52e-19 1e-4*OptimalityTolerance = 1e-10 (default)
r = 1.38e-01 sqrt(FunctionTolerance) = 1.0e-03 (default)
I would be very happy if anyone could help me on this.
Thank you.

 Akzeptierte Antwort

Star Strider
Star Strider am 7 Aug. 2019

0 Stimmen

There is actually no error or any other problem. The fsolve function is a root-finder, meaning of course that it search for values whre the function values are zero. If it cannot find the roots, it returns that message.

5 Kommentare

MW
MW am 7 Aug. 2019
Thank you for answering.
I get the following result for sigma in excel: 0,779385279680472. How can I retrieve this result using my formula?
My pleasure.
Excel must be doing something different, probably using a different optimization algorithm. When I run your code, I get:
sigma =
0.141848077754023
fval =
0.371210469413452 0.000000000025851
This is not to say that the MATLAB result is wrong, it just means that fsolve may not be the correct solver. Note that most of tthe solvers require that your objective function return a scalar value, not the (1x2) vector ‘nested_sigma’ returns.
‘I am actually trying to find sigma where price 2 matches price 1. Can you tell me the name of the solver with which such a problem can be solved?’
Solving that is relatively straight forward, and fsolve is ideal in that situation, since you are searching for the ‘sigma’ value where ‘diff(price)’ is zero.
Change your fsolve call to:
[sigma,fval] = fsolve(@(x)diff(nested_sigma(x)), sigma_start);
producing:
sigma =
0.778769515215809
fval =
-1.883382338974116e-12
as desired, and reasonably close to the Excel result.
MW
MW am 7 Aug. 2019
Thank you very much! Have a nice week :)
As always, my pleasure!
You, too!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John D'Errico
John D'Errico am 7 Aug. 2019
Bearbeitet: John D'Errico am 7 Aug. 2019

0 Stimmen

You seem to misunderstand how things work.
You have provided ONE unknown to solve for.
However, there are TWO equations that you are trying to solve. For example, at the start point of sigma==1, we see TWO results.
prices
prices =
0.37121 0.54361
Fsolve is a root finder. It tries to find a value of the unknown variable (here sigma) that reduces BOTH of those results to zero. Of course, no single number for sigma will exist that kills both of the prices off to zero, at the same time. But worse, consider your function. It never even gets to zero for any value of sigma.
I took your function, then varied sigma. So, in this next output, the first column is sigma, the second and third columns are the prices your function computes, as a function of sigma.
[siglist',pricelist]
ans =
0.1 0.37121 1.8361e-21
0.2 0.37121 2.9262e-06
0.3 0.37121 0.0024545
0.4 0.37121 0.029068
0.5 0.37121 0.096828
0.6 0.37121 0.19197
0.7 0.37121 0.2944
0.8 0.37121 0.39075
0.9 0.37121 0.47452
1 0.37121 0.54361
1.1 0.37121 0.59835
1.2 0.37121 0.64032
1.3 0.37121 0.67155
1.4 0.37121 0.69415
1.5 0.37121 0.71009
1.6 0.37121 0.72104
1.7 0.37121 0.72838
1.8 0.37121 0.73317
1.9 0.37121 0.73623
2 0.37121 0.73812
2.1 0.37121 0.73927
2.2 0.37121 0.73995
2.3 0.37121 0.74034
2.4 0.37121 0.74056
2.5 0.37121 0.74069
2.6 0.37121 0.74075
2.7 0.37121 0.74078
2.8 0.37121 0.7408
2.9 0.37121 0.74081
3 0.37121 0.74081
So your start value is 1. I varied sigma from 0.1 to 3. Note that the first price NEVER varies. The second price does not cross zero, although it might if I went lower.
You cannot use fsolve to solve a problem that has no solution.
Worse, you cannot use it to solve an overdetermined problem like this, because it will never satisfy both equatinos at once.

1 Kommentar

MW
MW am 7 Aug. 2019
I am actually trying to find sigma where price 2 matches price 1. Can you tell me the name of the solver with which such a problem can be solved?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Optimization Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by