Using Dictionary of int and list of tuple in C#Converting from binary to unary“Critter Tracking: When does...

What is the meaning of "of trouble" in the following sentence?

Patience, young "Padovan"

How can I fix this gap between bookcases I made?

Re-submission of rejected manuscript without informing co-authors

Unbreakable Formation vs. Cry of the Carnarium

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Does it makes sense to buy a new cycle to learn riding?

Is there a way to make member function NOT callable from constructor?

aging parents with no investments

Is there a name of the flying bionic bird?

How to make payment on the internet without leaving a money trail?

Can I legally use front facing blue light in the UK?

Lied on resume at previous job

Symmetry in quantum mechanics

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Typesetting a double Over Dot on top of a symbol

Information to fellow intern about hiring?

Are objects structures and/or vice versa?

How can I add custom success page

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

What does 'script /dev/null' do?

Where to refill my bottle in India?

I see my dog run

What is the command to reset a PC without deleting any files



Using Dictionary of int and list of tuple in C#


Converting from binary to unary“Critter Tracking: When does it cross its own path?”DataDictionary Application - ModelReturn IEnumerable<KeyValuePair> from a private method; use Dictionary or anon. type?Tuple<int, int> replacementSaving and resuming position while iterating over a containerSimple and reusable system for user registration and tracking and auto-updates - follow-upHackerEarth - SimpleFunctionSecurity Helper Class According To NIST SP800-63B GuidelinesLooping JSON in WebAPI Controller and Add new Property






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2












$begingroup$


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;
}
}









share|improve this question











