コード例 #1
0
        public void PearsonCorrelationTest()
        {
            var dataA = _data["lottery"].Data.Take(200);
            var dataB = _data["lew"].Data.Take(200);

            var corr = Correlation.Pearson(dataA, dataB);

            AssertHelpers.AlmostEqual(-0.029470861580726, corr, 14);
        }
コード例 #2
0
    // Executed when a new order has been created
    public PendingOrder RiskManagement(PendingOrder pendingOrder, AgentState state)
    {
        //Check if we have enough data
        if (!Agent.Bars[Agent.Security, (int)BarInterval.Day, -15].IsValid)
        {
            return(null);
        }
        //Check if we can calculate the correlation
        else if (!corr.ContainsKey(Agent.Security))
        {
            return(null);
        }

        //Calculate correlation for each symbol
        var basevalues = new List <double>();

        for (int i = 0; i < 15; i++)
        {
            basevalues.Add((double)Agent.Bars[Agent.Security, (int)BarInterval.Day, i].Close);
        }

        //Get all symbol data
        foreach (var symbol in corr.Keys.ToArray())
        {
            var values = new List <double>();
            for (int i = 0; i < 15; i++)
            {
                values.Add((double)Agent.Bars[symbol, (int)BarInterval.Day, i].Close);
            }

            //calculate correlation coeff
            corr[symbol] = Correlation.Pearson(basevalues.ToArray(), values.ToArray());
        }

        //Get item with the highest corr
        var max = corr
                  .Where(x => x.Key != pendingOrder.Order.Security)
                  .Max(n => n.Value);
        var security = corr
                       .Where(x => x.Value == max).FirstOrDefault().Key;

        //Remove old position
        if (lastsecurity != security && !Agent.Positions[security].IsFlat)
        {
            SubmitOrder(CreateOrder(lastsecurity.Name, Direction.Flat, Agent.Positions[security].FlatQuantity));
        }

        //Return our current order
        lastsecurity = security;
        decimal quantity = pendingOrder.Order.Quantity * (arbitrage / 100M);

        quantity  = quantity > 0.01M ? quantity : 0.01M;
        quantity += Math.Abs(Agent.Positions[security].FlatQuantity);

        return(CreateOrder(security.Name, pendingOrder.Order.Direction == Direction.Long ? Direction.Short : Direction.Long, quantity, 0, 0));
    }
コード例 #3
0
        private static double LogReturnsCorrelation(ReadOnlySpan <double> priceSims1Start, ReadOnlySpan <double> priceSims1End,
                                                    ReadOnlySpan <double> priceSims2Start, ReadOnlySpan <double> priceSims2End)
        {
            Vector <double> logChanges1 = LogChange(priceSims1Start, priceSims1End);
            Vector <double> logChanges2 = LogChange(priceSims2Start, priceSims2End);

            double correlation = Correlation.Pearson(logChanges1, logChanges2);

            return(correlation);
        }
コード例 #4
0
        public void PearsonCorrelationConsistentWithCovariance()
        {
            var dataA = _data["lottery"].Data.Take(200).ToArray();
            var dataB = _data["lew"].Data.Take(200).ToArray();

            var direct     = Correlation.Pearson(dataA, dataB);
            var covariance = dataA.Covariance(dataB) / (dataA.StandardDeviation() * dataB.StandardDeviation());

            AssertHelpers.AlmostEqual(covariance, direct, 14);
        }
コード例 #5
0
        public static double GetCorrelationBetween(TimeSeries ts1, TimeSeries ts2)
        {
            DateTime[] commonDomain = ts1.GetCommonDates(ts2)
                                      .OrderBy(date => date)
                                      .ToArray();
            IEnumerable <double> x = commonDomain.Select(date => ts1[date]);
            IEnumerable <double> y = commonDomain.Select(date => ts2[date]);

            return(Correlation.Pearson(x, y));
        }
コード例 #6
0
        public void ConstantWeightedPearsonCorrelationTest()
        {
            var dataA   = _data["lottery"].Data.Take(200);
            var dataB   = _data["lew"].Data.Take(200);
            var weights = Generate.Repeat(200, 2.0);

            var corr  = Correlation.Pearson(dataA, dataB);
            var corr2 = Correlation.WeightedPearson(dataA, dataB, weights);

            AssertHelpers.AlmostEqual(corr, corr2, 14);
        }
