Esempio n. 1
0
        private ImagePixel GetExpectedPosition(int frameNo)
        {
            ImagePixel rv = null;

            var intervalValues = new Dictionary<int, List<ImagePixel>>();
            var intervalMedians = new Dictionary<int, ImagePixel>();

            int earliestFrame = m_PastFrameNos[0];
            for (int i = 0; i < m_PastFrameNos.Count; i++)
            {
                int integrationInterval = (m_PastFrameNos[i] - earliestFrame) / m_MeasurementContext.IntegratedFramesCount;

                List<ImagePixel> intPoints;
                if (!intervalValues.TryGetValue(integrationInterval, out intPoints))
                {
                    intPoints = new List<ImagePixel>();
                    intervalValues.Add(integrationInterval, intPoints);
                }

                intPoints.Add(new ImagePixel(m_PastFramePosX[i], m_PastFramePosY[i]));
            }

            var calcBucketX = new List<double>();
            var calcBucketY = new List<double>();
            foreach (int key in intervalValues.Keys)
            {
                calcBucketX.Clear();
                calcBucketY.Clear();

                intervalValues[key].ForEach(v =>
                {
                    calcBucketX.Add(v.XDouble);
                    calcBucketY.Add(v.YDouble);
                });

                double xMed = calcBucketX.Median();
                double yMed = calcBucketY.Median();

                intervalMedians.Add(key, new ImagePixel(xMed, yMed));
            }

            var xMotion = new LinearRegression();
            var yMotion = new LinearRegression();

            foreach (int intInt in intervalMedians.Keys)
            {
                long t = intInt;
                double x = intervalMedians[intInt].XDouble;
                double y = intervalMedians[intInt].YDouble;
                if (x > 0 && y > 0)
                {
                    xMotion.AddDataPoint(t, x);
                    yMotion.AddDataPoint(t, y);
                }
            }

            try
            {
                xMotion.Solve();
                yMotion.Solve();

                int currIntInterval = (frameNo - earliestFrame) / m_MeasurementContext.IntegratedFramesCount;

                rv = new ImagePixel(xMotion.ComputeY(currIntInterval), yMotion.ComputeY(currIntInterval));
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.GetFullStackTrace());
            }

            return rv;
        }
Esempio n. 2
0
        public void Test1()
        {
            // Based on https://www.medcalc.org/manual/weighted-regression-worked-example.php

            var reg = new LinearRegression();

            double[] x_values = new double[]
            {
                27, 21, 22, 24, 25, 23, 20, 20, 29, 24, 25, 28, 26, 38, 32, 33, 31, 34, 37, 38, 33, 35, 30, 31, 37, 39, 46,
                49, 40, 42, 43, 46, 43, 44, 46, 47, 45, 49, 48, 40, 42, 55, 54, 57, 52, 53, 56, 52, 50, 59, 50, 52, 58,
                57
            };

            double[] y_values = new double[]
            {
                73, 66, 63, 75, 71, 70, 65, 70, 79, 72, 68, 67, 79, 91, 76, 69, 66, 73, 78, 87, 76, 79, 73, 80, 68, 75, 89,
                101, 70, 72, 80, 83, 75, 71, 80, 96, 92, 80, 70, 90, 85, 76, 71, 99, 86, 79, 92, 85, 71, 90, 91, 100, 80,
                109
            };

            for (int i = 0; i < x_values.Length; i++)
            {
                reg.AddDataPoint(x_values[i], y_values[i]);
            }
            reg.Solve();

            Assert.AreEqual(0.5800, reg.A, 0.0001);
            Assert.AreEqual(56.1569, reg.B, 0.0001);
            Assert.AreEqual(8.1457, reg.StdDev, 0.0001);
            Assert.AreEqual(0.09695, reg.Uncertainty_A, 0.0001);
            Assert.AreEqual(3.9937, reg.Uncertainty_B, 0.0001);

            var residuals = reg.Residuals.ToArray();

            var reg2 = new LinearRegression();
            for (int i = 0; i < x_values.Length; i++)
            {
                reg2.AddDataPoint(x_values[i], Math.Abs(residuals[i]));
            }
            reg2.Solve();

            Assert.AreEqual(0.1982, reg2.A, 0.0001);
            Assert.AreEqual(-1.5495, reg2.B, 0.0001);
            Assert.AreEqual(4.4606, reg2.StdDev, 0.0001);
            Assert.AreEqual(0.05309, reg2.Uncertainty_A, 0.0001);
            Assert.AreEqual(2.1869, reg2.Uncertainty_B, 0.0001);

            var predictedValues = new double[x_values.Length];

            for (int i = 0; i < x_values.Length; i++)
            {
                predictedValues[i] = reg2.ComputeY(x_values[i]);
            }

            var reg3 = new LinearRegression();
            var factor = 1;
            for (int i = 0; i < x_values.Length; i++)
            {
                double weight = factor / (predictedValues[i] * predictedValues[i]);
                reg3.AddDataPoint(x_values[i], y_values[i], weight);
            }
            reg3.Solve();

            Assert.AreEqual(0.5963, reg3.A, 0.0001);
            Assert.AreEqual(55.5658, reg3.B, 0.0001);
            Assert.AreEqual(1.2130, reg3.StdDevUnscaled, 0.0001);
        }
