Skip to content

Commit

Permalink
Risk matrix with time axis
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Apr 26, 2022
1 parent 40b4d99 commit 637aa9d
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions src/Qwack.Models/Risk/RiskMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
using Qwack.Core.Cubes;
using Qwack.Core.Instruments;
using Qwack.Core.Models;
using Qwack.Dates;
using Qwack.Models.Models;
using Qwack.Models.Risk.Mutators;
using Qwack.Transport.BasicTypes;
using Qwack.Utils.Parallel;

namespace Qwack.Models.Risk
{
public class RiskMatrix
{
private readonly ICurrencyProvider _currencyProvider;
private readonly ICalendarProvider _calendar;

public string AssetId { get; private set; }
public Currency Ccy { get; private set; }
Expand All @@ -24,6 +27,7 @@ public class RiskMatrix
public double ShiftSizeAsset { get; private set; }
public double ShiftSizeFx { get; private set; }
public int NScenarios { get; private set; }
public int? NTimeSteps { get; private set; }
public bool ReturnDifferential { get; private set; }

public FxPair Pair1 { get; private set; }
Expand All @@ -44,6 +48,19 @@ public RiskMatrix(string assetId, Currency ccy, MutationType shiftType, RiskMetr
ReturnDifferential = returnDifferential;
}

public RiskMatrix(string assetId, MutationType shiftType, RiskMetric metric, double shiftStepSizeAsset, int nTimeSteps, int nScenarios, ICurrencyProvider currencyProvider, ICalendarProvider calendar, bool returnDifferential = true)
{
AssetId = assetId;
NTimeSteps = nTimeSteps;
ShiftType = shiftType;
Metric = metric;
ShiftSizeAsset = shiftStepSizeAsset;
NScenarios = nScenarios;
_currencyProvider = currencyProvider;
_calendar = calendar;
ReturnDifferential = returnDifferential;
}

public RiskMatrix(FxPair p1, FxPair p2, MutationType shiftType, RiskMetric metric, double shiftStepSize1, double shiftStepSize2, int nScenarios, ICurrencyProvider currencyProvider, bool returnDifferential = true)
{
Pair1 = p1;
Expand All @@ -61,10 +78,15 @@ public RiskMatrix(Currency c1, Currency c2, MutationType shiftType, RiskMetric m
{
}

public Dictionary<Tuple<string,string>, IPvModel> GenerateScenarios(IPvModel model)
public Dictionary<Tuple<string, string>, IPvModel> GenerateScenarios(IPvModel model)
{
if (!string.IsNullOrEmpty(AssetId))
return GenerateScenariosAssetFx(model);
{
if (NTimeSteps.HasValue)
return GenerateScenariosAssetTime(model);
else
return GenerateScenariosAssetFx(model);
}
else
return GenerateScenariosFxFx(model);
}
Expand Down Expand Up @@ -116,6 +138,59 @@ private Dictionary<Tuple<string, string>, IPvModel> GenerateScenariosAssetFx(IPv
return o;
}

private Dictionary<Tuple<string, string>, IPvModel> GenerateScenariosAssetTime(IPvModel model)
{
var o = new Dictionary<Tuple<string, string>, IPvModel>();
var axisLength = NScenarios * 2 + 1;
var results = new KeyValuePair<Tuple<string, string>, IPvModel>[axisLength * NTimeSteps.Value];
ParallelUtils.Instance.For(-NScenarios, NScenarios + 1, 1, (i) =>
{
var thisShiftAsset = i * ShiftSizeAsset;
var thisLabelAsset = AssetId + "~" + thisShiftAsset;
var assetIx = i + NScenarios;
IPvModel shifted;
if (thisShiftAsset == 0)
shifted = model;
else
shifted = ShiftType switch
{
MutationType.FlatShift => FlatShiftMutator.AssetCurveShift(AssetId, thisShiftAsset, model),
_ => throw new Exception($"Unable to process shift type {ShiftType}"),
};
var d = model.VanillaModel.BuildDate;
var pd = d;
for (var iT = 0; iT < NTimeSteps.Value; iT++)
{
var thisLabelTime = $"+{iT} days";
IPvModel shiftedTime;
if (thisShiftAsset == 0)
shiftedTime = shifted;
else
{
d = d.AddPeriod(RollType.F, _calendar.GetCalendarSafe("USD"), iT.Bd());
shiftedTime = shifted.VanillaModel.Clone().RollModel(d, _currencyProvider);
shiftedTime = shifted.Rebuild(shiftedTime.VanillaModel, model.Portfolio.RollWithLifecycle(d, pd));
pd = d;
}
results[assetIx * NTimeSteps.Value + iT] = new KeyValuePair<Tuple<string, string>, IPvModel>(
new Tuple<string, string>(thisLabelAsset, thisLabelTime), shiftedTime);
}
}).Wait();

foreach (var kv in results)
o.Add(kv.Key, kv.Value);

return o;
}


private Dictionary<Tuple<string, string>, IPvModel> GenerateScenariosFxFx(IPvModel model)
{
var o = new Dictionary<Tuple<string, string>, IPvModel>();
Expand Down

0 comments on commit 637aa9d

Please sign in to comment.