コード例 #7
0
        public void SampleTest()
        {
            //Variances and Means
            double[] Sdv  = new double[2];
            double[] Mean = new double[2];
            for (int i = 0; i < 2; i++)
            {
                Sdv[i]  = RandomVar();
                Mean[i] = 5 * rnd.NextDouble();
            }

            //Cross Variance
            double rho = 1.0 - RandomVar();

            DensityLn <double[]> pdfLn = new DensityLn <double[]>(x => LogDen(x, Sdv, Mean, rho));


            HybridMC Hybrid = new HybridMC(new double[2] {
                0, 0
            }, pdfLn, 10, 0.1, 1000);

            Hybrid.BurnInterval = 0;

            int SampleSize = 10000;

            double[][] Sample     = Hybrid.Sample(SampleSize);
            double[][] NewSamples = ArrangeSamples(SampleSize, Sample);

            double[] Convergence = new double[2];
            double[] SampleMean  = new double[2];
            double[] SampleSdv   = new double[2];


            for (int i = 0; i < 2; i++)
            {
                Convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => x[i]));
                DescriptiveStatistics Stats = new DescriptiveStatistics(NewSamples[i]);
                SampleMean[i] = Stats.Mean;
                SampleSdv[i]  = Stats.StandardDeviation;
            }
            double SampleRho = Correlation.Pearson(NewSamples[0], NewSamples[1]);

            for (int i = 0; i < 2; i++)
            {
                string index = i.ToString();
                Assert.AreEqual(Mean[i], SampleMean[i], 10 * Convergence[i], index + "Mean");
                Assert.AreEqual(SampleSdv[i] * SampleSdv[i], Sdv[i] * Sdv[i], 10 * Convergence[i], index + "Standard Deviation");
            }

            double ConvergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(Sample, x => (x[0] - SampleMean[0]) * (x[1] - SampleMean[1])));

            Assert.AreEqual(SampleRho * SampleSdv[0] * SampleSdv[1], rho * Sdv[0] * Sdv[1], 10 * ConvergenceRho, "Rho");
        }
コード例 #8
0
        public void validateObviousData()
        {
            var axisX = new double[] { 1, 2, 3, 4, 5 };
            var axisY = new double[] { 2, 4, 9, 16, 25 };
            var correlationByPearsman  = Correlation.Pearson(axisX, axisY);
            var correlationBySpearsman = Correlation.Spearman(axisX, axisY);

            Assert.IsTrue(correlationByPearsman > 0.9, $"Shitty library: pearsman validation failed");
            Assert.IsTrue(correlationBySpearsman > 0.9, $"Shitty library: spearsman validation failed");
            Warn.If(correlationByPearsman <0.9 && correlationByPearsman> 0.8, $"Better, but shitty, pearsman correlation");
            Warn.If(correlationBySpearsman <0.9 && correlationBySpearsman> 0.8, $"Better, but shitty, spearsman correlation");
        }
コード例 #9
0
ファイル: Metric.cs プロジェクト: wubinzzu/RecSys
        private static double PearsonR(SparseVector Vector_a, SparseVector Vector_b)
        {
            double correlation = Correlation.Pearson(Vector_a, Vector_b);

            if (double.IsNaN(correlation))
            {
                // This means one of the row has 0 standard divation,
                // it does not correlate to anyone
                // so I assign the correlatino to be 0. however, strictly speaking, it should be left NaN
                correlation = 0;
            }
            return(correlation);
        }
コード例 #10
0
        private string GetCorrelation(Session currentSession)
        {
            var frametimes          = currentSession.FrameTimes.Where((x, i) => !_session.AppMissed[i]);
            var displayChangedTimes = currentSession.DisplayTimes.Where((x, i) => !_session.AppMissed[i]);

            if (frametimes.Count() != displayChangedTimes.Count())
            {
                return("NaN");
            }

            var correlation = Correlation.Pearson(frametimes, displayChangedTimes);

            return(Math.Round(correlation * 100, 0).ToString(CultureInfo.InvariantCulture) + "%");
        }
