예제 #1
0
        /// <summary>
        /// Calculate the covariance value of two array data. Supported data type: double/float/int.
        /// </summary>
        /// <param name="data1">The first array data to calculate Covariance value.</param>
        /// <param name="data2">The second array data to calculate Covariance value.</param>
        /// <typeparam name="TDataType">Supported data type: double/float/int.</typeparam>
        /// <returns>The covariance value of two array data in specific place.</returns>
        /// <exception cref="NotSupportedException">Supported data type: double/float/int.</exception>
        public static double Covariance <TDataType>(TDataType[] data1, TDataType[] data2)
        {
            double covariance = double.NaN;

            if (ReferenceEquals(typeof(TDataType), typeof(double)))
            {
                double[] doubleData1 = data1 as double[];
                double[] doubleData2 = data2 as double[];
                covariance = ArrayStatistics.Covariance(doubleData1, doubleData2);
            }
            else if (ReferenceEquals(typeof(TDataType), typeof(float)))
            {
                float[] floatData1 = data1 as float[];
                float[] floatData2 = data2 as float[];
                covariance = ArrayStatistics.Covariance(floatData1, floatData2);
            }
            else if (ReferenceEquals(typeof(TDataType), typeof(int)))
            {
                int[] intData1 = data1 as int[];
                int[] intData2 = data2 as int[];
                covariance = ArrayStatistics.Covariance(intData1, intData2);
            }
            else
            {
                throw new NotSupportedException("Unsupported data type.");
            }
            return(covariance);
        }
예제 #2
0
        public void CovarianceConsistentWithVariance(string dataSet)
        {
            var data = _data[dataSet];

            AssertHelpers.AlmostEqual(Statistics.Variance(data.Data), Statistics.Covariance(data.Data, data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.Variance(data.Data), ArrayStatistics.Covariance(data.Data, data.Data), 10);
            AssertHelpers.AlmostEqual(StreamingStatistics.Variance(data.Data), StreamingStatistics.Covariance(data.Data, data.Data), 10);
        }
예제 #3
0
        public void ThrowsOnNullData()
        {
            double[] data = null;

            // ReSharper disable InvokeAsExtensionMethod
            Assert.That(() => Statistics.Minimum(data), Throws.Exception);
            Assert.That(() => Statistics.Maximum(data), Throws.Exception);
            Assert.That(() => Statistics.Mean(data), Throws.Exception);
            Assert.That(() => Statistics.Median(data), Throws.Exception);
            Assert.That(() => Statistics.Quantile(data, 0.3), Throws.Exception);
            Assert.That(() => Statistics.Variance(data), Throws.Exception);
            Assert.That(() => Statistics.StandardDeviation(data), Throws.Exception);
            Assert.That(() => Statistics.PopulationVariance(data), Throws.Exception);
            Assert.That(() => Statistics.PopulationStandardDeviation(data), Throws.Exception);
            Assert.That(() => Statistics.Covariance(data, data), Throws.Exception);
            Assert.That(() => Statistics.PopulationCovariance(data, data), Throws.Exception);
            // ReSharper restore InvokeAsExtensionMethod

            Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.OrderStatistic(data, 1), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Median(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.LowerQuartile(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.UpperQuartile(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Percentile(data, 30), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.Quantile(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.InterquartileRange(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => SortedArrayStatistics.FiveNumberSummary(data), Throws.Exception.TypeOf <NullReferenceException>());

            Assert.That(() => ArrayStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.OrderStatisticInplace(data, 1), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.MedianInplace(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => ArrayStatistics.QuantileInplace(data, 0.3), Throws.Exception.TypeOf <NullReferenceException>());

            Assert.That(() => StreamingStatistics.Minimum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Maximum(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Mean(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Variance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.StandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationVariance(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationStandardDeviation(data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.Covariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
            Assert.That(() => StreamingStatistics.PopulationCovariance(data, data), Throws.Exception.TypeOf <NullReferenceException>());
        }
예제 #4
0
        public void ArrayStatisticsConsistentWithStreamimgStatistics(string dataSet)
        {
            var data = _data[dataSet];

            AssertHelpers.AlmostEqual(ArrayStatistics.Mean(data.Data), StreamingStatistics.Mean(data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.Variance(data.Data), StreamingStatistics.Variance(data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.StandardDeviation(data.Data), StreamingStatistics.StandardDeviation(data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.PopulationVariance(data.Data), StreamingStatistics.PopulationVariance(data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.PopulationStandardDeviation(data.Data), StreamingStatistics.PopulationStandardDeviation(data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.Covariance(data.Data, data.Data), StreamingStatistics.Covariance(data.Data, data.Data), 10);
            AssertHelpers.AlmostEqual(ArrayStatistics.PopulationCovariance(data.Data, data.Data), StreamingStatistics.PopulationCovariance(data.Data, data.Data), 10);
        }
예제 #5
0
        public void ThrowsOnNullData()
        {
            double[] data = null;

            Assert.Throws <ArgumentNullException>(() => Statistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Median(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Quantile(data, 0.3));
            Assert.Throws <ArgumentNullException>(() => Statistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => Statistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => Statistics.PopulationCovariance(data, data));

            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.OrderStatistic(data, 1));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Median(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.LowerQuartile(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.UpperQuartile(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Percentile(data, 30));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.Quantile(data, 0.3));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, 0, 0, 1, 0));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.QuantileCustom(data, 0.3, QuantileDefinition.Nearest));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.InterquartileRange(data));
            Assert.Throws <ArgumentNullException>(() => SortedArrayStatistics.FiveNumberSummary(data));

            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.OrderStatisticInplace(data, 1));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.PopulationCovariance(data, data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.MedianInplace(data));
            Assert.Throws <ArgumentNullException>(() => ArrayStatistics.QuantileInplace(data, 0.3));

            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Minimum(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Maximum(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Mean(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Variance(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.StandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationVariance(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationStandardDeviation(data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.Covariance(data, data));
            Assert.Throws <ArgumentNullException>(() => StreamingStatistics.PopulationCovariance(data, data));
        }
예제 #6
0
        public void CovarianceIsSymmetric()
        {
            var dataA = _data["lottery"].Data.Take(200);
            var dataB = _data["lew"].Data.Take(200);

            AssertHelpers.AlmostEqual(Statistics.Covariance(dataA, dataB), Statistics.Covariance(dataB, dataA), 12);
            AssertHelpers.AlmostEqual(StreamingStatistics.Covariance(dataA, dataB), StreamingStatistics.Covariance(dataB, dataA), 12);
            AssertHelpers.AlmostEqual(ArrayStatistics.Covariance(dataA.ToArray(), dataB.ToArray()), ArrayStatistics.Covariance(dataB.ToArray(), dataA.ToArray()), 12);

            AssertHelpers.AlmostEqual(Statistics.PopulationCovariance(dataA, dataB), Statistics.PopulationCovariance(dataB, dataA), 12);
            AssertHelpers.AlmostEqual(StreamingStatistics.PopulationCovariance(dataA, dataB), StreamingStatistics.PopulationCovariance(dataB, dataA), 12);
            AssertHelpers.AlmostEqual(ArrayStatistics.PopulationCovariance(dataA.ToArray(), dataB.ToArray()), ArrayStatistics.PopulationCovariance(dataB.ToArray(), dataA.ToArray()), 12);
        }
        private void Initialization()
        {
            //Calculate initialstate and initialstatecovariance
            var M = Matrix <double> .Build;

            initialstate           = M.Dense(4, 1);
            initialstatecovariance = M.DenseDiagonal(4, 4, 0.00);
            var tempyields = new double[maturities.Length];
            var StaticNS4factorCalibration = new StaticNS4FactorModelCalibration();

            StaticNS4factorCalibration.maturities = maturities;
            var tempbeta1 = new double[30];
            var tempbeta2 = new double[30];
            var tempbeta3 = new double[30];
            var tempbeta4 = new double[30];

            Parallel.For(0, 30, i =>
            {
                tempyields = yields[yields.ElementAt(i).Key].Clone() as double[];
                StaticNS4factorCalibration.yields = tempyields;
                var temppara = StaticNS4factorCalibration.Calibration();
                tempbeta1[i] = temppara[0];
                tempbeta2[i] = temppara[1];
                tempbeta3[i] = temppara[2];
                tempbeta4[i] = temppara[3];
            });
            initialstate[0, 0] = ArrayStatistics.Mean(tempbeta1);
            initialstate[1, 0] = ArrayStatistics.Mean(tempbeta2);
            initialstate[2, 0] = ArrayStatistics.Mean(tempbeta3);
            initialstate[3, 0] = ArrayStatistics.Mean(tempbeta4);

            initialstatecovariance[0, 0] = ArrayStatistics.Covariance(tempbeta1, tempbeta1);
            initialstatecovariance[0, 1] = ArrayStatistics.Covariance(tempbeta1, tempbeta2);
            initialstatecovariance[0, 2] = ArrayStatistics.Covariance(tempbeta1, tempbeta3);
            initialstatecovariance[0, 3] = ArrayStatistics.Covariance(tempbeta1, tempbeta4);

            initialstatecovariance[1, 0] = ArrayStatistics.Covariance(tempbeta2, tempbeta1);
            initialstatecovariance[1, 1] = ArrayStatistics.Covariance(tempbeta2, tempbeta2);
            initialstatecovariance[1, 2] = ArrayStatistics.Covariance(tempbeta2, tempbeta3);
            initialstatecovariance[1, 3] = ArrayStatistics.Covariance(tempbeta2, tempbeta4);

            initialstatecovariance[2, 0] = ArrayStatistics.Covariance(tempbeta3, tempbeta1);
            initialstatecovariance[2, 1] = ArrayStatistics.Covariance(tempbeta3, tempbeta2);
            initialstatecovariance[2, 2] = ArrayStatistics.Covariance(tempbeta3, tempbeta3);
            initialstatecovariance[2, 3] = ArrayStatistics.Covariance(tempbeta3, tempbeta4);

            initialstatecovariance[3, 0] = ArrayStatistics.Covariance(tempbeta4, tempbeta1);
            initialstatecovariance[3, 1] = ArrayStatistics.Covariance(tempbeta4, tempbeta2);
            initialstatecovariance[3, 2] = ArrayStatistics.Covariance(tempbeta4, tempbeta3);
            initialstatecovariance[3, 3] = ArrayStatistics.Covariance(tempbeta4, tempbeta4);
        }