Skip to content

Commit

Permalink
VaR tweaks for IPvModel
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Jul 16, 2024
1 parent a729ab8 commit 02f5f4e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
20 changes: 8 additions & 12 deletions src/Qwack.Models/Risk/VaR/VaRCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using System.Buffers.Text;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Qwack.Core.Basic;
using Qwack.Core.Cubes;
using Qwack.Core.Instruments;
using Qwack.Core.Models;
using Qwack.Math;
using Qwack.Math.Interpolation;
using Qwack.Models.Models;
using Qwack.Models.Risk.Mutators;
using Qwack.Transport.Results;
using Qwack.Utils.Parallel;
Expand All @@ -20,17 +16,17 @@ namespace Qwack.Models.Risk.VaR
{
public class VaRCalculator
{
private readonly IAssetFxModel _model;
private readonly IPvModel _model;
private readonly Portfolio _portfolio;
private readonly ILogger _logger;
private readonly Dictionary<string, VaRSpotScenarios> _spotTypeBumps = new();
private readonly Dictionary<string, VaRSpotScenarios> _spotFxTypeBumps = new();
private readonly Dictionary<string, VaRCurveScenarios> _curveTypeBumps = new();
private readonly Dictionary<string, VaRCurveScenarios> _surfaceTypeBumps = new();
private readonly Dictionary<DateTime, IAssetFxModel> _bumpedModels = new();
private readonly Dictionary<DateTime, IPvModel> _bumpedModels = new();
private VaREngine _varEngine;

public VaRCalculator(IAssetFxModel model, Portfolio portfolio, ILogger logger)
public VaRCalculator(IPvModel model, Portfolio portfolio, ILogger logger)
{
_model = model;
_portfolio = portfolio;
Expand Down Expand Up @@ -127,7 +123,7 @@ public void AddSurfaceAtmAbsoluteScenarioBumps(string assetId, Dictionary<DateTi

public void CalculateModels()
{
var allAssetIds = _model.CurveNames.Where(x => !(x.Length == 7 && x[3] == '/')).ToArray(); // _portfolio.AssetIds().Where(x => !(x.Length == 7 && x[3] == '/')).ToArray();
var allAssetIds = _model.VanillaModel.CurveNames.Where(x => !(x.Length == 7 && x[3] == '/')).ToArray(); // _portfolio.AssetIds().Where(x => !(x.Length == 7 && x[3] == '/')).ToArray();
var allDatesSet = new HashSet<DateTime>();

if (_spotTypeBumps.Any())
Expand Down Expand Up @@ -158,15 +154,15 @@ public void CalculateModels()

_logger?.LogInformation($"Total of {allDates.Count} dates");

ConcurrentDictionary<DateTime, IAssetFxModel> bumpedModels = new();
ConcurrentDictionary<DateTime, IPvModel> bumpedModels = new();

ParallelUtils.Instance.Foreach(allDates, d =>
{
//if (d != new DateTime(2021, 08, 02))
// return;
_logger?.LogDebug($"Computing scenarios for {d}");
var m = _model.Clone();
var m = _model.VanillaModel.Clone();
foreach (var assetId in allAssetIds)
{
Expand Down Expand Up @@ -198,7 +194,7 @@ public void CalculateModels()
}
}
foreach (var ccy in m.FundingModel.FxMatrix.SpotRates.Keys)
foreach (var ccy in m.VanillaModel.FundingModel.FxMatrix.SpotRates.Keys)
{
if (_spotFxTypeBumps.TryGetValue(ccy, out var spotBumpRecord))
{
Expand All @@ -208,7 +204,7 @@ public void CalculateModels()
// m = FlatShiftMutator.AssetCurveShift(assetId, spotBumpRecord.Bumps[d], m);
}
}
bumpedModels[d] = m;
bumpedModels[d] = _model.Rebuild(m, _portfolio);
}).Wait();

foreach (var kv in bumpedModels)
Expand Down
10 changes: 9 additions & 1 deletion src/Qwack.Models/Risk/VaR/VaREngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Qwack.Core.Models;
using Qwack.Math;
using Qwack.Math.Interpolation;
using Qwack.Models.Models;
using Qwack.Transport.Results;

namespace Qwack.Models.Risk
Expand Down Expand Up @@ -43,6 +42,15 @@ public VaREngine(ILogger logger, IPvModel baseModel, Portfolio portfolio, Dictio
public Dictionary<string, ICube> ResultsCache => _resultsCache.ToDictionary(x => x.Key, x => x.Value);
public ICube BasePvCube => _basePvCube;

public void SeedResults(ICube basePvCube, Dictionary<string, ICube> bumpedPvCubes)
{
_basePvCube = basePvCube;
foreach(var kv in bumpedPvCubes)
{
_resultsCache[kv.Key] = kv.Value;
}
}

public (double VaR, string ScenarioId, double cVaR) CalculateVaR(double ci, Currency ccy, string[] excludeTradeIds)
{
if (!_resultsCache.Any())
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>0.8.62</VersionPrefix>
<VersionPrefix>0.8.63</VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.62
0.8.63

0 comments on commit 02f5f4e

Please sign in to comment.