コード例 #11
0
        internal static double MeasureCorrelation(double[] totals, double[] goods)
        {
            if (totals.Length <= 1 || goods.Length <= 1)
            {
                return(0);
            }
            var result = Math.Round(Correlation.Pearson(totals, goods), 3);

            if (double.IsNaN(result))
            {
                return(0);
            }
            return(result);
        }
コード例 #12
0
        /// <summary>
        /// 自动对齐
        /// </summary>
        /// <param name="ww">输入参考波形1的数据</param>
        ///WW(1,:):左高低
        ///WW(2,:):右高低
        ///WW(3,:):左轨向
        ///WW(4,:):右轨向
        ///WW(5,:):轨距------(总共5列数据)
        /// <param name="ww1">参考波形2的数据</param>
        /// <returns>移动的点数</returns>
        /// 备注:波形1不动,波形2减去点数
        ///       波形2不动,波形1加上点数
        //public int AutoTranslation(float[][] ww, float[][] ww1)
        //{
        //    int retVal = 0;

        //    int rowLen_ww = ww.GetLength(0);
        //    int colLen_ww = ww[0].Length;
        //    MWNumericArray d_ww = new MWNumericArray(MWArrayComplexity.Real,rowLen_ww,colLen_ww);
        //    //matlab中矩阵的下标是从1开始的,而C#是从0开始的
        //    for (int i = 0; i < rowLen_ww; i++)
        //    {
        //        for (int j = 0; j < colLen_ww;j++ )
        //        {
        //            d_ww[i + 1, j + 1] = ww[i][j];
        //        }
        //    }

        //    int rowLen_ww1 = ww1.GetLength(0);
        //    int colLen_ww1 = ww1[0].Length;
        //    MWNumericArray d_ww1 = new MWNumericArray(MWArrayComplexity.Real, rowLen_ww1, colLen_ww1);
        //    //matlab中矩阵的下标是从1开始的,而C#是从0开始的
        //    for (int i = 0; i < rowLen_ww1; i++)
        //    {
        //        for (int j = 0; j < colLen_ww1; j++)
        //        {
        //            d_ww1[i + 1, j + 1] = ww1[i][j];
        //        }
        //    }

        //    try
        //    {
        //        //调用算法
        //        MWArray resultArray = advDataProcess.sub_preprocessing_alignment_data(d_ww, d_ww1);
        //        //MWArray resultArray = null;
        //        double[,] tmpArray = (double[,])(resultArray.ToArray());

        //        retVal = (int)tmpArray[0,0];
        //    }
        //    catch (System.Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }


        //    return retVal;
        //}
        #endregion

        public int AutoTranslation(double[][] ww, double[][] ww1)
        {
            int retVal     = 0;
            int corrLength = 300;
            int findCount  = 0;
            int index      = 0;

            //波形个数
            for (int i = 0; i < ww.Length; i++)
            {
                //第一个波形的点数
                for (int j = 0; j < (ww1[i].Length - corrLength); j++)
                {
                    double   lastCorr    = 0;
                    double[] sourceArray = new double[corrLength];
                    Array.Copy(ww1[i], j, sourceArray, 0, corrLength);
                    //第二个波形的点数
                    for (int k = 0; k < (ww[i].Length - corrLength); k++)
                    {
                        double[] targetArray = new double[corrLength];
                        Array.Copy(ww[i], k, targetArray, 0, corrLength);
                        double cor = Correlation.Pearson(sourceArray, targetArray);
                        if (lastCorr != 1 && lastCorr < cor)
                        {
                            lastCorr = cor;
                            index    = j - k;
                        }
                    }
                    if (lastCorr > 0.8)
                    {
                        if (findCount <= 0)//当前通道找到,进行下一个通道查找
                        {
                            findCount++;
                            break;
                        }
                        if (findCount >= 1 && retVal == index)//查找到,并且与前一次的记录值相同,认定为已经找到了,否则认为不稳定,继续查找
                        {
                            return(retVal);
                        }
                        else//第一次查找到,记录
                        {
                            retVal = index;
                        }
                    }
                }
            }

            return(retVal);
        }
