Used ChatGPT to fix some issues with the VaR
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MarketData.ValueAtRisk
|
||||
{
|
||||
@@ -9,15 +6,16 @@ namespace MarketData.ValueAtRisk
|
||||
{
|
||||
public double Value { get; private set; }
|
||||
public T Item { get; private set; }
|
||||
public BinResult()
|
||||
{
|
||||
}
|
||||
|
||||
public BinResult() { }
|
||||
|
||||
public BinResult(double value, T item)
|
||||
{
|
||||
this.Value = value;
|
||||
this.Item = item;
|
||||
}
|
||||
}
|
||||
|
||||
public class BinManager<T>
|
||||
{
|
||||
private List<BinItem<T>> binItems;
|
||||
@@ -29,11 +27,13 @@ namespace MarketData.ValueAtRisk
|
||||
this.binCount = binCount;
|
||||
InitializeBins();
|
||||
}
|
||||
|
||||
private void InitializeBins()
|
||||
{
|
||||
this.binItems = new List<BinItem<T>>();
|
||||
this.samples = 0;
|
||||
}
|
||||
|
||||
public BinResult<T> GetVaRReturn(double[] values, List<T> items, double percentile)
|
||||
{
|
||||
double minValue = float.NaN;
|
||||
@@ -43,30 +43,42 @@ namespace MarketData.ValueAtRisk
|
||||
|
||||
InitializeBins();
|
||||
GetMinMax(ref minValue, ref maxValue, values);
|
||||
|
||||
binSize = (maxValue - minValue) / (double)binCount;
|
||||
for (double value = minValue; value < maxValue; value += binSize)
|
||||
if (binSize == 0) binSize = 1e-10;
|
||||
|
||||
// Create bins
|
||||
for (double value = minValue; value <= maxValue; value += binSize)
|
||||
{
|
||||
binItems.Add(new BinItem<T>(value));
|
||||
}
|
||||
|
||||
// Add values and objects to bins
|
||||
for (int index = 0; index < values.Length; index++)
|
||||
{
|
||||
AddValueToBin(values[index]);
|
||||
AddObjectToBin(values[index], items[index]);
|
||||
}
|
||||
double samplesAtRank = samples-(samples * (percentile/100.00));
|
||||
|
||||
double samplesAtRank = samples - (samples * (percentile / 100.0));
|
||||
if (samplesAtRank < 1) samplesAtRank = 1;
|
||||
|
||||
long samplesInRank = 0;
|
||||
double percentVaR = 0;
|
||||
|
||||
for (int index = 0; index < binItems.Count; index++)
|
||||
{
|
||||
BinItem<T> binItem = binItems[index];
|
||||
samplesInRank += binItem.BinCount;
|
||||
percentVaR = binItem.BinValue;
|
||||
item = binItem.BinObject;
|
||||
|
||||
if (samplesInRank > samplesAtRank) break;
|
||||
}
|
||||
|
||||
return new BinResult<T>(percentVaR, item);
|
||||
}
|
||||
|
||||
public double GetVaRReturn(double[] values, double percentile)
|
||||
{
|
||||
double minValue = float.NaN;
|
||||
@@ -75,46 +87,62 @@ namespace MarketData.ValueAtRisk
|
||||
|
||||
InitializeBins();
|
||||
GetMinMax(ref minValue, ref maxValue, values);
|
||||
|
||||
binSize = (maxValue - minValue) / (double)binCount;
|
||||
for (double value = minValue; value < maxValue; value += binSize)
|
||||
if (binSize == 0) binSize = 1e-10;
|
||||
|
||||
// Create bins
|
||||
for (double value = minValue; value <= maxValue; value += binSize)
|
||||
{
|
||||
binItems.Add(new BinItem<T>(value));
|
||||
}
|
||||
|
||||
// Add values to bins
|
||||
for (int index = 0; index < values.Length; index++)
|
||||
{
|
||||
AddValueToBin(values[index]);
|
||||
}
|
||||
double samplesAtRank = samples-(samples * (percentile/100.00));
|
||||
|
||||
double samplesAtRank = samples - (samples * (percentile / 100.0));
|
||||
if (samplesAtRank < 1) samplesAtRank = 1;
|
||||
|
||||
long samplesInRank = 0;
|
||||
double percentVaR = 0;
|
||||
|
||||
for (int index = 0; index < binItems.Count; index++)
|
||||
{
|
||||
BinItem<T> binItem = binItems[index];
|
||||
samplesInRank += binItem.BinCount;
|
||||
percentVaR = binItem.BinValue;
|
||||
|
||||
if (samplesInRank > samplesAtRank) break;
|
||||
}
|
||||
|
||||
return percentVaR;
|
||||
}
|
||||
|
||||
private static void GetMinMax(ref double minValue, ref double maxValue, double[] values)
|
||||
{
|
||||
for (int index = 0; index < values.Length; index++)
|
||||
{
|
||||
double value = values[index];
|
||||
if (0 == index)
|
||||
|
||||
if (index == 0)
|
||||
{
|
||||
minValue = maxValue = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value > maxValue) maxValue = value;
|
||||
if (value < minValue) minValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddValueToBin(double value)
|
||||
{
|
||||
bool added = false;
|
||||
samples++;
|
||||
|
||||
for (int index = 0; index < binItems.Count; index++)
|
||||
{
|
||||
BinItem<T> binItem = binItems[index];
|
||||
@@ -125,12 +153,15 @@ namespace MarketData.ValueAtRisk
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (false == added && binItems.Count>0) binItems[binItems.Count - 1].BinCount++;
|
||||
|
||||
if (!added && binItems.Count > 0)
|
||||
binItems[binItems.Count - 1].BinCount++;
|
||||
}
|
||||
|
||||
private void AddObjectToBin(double value, T item)
|
||||
{
|
||||
bool added = false;
|
||||
// samples++;
|
||||
|
||||
for (int index = 0; index < binItems.Count; index++)
|
||||
{
|
||||
BinItem<T> binItem = binItems[index];
|
||||
@@ -141,7 +172,9 @@ namespace MarketData.ValueAtRisk
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (false == added && binItems.Count>0) binItems[binItems.Count - 1].BinObject=item;
|
||||
|
||||
if (!added && binItems.Count > 0)
|
||||
binItems[binItems.Count - 1].BinObject = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MarketData.DataAccess;
|
||||
using MarketData.Utils;
|
||||
|
||||
namespace MarketData.ValueAtRisk
|
||||
{
|
||||
@@ -11,81 +7,123 @@ namespace MarketData.ValueAtRisk
|
||||
{
|
||||
private int returnDays = 1;
|
||||
|
||||
private HistoricalVaR()
|
||||
{
|
||||
}
|
||||
private HistoricalVaR() { }
|
||||
|
||||
public static VaRResult GetVaR(PortfolioHoldings portfolioHoldings, double percentile, int returnDays = 1)
|
||||
{
|
||||
VaRResult varResult = new VaRResult();
|
||||
if (null == portfolioHoldings || 0 == portfolioHoldings.Count)
|
||||
|
||||
// Validate portfolio
|
||||
if (portfolioHoldings == null || portfolioHoldings.Count == 0)
|
||||
{
|
||||
varResult.Success = false;
|
||||
varResult.Message = "Portfolio is null or empty.";
|
||||
return varResult;
|
||||
}
|
||||
// This ensures that the pricing information for each holding is symmetric.
|
||||
// The piece of code that checks for jagged pricing should be handled differently. For example, we should be able to continue the VaR analysis and simply account for no exposure to the
|
||||
// given symbol in the event of a lack of pricing data. This would handle the current issue with securities that have been trading for less than the number of observation days.
|
||||
if (portfolioHoldings.Count > 1)
|
||||
|
||||
// Determine the minimum common price history across holdings
|
||||
int minPriceCount = int.MaxValue;
|
||||
for (int i = 0; i < portfolioHoldings.Count; i++)
|
||||
{
|
||||
int priceCount=-1;
|
||||
for (int index = 1; index < portfolioHoldings.Count; index++)
|
||||
{
|
||||
if(portfolioHoldings[index].Prices.Count>priceCount)priceCount=portfolioHoldings[index].Prices.Count;
|
||||
int count = portfolioHoldings[i].Prices.Count;
|
||||
if (count < minPriceCount)
|
||||
minPriceCount = count;
|
||||
}
|
||||
for (int index = 1; index < portfolioHoldings.Count; index++)
|
||||
{
|
||||
if (portfolioHoldings[index].Prices.Count != priceCount)
|
||||
|
||||
// Enforce minimum observation requirement
|
||||
if (minPriceCount < 30) // or whatever threshold you prefer
|
||||
{
|
||||
varResult.Success = false;
|
||||
varResult.Message=String.Format("Insufficient price history for {0}. {1}/{2}",portfolioHoldings[index].Symbol,portfolioHoldings[index].Prices.Count,priceCount);
|
||||
varResult.Message = $"Insufficient common price history ({minPriceCount} observations).";
|
||||
return varResult;
|
||||
}
|
||||
|
||||
// Truncate all holdings to the common window
|
||||
for (int i = 0; i < portfolioHoldings.Count; i++)
|
||||
{
|
||||
if (portfolioHoldings[i].Prices.Count > minPriceCount)
|
||||
{
|
||||
portfolioHoldings[i].Prices = new MarketDataModel.Prices(
|
||||
portfolioHoldings[i].Prices
|
||||
.Skip(portfolioHoldings[i].Prices.Count - minPriceCount)
|
||||
.ToList()
|
||||
);
|
||||
}
|
||||
}
|
||||
// Calculate total market value and then calculate the weightings of each holding
|
||||
|
||||
// Calculate total market value and holdings weightings
|
||||
double marketValue = portfolioHoldings.GetMarketValue();
|
||||
if (marketValue == 0)
|
||||
{
|
||||
varResult.Success = false;
|
||||
varResult.Message = "Portfolio market value is zero.";
|
||||
return varResult;
|
||||
}
|
||||
|
||||
for (int index = 0; index < portfolioHoldings.Count; index++)
|
||||
{
|
||||
PortfolioHolding portfolioHolding = portfolioHoldings[index];
|
||||
portfolioHolding.Weight = portfolioHolding.MarketValue / marketValue;
|
||||
}
|
||||
// Calculate the weighted returns for the observation period
|
||||
|
||||
// Calculate weighted returns for the observation period
|
||||
portfolioHoldings.SetReturnDays(returnDays);
|
||||
int numReturns=portfolioHoldings[0].Returns.Length;
|
||||
WeightedReturnsWithContribution weightedReturnsWithContrbution=new WeightedReturnsWithContribution();
|
||||
int numReturns = portfolioHoldings.Min(p => p.Returns.Length);
|
||||
|
||||
WeightedReturnsWithContribution weightedReturnsWithContribution = new WeightedReturnsWithContribution();
|
||||
for (int index = 0; index < numReturns; index++)
|
||||
{
|
||||
for (int portfolioIndex = 0; portfolioIndex < portfolioHoldings.Count; portfolioIndex++)
|
||||
{
|
||||
PortfolioHolding portfolioHolding = portfolioHoldings[portfolioIndex];
|
||||
WeightedReturn weightedReturn=new WeightedReturn(portfolioHolding.Symbol,portfolioHolding.Prices[index].Date,portfolioHolding.Returns[index] * portfolioHolding.Weight);
|
||||
weightedReturnsWithContrbution.Add(index,weightedReturn);
|
||||
WeightedReturn weightedReturn = new WeightedReturn(
|
||||
portfolioHolding.Symbol,
|
||||
portfolioHolding.Prices[index].Date,
|
||||
portfolioHolding.Returns[index] * portfolioHolding.Weight
|
||||
);
|
||||
|
||||
weightedReturnsWithContribution.Add(index, weightedReturn);
|
||||
}
|
||||
}
|
||||
double[] weightedReturns=weightedReturnsWithContrbution.GetWeightedReturns();
|
||||
List<Contributions> contributionsList=weightedReturnsWithContrbution.GetContributions();
|
||||
// Organize the weighted returns into bins so we can access the nth percentile rank
|
||||
// The VaR nth percentile VaR is simply the weighted return at the given rank (i.e.) 90%, 95%, 99% within the bin
|
||||
// The VaR result will be a negative percent and negative amount to show that this is a loss. We display this as a positive number so we need to take the absolute value of the result
|
||||
// ** To Do ** bring out the contributors to VaR at the given rank.
|
||||
|
||||
double[] weightedReturns = weightedReturnsWithContribution.GetWeightedReturns();
|
||||
List<Contributions> contributionsList = weightedReturnsWithContribution.GetContributions();
|
||||
|
||||
// Organize the weighted returns into bins for percentile access
|
||||
BinManager<Contributions> binManager = new BinManager<Contributions>();
|
||||
BinResult<Contributions> binResult = binManager.GetVaRReturn(weightedReturns, contributionsList, percentile);
|
||||
Contributions contributions = binResult.Item;
|
||||
if(null==contributions){varResult.Success=false;return varResult;}
|
||||
Dictionary<String,PortfolioHolding> portfolioHoldingsBySymbol=new Dictionary<String,PortfolioHolding>();
|
||||
foreach(PortfolioHolding portfolioHolding in portfolioHoldings){if(!portfolioHoldingsBySymbol.ContainsKey(portfolioHolding.Symbol))portfolioHoldingsBySymbol.Add(portfolioHolding.Symbol,portfolioHolding);}
|
||||
|
||||
if (contributions == null)
|
||||
{
|
||||
varResult.Success = false;
|
||||
return varResult;
|
||||
}
|
||||
|
||||
// Map contributions back to portfolio holdings
|
||||
Dictionary<string, PortfolioHolding> portfolioHoldingsBySymbol = new Dictionary<string, PortfolioHolding>();
|
||||
foreach (PortfolioHolding portfolioHolding in portfolioHoldings)
|
||||
{
|
||||
if (!portfolioHoldingsBySymbol.ContainsKey(portfolioHolding.Symbol))
|
||||
portfolioHoldingsBySymbol.Add(portfolioHolding.Symbol, portfolioHolding);
|
||||
}
|
||||
|
||||
foreach (Contribution contribution in contributions)
|
||||
{
|
||||
if(!portfolioHoldingsBySymbol.ContainsKey(contribution.Symbol))continue;
|
||||
if (!portfolioHoldingsBySymbol.ContainsKey(contribution.Symbol))
|
||||
continue;
|
||||
|
||||
portfolioHoldingsBySymbol[contribution.Symbol].Contribution = contribution.ContributionValue;
|
||||
portfolioHoldingsBySymbol[contribution.Symbol].ContributionDate = contribution.AnalysisDate;
|
||||
}
|
||||
return new VaRResult(binResult.Value, portfolioHoldings.GetMarketValue() * binResult.Value);
|
||||
|
||||
return new VaRResult(binResult.Value, marketValue * binResult.Value);
|
||||
}
|
||||
|
||||
public int ReturnDays
|
||||
{
|
||||
get { return returnDays; }
|
||||
set { returnDays = value; }
|
||||
get => returnDays;
|
||||
set => returnDays = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,6 @@ namespace MarketData.ValueAtRisk
|
||||
}
|
||||
public void SetReturnDays(int days)
|
||||
{
|
||||
if (returnDays == days) return;
|
||||
returnDays = days;
|
||||
returns = prices.GetReturnsAsDoubleArray(returnDays);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user