Skip to content

Commit

Permalink
Improved var breakdown calc
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Apr 6, 2022
1 parent ddeaf10 commit 12f892c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Qwack.Core/Cubes/ResultCube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResultCube(TO_ResultCube transportObject)

public void Initialize(Dictionary<string, Type> dataTypes)
{
_types = dataTypes;
_types = new Dictionary<string,Type>(dataTypes);
_fieldNames = dataTypes.Keys.ToList();
_rows = new List<ResultCubeRow>();
}
Expand Down
31 changes: 25 additions & 6 deletions src/Qwack.Models/Risk/VaRCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.Models.Models;
Expand Down Expand Up @@ -190,20 +191,38 @@ public void CalculateModels()
}
public (double VaR, DateTime ScenarioDate) CalculateVaR(double ci, Currency ccy, string[] excludeTradeIds)
{
var pf = _portfolio.Clone();
pf.Instruments.RemoveAll(i => excludeTradeIds.Contains(i.TradeId));
return CalculateVaR(ci, ccy, pf);
if (!_resultsCache.Any())
{
var pf = _portfolio.Clone();
pf.Instruments.RemoveAll(i => excludeTradeIds.Contains(i.TradeId));
return CalculateVaR(ci, ccy, pf);
}
else
{
var filterDict = excludeTradeIds.Select(x => new KeyValuePair<string, object>("TradeId", (object)x)).ToList();
var results = _resultsCache.ToDictionary(x => x.Key, x => x.Value.Filter(filterDict, true).SumOfAllRows);
var sortedResults = results.OrderBy(kv => kv.Value).ToList();
var ixCi = (int)System.Math.Floor(sortedResults.Count() * (1.0 - ci));
var ciResult = sortedResults[System.Math.Min(System.Math.Max(ixCi, 0), sortedResults.Count - 1)];
var basePvForSet = _basePvCube.Filter(filterDict, true).SumOfAllRows;
return (ciResult.Value - basePvForSet, ciResult.Key);
}
}

public (double VaR, DateTime ScenarioDate) CalculateVaR(double ci, Currency ccy) => CalculateVaR(ci, ccy, _portfolio);

private readonly ConcurrentDictionary<DateTime, ICube> _resultsCache = new();
private ICube _basePvCube;
public (double VaR, DateTime ScenarioDate) CalculateVaR(double ci, Currency ccy, Portfolio pf, bool parallelize=true)
{
var basePv = pf.PV(_model, ccy).SumOfAllRows;

_basePvCube = pf.PV(_model, ccy);
var basePv = _basePvCube.SumOfAllRows;
_resultsCache.Clear();
var results = new ConcurrentDictionary<DateTime, double>();
var varFunc = new Action<DateTime, IAssetFxModel>((d, m) => {
var scenarioPv = pf.PV(m, ccy, false).SumOfAllRows;
var cube = pf.PV(m, ccy, false);
var scenarioPv = cube.SumOfAllRows;
_resultsCache[d] = cube;
results[d] = scenarioPv - basePv;
});
if (parallelize)
Expand Down

0 comments on commit 12f892c

Please sign in to comment.