コード例 #13
0
        protected void checkCorrelationComplex(IEnumerable <double> vectorA, IEnumerable <double> vectorB)
        {
            var correlationByPearsman = Math.Abs(Correlation.Pearson(vectorA, vectorB));
            var correlationBySpearman = Math.Abs(Correlation.Spearman(vectorA, vectorB));
            var autoCorrelationValue  = Correlation.Auto(vectorB.ToArray());

            Warn.If(correlationByPearsman < 0.7, $"Weak correlation by pearsman, was {correlationByPearsman}");
            Warn.If(correlationBySpearman < 0.7, $"Weak correlation by Spearman, was {correlationBySpearman}");
            Console.WriteLine($"{nameof(correlationByPearsman)}: {correlationByPearsman}");
            Console.WriteLine($"{nameof(correlationBySpearman)}: {correlationBySpearman}");
            var st = new StackTrace();
            var sf = st.GetFrame(1);

            Console.WriteLine($"Complex correlation check for {sf.GetMethod()} done");
        }
コード例 #14
0
        /// <summary>
        /// Estimates the autocorrelations.
        /// </summary>
        /// <returns></returns>
        private List <double> EstimateAutocorrelations()
        {
            List <double> correlations = new List <double>();

            var currentSeries = sSmootherWindow.ToList().GetRange(0, _correlationWidth);

            for (int lag = 1; lag <= _longPeriod; lag++)
            {
                var laggedSeries = sSmootherWindow.ToList().GetRange(lag, _correlationWidth);

                double pearson = Correlation.Pearson(currentSeries, laggedSeries);
                correlations.Add(pearson);
            }
            return(correlations);
        }
コード例 #15
0
        private static (List <double>, List <double>) GenerateUncorelatedPnSequences(int length)
        {
            var randomOne      = new Random(PN_SEQUENCE_SEED);
            var randomZero     = new Random(PN_SEQUENCE_SEED + 1);
            var pnSequenceZero = GeneratePnSequence(length, randomOne);
            var pnSequenceOne  = GeneratePnSequence(length, randomZero);

            while (Correlation.Pearson(pnSequenceZero, pnSequenceOne) > 0.3 ||
                   Correlation.Pearson(pnSequenceZero, pnSequenceOne) < -0.3)
            {
                pnSequenceZero = GeneratePnSequence(length, randomOne);
                pnSequenceOne  = GeneratePnSequence(length, randomZero);
            }

            return(pnSequenceZero, pnSequenceOne);
        }
コード例 #16
0
        //void Test()
        //{
        //    for (int i = 0; i < Clusters1.Count; i++)
        //    {
        //        pearsonResult.Add(new List<double>());
        //        spearmenResult.Add(new List<double>());
        //        for (int y = 0; y < Clusters1.Count; y++)
        //        {

        //            ((List<double>)pearsonResult[i]).Add(Math.Round(Correlation.Pearson(Clusters1[i], Clusters1[y]), 2));
        //            ((List<double>)spearmenResult[i]).Add(Math.Round(Correlation.Spearman(Clusters1[i], Clusters1[y]), 2));
        //            //double pearson = Correlation.Pearson(Clusters[0], Clusters[1]);
        //        }
        //    }
        //}


        static void trr(int iter)
        {
            //for (int i = 0; i < iter; i++)
            //{
            //pearsonResult1.Add(new List<double>());
            //spearmenResult1.Add(new List<double>());
            for (int y = 0; y < Clusters1.Count; y++)
            {
                pearsonResult1.Add(Correlation.Pearson(Clusters1[iter], Clusters1[y]));
                spearmenResult1.Add(Correlation.Spearman(Clusters1[iter], Clusters1[y]));
                //((List<double>)pearsonResult1[iter]).Add(Correlation.Pearson(Clusters1[iter], Clusters1[y]));
                //((List<double>)spearmenResult1[iter]).Add(Correlation.Spearman(Clusters1[iter], Clusters1[y]));
                //double pearson = Correlation.Pearson(Clusters[0], Clusters[1]);
            }
            //}
        }
