I have implemented mathematical functions using a C# library. I basically need to output data by the RiskMatrixByFunds
in the following format.
Key will contain Id and value will contain collection of string, double, double
For example:
Id 1
value
{'ArithmeticMean', 12.34, 3.44},
{'AverageGain', 12.35, 3.45},
{'AverageLoss', 12.36, 3.46},
I have used a Dictionary structure that will contain the int and list of tuple collection.
Let me know if it can be enhanced, any ideas on magic strings used, a better way to implement it.
public class RiskMatrix : IRiskMatrix
{
public (double Monthly, double Annual) ArithmeticMean(IEnumerable<double> ReturnsList)
{
double returnList = ReturnsList.Mean();
return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
}
public (double Monthly, double Annual) AverageGain(IEnumerable<double> ReturnsList)
{
double returnList = ReturnsList.GainMean();
return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
}
public (double Monthly, double Annual) AverageLoss(IEnumerable<double> ReturnsList)
{
double returnList = ReturnsList.LossMean();
return (Monthly: returnList, Annual: returnList * Math.Pow(12, 0.5));
}
public Dictionary<int, List<Tuple<string,double, double>>> RiskMatrixByFunds(Dictionary<int, IEnumerable<double>> ReturnsList)
{
Dictionary<int, List<Tuple<string ,double, double>>> returnsList = new Dictionary<int, List<Tuple<string,double, double>>>();
List <Tuple<string, double, double>> list = null;
foreach (KeyValuePair<int, IEnumerable<double>> entry in ReturnsList)
{
list = new List<Tuple<string, double, double>>();
(double Monthly, double Annual) arithmeticMeanResult = ArithmeticMean(entry.Value);
list.Add(new Tuple<string, double, double>("ArithmeticMean", arithmeticMeanResult.Monthly, arithmeticMeanResult.Annual));
(double Monthly, double Annual) averageGainResult = AverageGain(entry.Value);
list.Add(new Tuple<string, double, double>("AverageGain", averageGainResult.Monthly, averageGainResult.Annual));
(double Monthly, double Annual) averageLossResult = AverageLoss(entry.Value);
list.Add(new Tuple<string, double, double>("AverageLoss", averageLossResult.Monthly, averageLossResult.Annual));
(double Monthly, double Annual) betaCorrelationresult = BetaCorrelation(entry.Value);
returnsList.Add(entry.Key, list);
}
return returnsList;
}
}