vish1990 vish1990 - 29 days ago 6
C# Question

Mean and standard Deviaton of array with Math.Net

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

Answer

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