コード例 #17
0
        public int BestCorrelation(int index)
        {
            Double maxCorr    = -1;
            int    maxCorrIdx = 0;
            Double currentCorr;

            for (int i = 0; i < this.getNumOfColumns(); i++)
            {
                currentCorr = Math.Abs(Correlation.Pearson(this.getColumn(index), this.getColumn(i)));
                if (currentCorr > maxCorr && i != index)
                {
                    maxCorr    = currentCorr;
                    maxCorrIdx = i;
                }
            }
            return(maxCorrIdx);
        }
コード例 #18
0
        public void CalcFitnessCorrelationCoefficient()
        {
            var parentFitnessVDLS   = new List <double>();
            var childrenFitnessVDLS = new List <double>();

            //Console.WriteLine("CalcFitnessCorrelationCoefficient");
            //Console.WriteLine("--------------------------------------");

            foreach (var p in CurrentPopulation) //individual fitness parent with vdls
            {
                parentFitnessVDLS.Add(p.GetConflicts());
            }

            var childPopulationNOVDLS = new Graph[PopulationSize];

            for (var i = 0; i < PopulationSize; i += 2) //generate all children
            {
                var c1 = CrossoverGPX(CurrentPopulation[i], CurrentPopulation[i + 1], GraphSize, ColorsCount);
                var c2 = CrossoverGPX(CurrentPopulation[i], CurrentPopulation[i + 1], GraphSize, ColorsCount);
                childPopulationNOVDLS[i]     = c1;
                childPopulationNOVDLS[i + 1] = c2;
            }

            var childPopulationVDLS = childPopulationNOVDLS;

            for (var i = 0; i < PopulationSize; i++)
            {
                VDSL(childPopulationVDLS[i]);
            }

            foreach (var c in childPopulationVDLS) //individual fitnesschildren with vdls
            {
                childrenFitnessVDLS.Add(c.GetConflicts());
            }

            //Covariance parent child NOVDLS
            //Covariance parent child VDLS


            var cnt = Correlation.Pearson(parentFitnessVDLS, childrenFitnessVDLS);

            Console.WriteLine(cnt);
        }
コード例 #19
0
        public double GetCorrelation(string symbol1, string symbol2)
        {
            double result = 0;

            try
            {
                List <CorrelUnit> array1 = new List <CorrelUnit>();
                List <CorrelUnit> array2 = new List <CorrelUnit>();
                List <double>     data1  = new List <double>();
                List <double>     data2  = new List <double>();

                array1 = db.Stocks.Where(s => s.Symbol == symbol1)
                         .Select(s => new CorrelUnit()
                {
                    Date = s.Date, Value = s.Close
                }).ToList();

                array2 = db.Stocks.Where(s => s.Symbol == symbol2)
                         .Select(s => new CorrelUnit()
                {
                    Date = s.Date, Value = s.Close
                }).ToList();

                var join = from q in array1
                           join a in array2
                           on q.Date equals a.Date
                           select new { Date = q.Date, Value1 = q.Value, Value2 = a.Value };

                foreach (var d in join)
                {
                    data1.Add(Convert.ToDouble(d.Value1));
                    data2.Add(Convert.ToDouble(d.Value2));
                }

                result = Correlation.Pearson(data1, data2);
            }
            catch
            {
                result = 0;
            }

            return(result);
        }
コード例 #20
0
ファイル: BacteriaMerger.cs プロジェクト: KristofferK/mi-aau
        public static MergedBacteria MergeBacteria(Bacterium b1, Bacterium b2)
        {
            var merged = new MergedBacteria()
            {
                Bacterium1 = b1,
                Bacterium2 = b2
            };

            merged.Points = new List <Tuple <double, double> >();
            for (var i = 0; i < b1.Measurements.Count; i++)
            {
                merged.Points.Add(new Tuple <double, double>(b1.Measurements[i], b2.Measurements[i]));
            }

            merged.CosineDistance      = 1 - Distance.Cosine(b1.Measurements.ToArray(), b2.Measurements.ToArray());
            merged.SpearmanCorrelation = Correlation.Spearman(b1.Measurements, b2.Measurements);
            merged.PearsonCorrelation  = Correlation.Pearson(b1.Measurements, b2.Measurements);

            return(merged);
        }
