Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Using LINQ to Calculate Basic Statistics

Rate me:
Please Sign up or sign in to vote.
4.90/5 (64 votes)
3 Nov 2020CPOL6 min read 163.6K   2.2K   151  
Extension methods for variance, standard deviation, range, median, mode and some other basic descriptive statistics
A statistical and numerical processing package with a simple and lightweight implementation for the basic stats: variance (sample and population), standard deviation (sample and population), covariance, Pearson (chi squared), range, median, least squares, root mean square, histogram, and mode.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LinqStatistics.UnitTests
{
    /// <summary>
    /// Summary description for CovarianceTests
    /// </summary>
    [TestClass]
    public class CovarianceTests
    {
        public CovarianceTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Covariance()
        {
            IEnumerable<double> source = TestData.GetDoubles();
            IEnumerable<double> other = TestData.GetDoubles();


            double result = source.Covariance(other);

            Assert.AreEqual(result, 3.081875, double.Epsilon);
        }

        [TestMethod]
        public void Covariance1()
        {
            IEnumerable<double> source = TestData.GetDoubles();
            IEnumerable<double> other = TestData.GetInts().Select(x => (double)x);


            double result = source.Covariance(other);

            Assert.AreEqual(result, 2.59375, double.Epsilon);
        }


        [TestMethod]
        public void PearsonIdentity()
        {
            IEnumerable<double> source = TestData.GetDoubles();
            IEnumerable<double> other = TestData.GetDoubles();


            double result = source.Pearson(other);

            Assert.AreEqual(result, 1.0, double.Epsilon);
        }

        [TestMethod]
        public void Pearson1()
        {
            IEnumerable<double> source = TestData.GetDoubles();
            IEnumerable<double> other = TestData.GetInts().Select(x => (double)x);

            double result = source.Pearson(other);

            Assert.AreEqual(result, 0.998956491208287, double.Epsilon);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions