2

I am using MathNet .dll and have to calculate mean and standard deviation from array of decimal values.

using MathNet.Numerics.Statistics;



static void Main(string[] args)
        {
            decimal[] values = { 39.99m, 29.99m, 19.99m, 49.99m };
            MathNet.Numerics.Statistics.
        }

But I am not getting a method to calculate mean.I have to perform many mathmatical operation but no getting starting point.I would be grateful if someone can point me in right direction.I tried but can not find any sample so that I can use that info for rest of mathmatical operations. I have to use MathNet library . Thanks

Entire Code

using System;
using MathNet.Numerics.Statistics;
using System.Linq;

public class Program
{
    public static void Main()
    {
        decimal[] values = new[] { 39.99m, 29.99m, 19.99m, 49.99m };
        Tuple<double, double> meanStd = values
            .Select(x => (double)x)
            .MeanStandardDeviation();

        double mean = meanStd.Item1;
        double std = meanStd.Item2;

        Console.WriteLine("Mean = " + mean);
        Console.WriteLine("Std = " + std);
    }
}
vish1990
  • 272
  • 3
  • 16
  • Have you read the documentation? http://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Statistics.htm#MeanStandardDeviation – Steve Nov 11 '16 at 17:19
  • Yes Sir but could not understand how can i implement that.Appologies but i am trying – vish1990 Nov 11 '16 at 17:22

1 Answers1

9

You can use extension method. If you have decimal values, you must cast it to double first. Don't forget to add using System.Linq and using MathNet.Numerics.Statistics at the top.

You can read documentation for MeanStandardDeviation method here.

decimal[] values = new []{ 39.99m, 29.99m, 19.99m, 49.99m };
Tuple<double, double> meanStd = values
    .Select(x=>(double)x)
    .MeanStandardDeviation();

double mean = meanStd.Item1;
double std = meanStd.Item2;

Fiddle: https://dotnetfiddle.net/LubPTH

Niyoko
  • 7,512
  • 4
  • 32
  • 59
  • Sorry Sir but array do not have MeanStandardDeviation deviation. – vish1990 Nov 11 '16 at 17:21
  • @vish1990 It is an extension method. See https://msdn.microsoft.com/en-us//library/bb383977.aspx – Steve Nov 11 '16 at 17:23
  • Thanks Sir but I have to use mathNet .dll .I have my own method to calculate mean but I am asked to change all by using external .dll (MathNet) – vish1990 Nov 11 '16 at 17:24
  • @vish1990 It is MathNet sir. `MeanStandardDeviation` **is** a Math.Net method. – Niyoko Nov 11 '16 at 17:25
  • Thanks a lot Sir,Now I hope I can write all other Mathmatical functions.you guys are of great help. – vish1990 Nov 11 '16 at 17:28
  • Sir I tried same code but i ma getting below error:`Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'MeanStandardDeviation' and no extension method 'MeanStandardDeviation' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)` – vish1990 Nov 11 '16 at 17:47
  • @vish1990 add `using MathNet.Numerics.Statistics;` into the top of the file. – Niyoko Nov 11 '16 at 17:48
  • Sir,I have used it.I have downloaded the .dll from http://mathnetnumerics.codeplex.com/downloads/get/616759 (MathNet.Numerics-2.4.0.26) – vish1990 Nov 11 '16 at 17:53
  • Got it.Uninstalled and installed again.Thanks again and for your patience too – vish1990 Nov 11 '16 at 18:09
  • Sorry guys,but i am getting double value and i have to return decimal.Can i simply do `Convert.ToDecimal()` .Will it have any impact on data. – vish1990 Nov 12 '16 at 10:02
  • You can simply cast it `decimal mean = (decimal) meanStd.Item1;` – Niyoko Nov 12 '16 at 14:27
  • @vish1990 `Convert.ToDecimal` will also do the job perfectly. – Niyoko Nov 12 '16 at 14:28