コード例 #21
0
        public void SampleTest(double[] sdv, double[] mean, double rho, int seed)
        {
            var pdfLn  = new DensityLn <double[]>(x => LogDen(x, sdv, mean, rho));
            var hybrid = new HybridMC(new double[2] {
                0, 0
            }, pdfLn, 10, 0.1, 1000, new double[] { 1, 1 }, new System.Random(seed))
            {
                BurnInterval = 0
            };

            const int sampleSize = 10000;

            double[][] sample     = hybrid.Sample(sampleSize);
            double[][] newSamples = ArrangeSamples(sampleSize, sample);

            var convergence = new double[2];
            var sampleMean  = new double[2];
            var sampleSdv   = new double[2];

            for (int i = 0; i < 2; i++)
            {
                convergence[i] = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => x[i]));
                var stats = new DescriptiveStatistics(newSamples[i]);
                sampleMean[i] = stats.Mean;
                sampleSdv[i]  = stats.StandardDeviation;
            }

            double sampleRho = Correlation.Pearson(newSamples[0], newSamples[1]);

            for (int i = 0; i < 2; i++)
            {
                string index = i.ToString();
                Assert.AreEqual(mean[i], sampleMean[i], 10 * convergence[i], index + "Mean");
                Assert.AreEqual(sampleSdv[i] * sampleSdv[i], sdv[i] * sdv[i], 10 * convergence[i], index + "Standard Deviation");
            }

            double convergenceRho = 1 / Math.Sqrt(MCMCDiagnostics.EffectiveSize(sample, x => (x[0] - sampleMean[0]) * (x[1] - sampleMean[1])));

            Assert.AreEqual(sampleRho * sampleSdv[0] * sampleSdv[1], rho * sdv[0] * sdv[1], 10 * convergenceRho, "Rho");
        }
コード例 #22
0
ファイル: MCMCDiagnostics.cs プロジェクト: Xornent/simula
        /// <summary>
        /// Computes the auto correlations of a series evaluated by a function f.
        /// </summary>
        /// <param name="series">The series for computing the auto correlation.</param>
        /// <param name="lag">The lag in the series</param>
        /// <param name="f">The function used to evaluate the series.</param>
        /// <returns>The auto correlation.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Throws if lag is zero or if lag is
        /// greater than or equal to the length of Series.</exception>
        public static double ACF <T>(IEnumerable <T> series, int lag, Func <T, double> f)
        {
            if (lag < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(lag), "Lag must be positive");
            }

            int length = series.Count();

            if (lag >= length)
            {
                throw new ArgumentOutOfRangeException(nameof(lag), "Lag must be smaller than the sample size");
            }

            var transformedSeries = series.Select(f);

            var enumerable   = transformedSeries as double[] ?? transformedSeries.ToArray();
            var firstSeries  = enumerable.Take(length - lag);
            var secondSeries = enumerable.Skip(lag);

            return(Correlation.Pearson(firstSeries, secondSeries));
        }
コード例 #23
0
        /// <summary>
        /// Computes the auto correlations of a series evaluated by a function f.
        /// </summary>
        /// <param name="series">The series for computing the auto correlation.</param>
        /// <param name="lag">The lag in the series</param>
        /// <param name="f">The function used to evaluate the series.</param>
        /// <returns>The auto correlation.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Throws if lag is zero or if lag is
        /// greater than or equal to the length of Series.</exception>
        static public double ACF <T>(IEnumerable <T> series, int lag, Func <T, double> f)
        {
            if (lag < 0)
            {
                throw new ArgumentOutOfRangeException("lag", Resources.LagMustBePositive);
            }

            int length = series.Count();

            if (lag >= length)
            {
                throw new ArgumentOutOfRangeException("lag", Resources.LagMustBeSmallerThanTheSampleSize);
            }

            var transformedSeries = series.Select(f);

            var enumerable   = transformedSeries as double[] ?? transformedSeries.ToArray();
            var firstSeries  = enumerable.Take(length - lag);
            var secondSeries = enumerable.Skip(lag);

            return(Correlation.Pearson(firstSeries, secondSeries));
        }