$endgroup$



















    2












    $begingroup$


    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;
    }
    }









    share|improve this question











    $endgroup$















      2












      2








      2





      $begingroup$


      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;
      }
      }









      share|improve this question











      $endgroup$




      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;
      }
      }






      c#






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Jamal

      30.6k11121227




      30.6k11121227










      asked yesterday









      TomTom

      1213




      1213






















          1 Answer
          1






          active

          oldest

          votes


















          4












          $begingroup$

          There might be a reason why you used a List<Tuple<string ,double, double>>. E.g. if you need to pass the data to an existing library or your UI needs to bind to a list. Otherwise I would recommend to create a dedicated class



          public class RiskStatistics
          {
          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual { get; set; }

          public double AverageGainMonthly { get; set; }
          public double AverageGainAnnual { get; set; }

          public double AverageLossMonthly { get; set; }
          public double AverageLossAnnual { get; set; }
          }


          Since the properties are named, there's no need for magic strings. This makes the creation and usage of the risk matrix less convoluted:



          public Dictionary<int, RiskStatistics> RiskMatrixByFunds(
          Dictionary<int, IEnumerable<double>> returnsLists)
          {
          var riskMatrixDict = new Dictionary<int, RiskStatistics>();

          foreach (KeyValuePair<int, IEnumerable<double>> entry in returnsLists) {
          var risk = new RiskStatistics();
          (risk.ArithmeticMeanMonthly, risk.ArithmeticMeanAnnual) = ArithmeticMean(entry.Value);
          (risk.AverageGainMonthly, risk.AverageGainAnnual) = AverageGain(entry.Value);
          (risk.AverageLossMonthly, risk.AverageLossAnnual) = AverageLoss(entry.Value);
          riskMatrixDict.Add(entry.Key, risk);
          }
          return riskMatrixDict;
          }




          You could also calculate the annual values on the fly. This would make the creation even easier. Here the adapted average gain properties as an example:



          private static readonly double Sqrt12 = Math.Pow(12, 0.5);

          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual => ArithmeticMeanMonthly * Sqrt12;


          With this adapted RiskStatistics class, you could directly assign from the extension methods and skip the methods returning tuples.



          var risk = new RiskStatistics {
          ArithmeticMeanMonthly = entry.Value.Mean(),
          AverageGainMonthly = entry.Value.GainMean(),
          AverageLossMonthly = entry.Value.LossMean()
          };
          riskMatrixDict.Add(entry.Key, risk);


          The annual values are read-only and need not to be assigned.






          share|improve this answer











          $endgroup$









          • 1




            $begingroup$
            You could make the sqrt(12) as a const or static readonly field as a minor optimization.
            $endgroup$
            – Henrik Hansen
            22 hours ago












          • $begingroup$
            Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
            $endgroup$
            – Olivier Jacot-Descombes
            17 hours ago












          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217036%2fusing-dictionary-of-int-and-list-of-tuple-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4












          $begingroup$

          There might be a reason why you used a List<Tuple<string ,double, double>>. E.g. if you need to pass the data to an existing library or your UI needs to bind to a list. Otherwise I would recommend to create a dedicated class



          public class RiskStatistics
          {
          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual { get; set; }

          public double AverageGainMonthly { get; set; }
          public double AverageGainAnnual { get; set; }

          public double AverageLossMonthly { get; set; }
          public double AverageLossAnnual { get; set; }
          }


          Since the properties are named, there's no need for magic strings. This makes the creation and usage of the risk matrix less convoluted:



          public Dictionary<int, RiskStatistics> RiskMatrixByFunds(
          Dictionary<int, IEnumerable<double>> returnsLists)
          {
          var riskMatrixDict = new Dictionary<int, RiskStatistics>();

          foreach (KeyValuePair<int, IEnumerable<double>> entry in returnsLists) {
          var risk = new RiskStatistics();
          (risk.ArithmeticMeanMonthly, risk.ArithmeticMeanAnnual) = ArithmeticMean(entry.Value);
          (risk.AverageGainMonthly, risk.AverageGainAnnual) = AverageGain(entry.Value);
          (risk.AverageLossMonthly, risk.AverageLossAnnual) = AverageLoss(entry.Value);
          riskMatrixDict.Add(entry.Key, risk);
          }
          return riskMatrixDict;
          }




          You could also calculate the annual values on the fly. This would make the creation even easier. Here the adapted average gain properties as an example:



          private static readonly double Sqrt12 = Math.Pow(12, 0.5);

          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual => ArithmeticMeanMonthly * Sqrt12;


          With this adapted RiskStatistics class, you could directly assign from the extension methods and skip the methods returning tuples.



          var risk = new RiskStatistics {
          ArithmeticMeanMonthly = entry.Value.Mean(),
          AverageGainMonthly = entry.Value.GainMean(),
          AverageLossMonthly = entry.Value.LossMean()
          };
          riskMatrixDict.Add(entry.Key, risk);


          The annual values are read-only and need not to be assigned.






          share|improve this answer











          $endgroup$









          • 1




            $begingroup$
            You could make the sqrt(12) as a const or static readonly field as a minor optimization.
            $endgroup$
            – Henrik Hansen
            22 hours ago












          • $begingroup$
            Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
            $endgroup$
            – Olivier Jacot-Descombes
            17 hours ago
















          4












          $begingroup$

          There might be a reason why you used a List<Tuple<string ,double, double>>. E.g. if you need to pass the data to an existing library or your UI needs to bind to a list. Otherwise I would recommend to create a dedicated class



          public class RiskStatistics
          {
          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual { get; set; }

          public double AverageGainMonthly { get; set; }
          public double AverageGainAnnual { get; set; }

          public double AverageLossMonthly { get; set; }
          public double AverageLossAnnual { get; set; }
          }


          Since the properties are named, there's no need for magic strings. This makes the creation and usage of the risk matrix less convoluted:



          public Dictionary<int, RiskStatistics> RiskMatrixByFunds(
          Dictionary<int, IEnumerable<double>> returnsLists)
          {
          var riskMatrixDict = new Dictionary<int, RiskStatistics>();

          foreach (KeyValuePair<int, IEnumerable<double>> entry in returnsLists) {
          var risk = new RiskStatistics();
          (risk.ArithmeticMeanMonthly, risk.ArithmeticMeanAnnual) = ArithmeticMean(entry.Value);
          (risk.AverageGainMonthly, risk.AverageGainAnnual) = AverageGain(entry.Value);
          (risk.AverageLossMonthly, risk.AverageLossAnnual) = AverageLoss(entry.Value);
          riskMatrixDict.Add(entry.Key, risk);
          }
          return riskMatrixDict;
          }




          You could also calculate the annual values on the fly. This would make the creation even easier. Here the adapted average gain properties as an example:



          private static readonly double Sqrt12 = Math.Pow(12, 0.5);

          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual => ArithmeticMeanMonthly * Sqrt12;


          With this adapted RiskStatistics class, you could directly assign from the extension methods and skip the methods returning tuples.



          var risk = new RiskStatistics {
          ArithmeticMeanMonthly = entry.Value.Mean(),
          AverageGainMonthly = entry.Value.GainMean(),
          AverageLossMonthly = entry.Value.LossMean()
          };
          riskMatrixDict.Add(entry.Key, risk);


          The annual values are read-only and need not to be assigned.






          share|improve this answer











          $endgroup$









          • 1




            $begingroup$
            You could make the sqrt(12) as a const or static readonly field as a minor optimization.
            $endgroup$
            – Henrik Hansen
            22 hours ago












          • $begingroup$
            Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
            $endgroup$
            – Olivier Jacot-Descombes
            17 hours ago














          4












          4








          4





          $begingroup$

          There might be a reason why you used a List<Tuple<string ,double, double>>. E.g. if you need to pass the data to an existing library or your UI needs to bind to a list. Otherwise I would recommend to create a dedicated class



          public class RiskStatistics
          {
          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual { get; set; }

          public double AverageGainMonthly { get; set; }
          public double AverageGainAnnual { get; set; }

          public double AverageLossMonthly { get; set; }
          public double AverageLossAnnual { get; set; }
          }


          Since the properties are named, there's no need for magic strings. This makes the creation and usage of the risk matrix less convoluted:



          public Dictionary<int, RiskStatistics> RiskMatrixByFunds(
          Dictionary<int, IEnumerable<double>> returnsLists)
          {
          var riskMatrixDict = new Dictionary<int, RiskStatistics>();

          foreach (KeyValuePair<int, IEnumerable<double>> entry in returnsLists) {
          var risk = new RiskStatistics();
          (risk.ArithmeticMeanMonthly, risk.ArithmeticMeanAnnual) = ArithmeticMean(entry.Value);
          (risk.AverageGainMonthly, risk.AverageGainAnnual) = AverageGain(entry.Value);
          (risk.AverageLossMonthly, risk.AverageLossAnnual) = AverageLoss(entry.Value);
          riskMatrixDict.Add(entry.Key, risk);
          }
          return riskMatrixDict;
          }




          You could also calculate the annual values on the fly. This would make the creation even easier. Here the adapted average gain properties as an example:



          private static readonly double Sqrt12 = Math.Pow(12, 0.5);

          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual => ArithmeticMeanMonthly * Sqrt12;


          With this adapted RiskStatistics class, you could directly assign from the extension methods and skip the methods returning tuples.



          var risk = new RiskStatistics {
          ArithmeticMeanMonthly = entry.Value.Mean(),
          AverageGainMonthly = entry.Value.GainMean(),
          AverageLossMonthly = entry.Value.LossMean()
          };
          riskMatrixDict.Add(entry.Key, risk);


          The annual values are read-only and need not to be assigned.






          share|improve this answer











          $endgroup$



          There might be a reason why you used a List<Tuple<string ,double, double>>. E.g. if you need to pass the data to an existing library or your UI needs to bind to a list. Otherwise I would recommend to create a dedicated class



          public class RiskStatistics
          {
          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual { get; set; }

          public double AverageGainMonthly { get; set; }
          public double AverageGainAnnual { get; set; }

          public double AverageLossMonthly { get; set; }
          public double AverageLossAnnual { get; set; }
          }


          Since the properties are named, there's no need for magic strings. This makes the creation and usage of the risk matrix less convoluted:



          public Dictionary<int, RiskStatistics> RiskMatrixByFunds(
          Dictionary<int, IEnumerable<double>> returnsLists)
          {
          var riskMatrixDict = new Dictionary<int, RiskStatistics>();

          foreach (KeyValuePair<int, IEnumerable<double>> entry in returnsLists) {
          var risk = new RiskStatistics();
          (risk.ArithmeticMeanMonthly, risk.ArithmeticMeanAnnual) = ArithmeticMean(entry.Value);
          (risk.AverageGainMonthly, risk.AverageGainAnnual) = AverageGain(entry.Value);
          (risk.AverageLossMonthly, risk.AverageLossAnnual) = AverageLoss(entry.Value);
          riskMatrixDict.Add(entry.Key, risk);
          }
          return riskMatrixDict;
          }




          You could also calculate the annual values on the fly. This would make the creation even easier. Here the adapted average gain properties as an example:



          private static readonly double Sqrt12 = Math.Pow(12, 0.5);

          public double ArithmeticMeanMonthly { get; set; }
          public double ArithmeticMeanAnnual => ArithmeticMeanMonthly * Sqrt12;


          With this adapted RiskStatistics class, you could directly assign from the extension methods and skip the methods returning tuples.



          var risk = new RiskStatistics {
          ArithmeticMeanMonthly = entry.Value.Mean(),
          AverageGainMonthly = entry.Value.GainMean(),
          AverageLossMonthly = entry.Value.LossMean()
          };
          riskMatrixDict.Add(entry.Key, risk);


          The annual values are read-only and need not to be assigned.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 14 hours ago

























          answered yesterday









          Olivier Jacot-DescombesOlivier Jacot-Descombes

          2,6971218




          2,6971218








          • 1




            $begingroup$
            You could make the sqrt(12) as a const or static readonly field as a minor optimization.
            $endgroup$
            – Henrik Hansen
            22 hours ago












          • $begingroup$
            Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
            $endgroup$
            – Olivier Jacot-Descombes
            17 hours ago














          • 1




            $begingroup$
            You could make the sqrt(12) as a const or static readonly field as a minor optimization.
            $endgroup$
            – Henrik Hansen
            22 hours ago












          • $begingroup$
            Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
            $endgroup$
            – Olivier Jacot-Descombes
            17 hours ago








          1




          1




          $begingroup$
          You could make the sqrt(12) as a const or static readonly field as a minor optimization.
          $endgroup$
          – Henrik Hansen
          22 hours ago






          $begingroup$
          You could make the sqrt(12) as a const or static readonly field as a minor optimization.
          $endgroup$
          – Henrik Hansen
          22 hours ago














          $begingroup$
          Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
          $endgroup$
          – Olivier Jacot-Descombes
          17 hours ago




          $begingroup$
          Good idea. Integrated it in my answer. Multiplication is fast and should have no noticeable impact on speed.
          $endgroup$
          – Olivier Jacot-Descombes
          17 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217036%2fusing-dictionary-of-int-and-list-of-tuple-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...