Esempio n. 3
0
        public void Test4()
        {
            // Based on https://onlinecourses.science.psu.edu/stat501/node/397

            double[] x_values = new double[] { 16, 14, 22, 10, 14, 17, 10, 13, 19, 12, 18, 11 };
            double[] y_values = new double[] { 77, 70, 85, 50, 62, 70, 55, 63, 88, 57, 81, 51 };

            var reg_ols = new LinearRegression();

            for (int i = 0; i < x_values.Length; i++)
            {
                reg_ols.AddDataPoint(x_values[i], y_values[i]);
            }

            reg_ols.Solve();

            Assert.AreEqual(3.269, reg_ols.A, 0.001);
            Assert.AreEqual(19.47, reg_ols.B, 0.01);
            Assert.AreEqual(4.5983, reg_ols.StdDev, 0.0001);
            Assert.AreEqual(0.365, reg_ols.Uncertainty_A, 0.001);
            Assert.AreEqual(5.52, reg_ols.Uncertainty_B, 0.01);

            var residuals = reg_ols.Residuals.ToArray();

            var reg2 = new LinearRegression();
            for (int i = 0; i < x_values.Length; i++)
            {
                reg2.AddDataPoint(x_values[i], Math.Abs(residuals[i]));
            }
            reg2.Solve();

            var predictedValues = new double[x_values.Length];

            for (int i = 0; i < x_values.Length; i++)
            {
                predictedValues[i] = reg2.ComputeY(x_values[i]);
            }

            var reg_wls = new LinearRegression();
            var factor = 1;
            for (int i = 0; i < x_values.Length; i++)
            {
                double weight = factor / (predictedValues[i] * predictedValues[i]);
                reg_wls.AddDataPoint(x_values[i], y_values[i], weight);
            }
            reg_wls.Solve();

            Assert.AreEqual(3.421, reg_wls.A, 0.001);
            Assert.AreEqual(17.30, reg_wls.B, 0.01);
            Assert.AreEqual(1.15935, reg_wls.StdDevUnscaled, 0.00001);
        }
Esempio n. 4
0
        private void LinearFitOfAveragedModel(uint[,] intensity)
        {
            if (m_BackgroundModel != null)
                throw new NotSupportedException("Background modelling cannot be used directly with linear fit of average model.");

            // First do a non linear fit to find X0 and Y0
            NonLinearFit(intensity, false);

            if (m_IsSolved)
            {
                // Then do a linear fit to find IBackground and IStarMax
                // I(x, y) = IBackground + IStarMax * Exp ( -((x - X0)*(x - X0) + (y - Y0)*(y - Y0)) / (r0 * r0))
                LinearRegression linearFit = new LinearRegression();

                double modelR = m_ModelFWHM/(2*Math.Sqrt(Math.Log(2)));
                double modelRSquare = modelR*modelR;
                double[,] modelData = new double[m_MatrixSize, m_MatrixSize];

                for (int x = 0; x < m_MatrixSize; x++)
                    for (int y = 0; y < m_MatrixSize; y++)
                    {
                        double modelVal = Math.Exp(-((x - X0)*(x - X0) + (y - Y0)*(y - Y0))/(modelRSquare));
                        modelData[x, y] = modelVal;
                        linearFit.AddDataPoint(modelVal, intensity[x, y]);
                    }
                linearFit.Solve();

                for (int x = 0; x < m_MatrixSize; x++)
                    for (int y = 0; y < m_MatrixSize; y++)
                    {
                        m_Residuals[x, y] = intensity[x, y] - linearFit.ComputeY(modelData[x, y]);
                    }

                m_IBackground = linearFit.B;
                m_IStarMax = linearFit.A;
                m_R0 = modelR;
            }
        }
        private void PerformLinearFitReadingsNormalisation(ref List<double> normalIndexes)
        {
            var linearRegression = new LinearRegression();

            foreach (LCMeasurement reading in m_LightCurveController.Context.AllReadings[m_LightCurveController.Context.Normalisation])
            {
                if (reading.IsSuccessfulReading)
                    linearRegression.AddDataPoint(reading.CurrFrameNo, reading.AdjustedReading);
            }

            linearRegression.Solve();

            double firstValue = linearRegression.ComputeY(m_LightCurveController.Context.AllReadings[m_LightCurveController.Context.Normalisation][0].CurrFrameNo);

            foreach (LCMeasurement reading in m_LightCurveController.Context.AllReadings[m_LightCurveController.Context.Normalisation])
            {
                normalIndexes.Add(firstValue / linearRegression.ComputeY(reading.CurrFrameNo));
            }
        }
        private void PerformLinearFitBinnedNormalisation(ref List<double> normalIndexes)
        {
            var linearRegression = new LinearRegression();

            foreach (BinnedValue binnedValue in m_AllBinnedReadings[m_LightCurveController.Context.Normalisation])
            {
                if (binnedValue.IsSuccessfulReading)
                    linearRegression.AddDataPoint(binnedValue.BinNo, binnedValue.AdjustedValue);
            }

            linearRegression.Solve();

            double firstValue = linearRegression.ComputeY(m_AllBinnedReadings[m_LightCurveController.Context.Normalisation][0].BinNo);

            foreach (BinnedValue binnedValue in m_AllBinnedReadings[m_LightCurveController.Context.Normalisation])
            {
                normalIndexes.Add(firstValue / linearRegression.ComputeY(binnedValue.BinNo));
            }
        }