コード例 #24
0
        public static double ComputeSpo2(List <MeasureModel> ir, List <MeasureModel> r)
        {
            double coeff = Correlation.Pearson(ir.Select(n => n.Value), r.Select(n => n.Value));

            if (coeff > .97)
            {
                double rmsR  = Statistics.RootMeanSquare(r.Select(n => n.Value));
                double rmsIR = Statistics.RootMeanSquare(ir.Select(n => n.Value));

                double meanR  = Statistics.Mean(r.Select(n => n.Value));
                double meanIr = Statistics.Mean(ir.Select(n => n.Value));

                double z = (rmsR / meanR) / (rmsIR / meanIr);

                double spo2 = -45.060 * z * z + 30.354 * z + 94.845;

                return(spo2);
            }
            else
            {
                return(-1);
            }
        }
コード例 #25
0
        /// <summary>
        /// Computes the auto correlations of a series evaluated by a function f.
        /// </summary>
        /// <param name="Series">The series for computing the auto correlation.</param>
        /// <param name="lag">The lag in the series</param>
        /// <param name="f">The function used to evaluate the series.</param>
        /// <returns>The auto correlation.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Throws if lag is zero or if lag is
        /// greater than or equal to the length of Series.</exception>
        static public double ACF <T>(IEnumerable <T> Series, int lag, Func <T, double> f)
        {
            if (lag < 0)
            {
                throw new ArgumentOutOfRangeException("Lag must be positive");
            }

            int Length = Series.Count();

            if (lag >= Length)
            {
                throw new ArgumentOutOfRangeException("Lag must be smaller than the sample size");
            }

            var TransformedSeries = from data in Series
                                    select f(data);

            var FirstSeries = TransformedSeries.Take(Length - lag);

            var SecondSeries = TransformedSeries.Skip(lag);

            return(Correlation.Pearson(FirstSeries, SecondSeries));
        }
コード例 #26
0
ファイル: Tasks.cs プロジェクト: DrAnaconda/ML.Experiments
        public async Task splitDatasetIntoGroupsForAnalyze()
        {
            var fullDataset = await dataParser.readFillMergeData(true, false, false, false);

            var daysGroups = fullDataset.Values.GroupBy(key => new { key.year, key.month, key.day }).Select(x => x.ToList());
            var results    = new List <DataGroupCheck>(daysGroups.Count());

            foreach (var day in daysGroups)
            {
                var minutesAfterMidnight = day.Select(x => (double)x.minutesAfterMidnight);
                var hrValues             = day.Select(x => (double)x.HRValue);
                var pearcman             = Correlation.Pearson(minutesAfterMidnight, hrValues);
                var spearman             = Correlation.Spearman(minutesAfterMidnight, hrValues);
                var currentDayObject     = new DataGroupCheck()
                {
                    innerData = day, pearsmanValue = pearcman, spearsmanValue = spearman
                };
                results.Add(currentDayObject);
                File.WriteAllText($"{day[0].year}-{day[0].month}-{day[0].day}.json",
                                  JsonConvert.SerializeObject(currentDayObject, Formatting.Indented));
            }
            var test = results.OrderBy(x => x.spearsmanValue);
        }
コード例 #27
0
ファイル: Rolling.cs プロジェクト: vqbridge/Lean
        /// <summary>
        /// Calculate the rolling beta with the given window size (in days)
        /// </summary>
        /// <param name="equityCurve">The equity curve you want to measure beta for</param>
        /// <param name="benchmarkSeries">The benchmark/series you want to calculate beta with</param>
        /// <param name="windowSize">Days/window to lookback</param>
        /// <returns>Rolling beta</returns>
        public static Series <DateTime, double> Beta(Series <DateTime, double> equityCurve, Series <DateTime, double> benchmarkSeries, int windowSize = 132)
        {
            var dailyReturnsSeries = equityCurve.PercentChange().ResampleEquivalence(date => date.Date, s => s.Sum());
            var benchmarkReturns   = benchmarkSeries.PercentChange().ResampleEquivalence(date => date.Date, s => s.Sum());

            var returns = Frame.CreateEmpty <DateTime, string>();

            returns["strategy"] = dailyReturnsSeries;
            returns             = returns.Join("benchmark", benchmarkReturns)
                                  .FillMissing(Direction.Forward)
                                  .DropSparseRows();

            var correlation = returns
                              .Window(windowSize)
                              .SelectValues(x => Correlation.Pearson(x["strategy"].Values, x["benchmark"].Values));

            var portfolioStandardDeviation = dailyReturnsSeries.Window(windowSize).SelectValues(s => s.StdDev());
            var benchmarkStandardDeviation = benchmarkReturns.Window(windowSize).SelectValues(s => s.StdDev());

            return((correlation * (portfolioStandardDeviation / benchmarkStandardDeviation))
                   .FillMissing(Direction.Forward)
                   .DropMissing());
        }
