Using MathNet.Numerics.Statistics

Questions about MultiCharts .NET and user contributed studies.
Louis88
Posts: 24
Joined: 11 May 2013
Has thanked: 28 times
Been thanked: 1 time

Using MathNet.Numerics.Statistics

Postby Louis88 » 27 Sep 2015

Hello,
I'm trying to use the Math.Net Numerics Package (version 3.7.1) and I've started from Statistics.
In the following code:

Code: Select all

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




namespace PowerLanguage.Indicator
{
public class PR_LognormalExample : IndicatorObject
{
public PR_LognormalExample(object _ctx):base(_ctx){}
private List<double> L_myLogVolumes;

protected override void Create()
{
L_myLogVolumes = new List<double>();
}

protected override void StartCalc()
{
L_myLogVolumes.Clear();
Output.Clear();
}

protected override void CalcBar()
{
if (Bars.VolumeValue > 0)
L_myLogVolumes.Add(Math.Log(Bars.VolumeValue));

IEnumerable<double> myVols = L_myLogVolumes;

if (Bars.LastBarOnChart)
{
double myLogMean = myVols.Average();
double myLogVariance = CalculateVariance(myVols);
double myLogStDev = Math.Sqrt(myLogVariance);

Output.WriteLine("Log Mean: {0}", myLogMean);
Output.WriteLine("Log Standard Deviation: {0}", myLogStDev);
Output.WriteLine("Log Variance: {0}", myLogVariance);

////////////////// Math Net Statistics /////////////////////////////////
//double mathNetLogMean = Statistics.Mean(myVols);
double mathNetLogStdDev = Statistics.PopulationStandardDeviation(myVols);
double mathNetVariance = Statistics.PopulationVariance(myVols);

Output.WriteLine(" ");
// Output.WriteLine("MathNet Log Mean: {0}", mathNetLogMean);
Output.WriteLine("MathNet Log Standard Deviation: {0}", mathNetLogStdDev);
Output.WriteLine("MathNet Log Variance: {0}", mathNetVariance);
}
}


private double CalculateVariance(IEnumerable<double> values)
{
double varianceRet = 0;
if (values.Count() > 0)
{
//Compute the Average
double avg = values.Average();
//Perform the Sum of (value-avg)_2_2
double sum = values.Sum(d => Math.Pow(d - avg, 2));
//Put it all together
varianceRet = sum / values.Count();
}
return varianceRet;
}
}
}
I can calculate population variance and population standard deviation but an error message is raised if I try to calculate the mean and all the other interesting statistics.
Please uncomment rows no. 45 and 50 and rebuilt.
The error is about overflow or underflow (please see attached jpeg).
It seems quite stange because variance has been already calculated. I mean that I must have the aritmatic mean to calculate variance, isn't it?
I can't find the error.
Thanks for your time.
Attachments
Error message.jpg
(253.97 KiB) Downloaded 817 times

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Using MathNet.Numerics.Statistics

Postby Henry MultiСharts » 05 Oct 2015

Hello Louis88,

The exception appears in the third party dll you are using. Please contact the dll developer for assistance.


Return to “MultiCharts .NET”