Skip to content

Commit

Permalink
Fix up
Browse files Browse the repository at this point in the history
  • Loading branch information
Drawaes committed Sep 18, 2024
1 parent 19c3b56 commit d002b19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/Qwack.Math/Solvers/Brent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace Qwack.Math.Solvers
{
Expand All @@ -10,6 +11,12 @@ namespace Qwack.Math.Solvers

public static class Brent
{
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowInvalidOperation(double fb)
{
throw new Exception($"Error is {fb}");
}

public static double BrentsMethodSolve(Func<double, double> function, double lowerLimit, double upperLimit, double errorTol)
{
var a = lowerLimit;
Expand Down Expand Up @@ -79,7 +86,7 @@ public static double BrentsMethodSolve(Func<double, double> function, double low
{ var tmp = a; a = b; b = tmp; tmp = fa; fa = fb; fb = tmp; }
i++;
if (i > 1000)
throw new Exception($"Error is {fb}");
ThrowInvalidOperation(fb);
}
return b;
}
Expand Down
22 changes: 11 additions & 11 deletions src/Qwack.Options/VolSurfaces/GridVolSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace Qwack.Options.VolSurfaces
public class GridVolSurface : IVolSurface, IATMVolSurface
{
private readonly bool _allowCaching = true;
private readonly ConcurrentDictionary<string, double> _absVolCache = new();
private readonly ConcurrentDictionary<string, double> _deltaVolCache = new();
//private readonly ConcurrentDictionary<string, double> _absVolCache = new();
//private readonly ConcurrentDictionary<string, double> _deltaVolCache = new();
public Frequency OverrideSpotLag { get; set; }
public string Name { get; set; }
public DateTime OriginDate { get; set; }
Expand Down Expand Up @@ -85,10 +85,10 @@ public void Build(DateTime originDate, double[] strikes, DateTime[] expiries, do

public double GetVolForAbsoluteStrike(double strike, double maturity, double forward)
{
var key = $"{strike:f6}~{maturity:f3}~{forward:f6}";
/*var key = $"{strike:f6}~{maturity:f3}~{forward:f6}";
if (_allowCaching && _absVolCache.TryGetValue(key, out var vol))
return vol;

return vol;*/
double vol;
if (StrikeType == StrikeType.Absolute)
{
var interpForStrike = InterpolatorFactory.GetInterpolator(ExpiriesDouble,
Expand Down Expand Up @@ -129,7 +129,7 @@ public double GetVolForAbsoluteStrike(double strike, double maturity, double for
vol = interpForSolvedStrike.Interpolate(maturity);
}

if (_allowCaching) _absVolCache[key] = vol;
//if (_allowCaching) _absVolCache[key] = vol;
return vol;
}

Expand All @@ -148,10 +148,10 @@ public double GetVolForDeltaStrike(double deltaStrike, double maturity, double f
if (deltaStrike is > 1.0 or < (-1.0))
throw new ArgumentOutOfRangeException($"Delta strike must be in range -1.0 < x < 1.0 - value was {deltaStrike}");

var key = $"{deltaStrike:f6}~{maturity:f3}~{forward:f6}";
if (_allowCaching && _deltaVolCache.TryGetValue(key, out var vol))
return vol;

//var key = $"{deltaStrike:f6}~{maturity:f3}~{forward:f6}";
//if (_allowCaching && _deltaVolCache.TryGetValue(key, out var vol))
// return vol;
double vol;
if (StrikeType == StrikeType.ForwardDelta)
{
var interpForStrike = InterpolatorFactory.GetInterpolator(ExpiriesDouble,
Expand Down Expand Up @@ -180,7 +180,7 @@ public double GetVolForDeltaStrike(double deltaStrike, double maturity, double f
vol = interpForSolvedStrike.Interpolate(maturity);
}

if (_allowCaching) _deltaVolCache[key] = vol;
//if (_allowCaching) _deltaVolCache[key] = vol;
return vol;
}

Expand Down

0 comments on commit d002b19

Please sign in to comment.