コード例 #28
0
        public static double CalculateAcf(double[] data, int lag)
        {
            var acc    = 0.0;
            var length = data.Length;

            if (lag < 0)
            {
                throw new ArgumentException("Negative Lags are not allowed");
            }
            if (lag > length)
            {
                throw new ArgumentException("Lag exceeds sample size");
            }
            if (lag == 0)
            {
                acc = 1.0;
            }
            else
            {
                acc = Correlation.Pearson(data.CopyOfRange(0, length - lag), data.CopyOfRange(lag, length));
            }
            return(acc);
        }
コード例 #29
0
        public static void getStatisticsOutcomes(double[] values, double[][] prices, double[][] outcomes, out double spearmanMin, out double spearmanMax, out double spearmanActual, out double pearsonMin, out double pearsonMax, out double pearsonActual)
        {
            if (values.Length != outcomes.Length)
            {
                throw new Exception("Arrays have to be the same size: " + values.Length + " != " + outcomes.Length);
            }

            List <double> minList    = new List <double>();
            List <double> maxList    = new List <double>();
            List <double> actualList = new List <double>();
            List <double> valuesList = new List <double>();

            for (int i = 0; i < outcomes.Length; i++)
            {
                if (outcomes[i] != null)
                {
                    double mid = (prices[i][(int)PriceDataIndeces.Ask] + prices[i][(int)PriceDataIndeces.Bid]) / 2d;

                    double minChange    = ((outcomes[i][(int)OutcomeMatrixIndices.Min] / mid) - 1d) * 100;
                    double maxChange    = ((outcomes[i][(int)OutcomeMatrixIndices.Max] / mid) - 1d) * 100;
                    double actualChange = ((outcomes[i][(int)OutcomeMatrixIndices.Actual] / mid) - 1d) * 100;

                    minList.Add(minChange);
                    maxList.Add(maxChange);
                    actualList.Add(actualChange);
                    valuesList.Add(values[i]);
                }
            }

            spearmanMin    = Correlation.Spearman(valuesList, minList);
            spearmanMax    = Correlation.Spearman(valuesList, maxList);
            spearmanActual = Correlation.Spearman(valuesList, actualList);

            pearsonMin    = Correlation.Pearson(valuesList, minList);
            pearsonMax    = Correlation.Pearson(valuesList, maxList);
            pearsonActual = Correlation.Pearson(valuesList, actualList);
        }
コード例 #30
0
        //Takes in weights, phi challenges and target response bits
        public override double ObjFunValue(double[] weightVector, double[][] phiChallenges, double[][] reliabilityTargets)
        {
            int sampleNumber = phiChallenges.Length; //Number of challenge-relability pairs (number of training samples)

            double[] modelReliability = new double[sampleNumber];
            double[] trueReliability  = new double[sampleNumber];
            for (int currentSample = 0; currentSample < sampleNumber; currentSample++)
            {
                modelReliability[currentSample] = ComputeReliabilityFromModel(weightVector, phiChallenges[currentSample]);
                trueReliability[currentSample]  = reliabilityTargets[currentSample][0];
            }
            //double acc = DataGeneration.PearsonCorrelationCoefficient(modelReliability, trueReliability);
            double acc = Correlation.Pearson(trueReliability, modelReliability);

            //try to turn it into an optimization problem
            //if (acc < 0)
            //{
            //    acc = -1.0 * acc; //fl
            //    //return acc = double.MaxValue;
            //}
            //if (acc == 0)
            //{
            //    return acc;
            //}
            //else
            //{
            //    acc = 1.0 / acc;
            //}
            //acc = 1.0 / (acc * acc);
            //if (acc < 0)
            //{
            //    acc = acc * -1.0;
            //}
            //acc = 1.0 / (acc * acc);
            return(acc);
        }