Ejemplo n.º 1
0
 public static void Interpolation(Altaxo.Data.DataColumn xCol, Altaxo.Data.DataColumn yCol,
                                  InterpolationParameters parameters,
                                  Altaxo.Data.DataColumn xRes, Altaxo.Data.DataColumn yRes)
 {
     Interpolation(
         xCol, yCol,
         parameters.InterpolationInstance,
         VectorMath.CreateEquidistantSequenceByStartEndLength(parameters.XOrg, parameters.XEnd, parameters.NumberOfPoints),
         xRes, yRes);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the column frequencies.
        /// </summary>
        /// <param name="numberOfResultColumnFrequencies">The number of resulting column frequencies. Use the value <see cref="NumberOfColumns"/> to get all frequencies, or a value less than that to get only a part of the frequencies.</param>
        /// <returns>Vector that accomodates the column frequencies.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">NumberOfResultRows has to be less than or equal to the existing number of rows.</exception>
        public IROVector <double> GetColumnFrequencies(int numberOfResultColumnFrequencies)
        {
            if (numberOfResultColumnFrequencies > NumberOfColumns)
            {
                throw new ArgumentOutOfRangeException(string.Format("numberOfResultRows has to be less than or equal to the existing number of rows"));
            }

            var endFreq = 0.5 / (_columnSpacing.HasValue ? _columnSpacing.Value : 1);

            return(VectorMath.CreateEquidistantSequenceByStartEndLength(0, endFreq, numberOfResultColumnFrequencies));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        /// <param name="bw"></param>
        /// <param name="bwSel"></param>
        /// <param name="adjust"></param>
        /// <param name="kernel"></param>
        /// <param name="weights"></param>
        /// <param name="width"></param>
        /// <param name="widthSel"></param>
        /// <param name="n"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="cut"></param>
        /// <remarks>Adapted from the R-project (www.r-project.org), Version 2.72, file density.R</remarks>
        public static ProbabilityDensityResult ProbabilityDensity(
            this IReadOnlyList <double> x,
            double bw,
            string bwSel,
            double adjust,
            ConvolutionKernel kernel,
            IReadOnlyList <double> weights,
            double width,
            string widthSel,
            int n,
            double from,
            double to,
            double cut // default: 3
            )
        {
            double wsum;

            if (null == weights)
            {
                weights = VectorMath.GetConstantVector(1.0 / x.Count, x.Count);
                wsum    = 1;
            }
            else
            {
                wsum = weights.Sum();
            }

            double totMass = 1;

            int n_user = n;

            n = Math.Max(n, 512);
            if (n > 512)
            {
                n = BinaryMath.NextPowerOfTwoGreaterOrEqualThan(n);
            }

            if (bw.IsNaN() && !(width.IsNaN() && null == widthSel))
            {
                if (!width.IsNaN())
                {
                    // S has width equal to the length of the support of the kernel
                    // except for the gaussian where it is 4 * sd.
                    // R has bw a multiple of the sd.
                    double fac = 1;
                    switch (kernel)
                    {
                    case ConvolutionKernel.Gaussian:
                        fac = 4;
                        break;

                    case ConvolutionKernel.Rectangular:
                        fac = 2 * Math.Sqrt(3);
                        break;

                    case ConvolutionKernel.Triangular:
                        fac = 2 * Math.Sqrt(6);
                        break;

                    case ConvolutionKernel.Epanechnikov:
                        fac = 2 * Math.Sqrt(5);
                        break;

                    case ConvolutionKernel.Biweight:
                        fac = 2 * Math.Sqrt(7);
                        break;

                    case ConvolutionKernel.Cosine:
                        fac = 2 / Math.Sqrt(1 / 3 - 2 / (Math.PI * Math.PI));
                        break;

                    case ConvolutionKernel.Optcosine:
                        fac = 2 / Math.Sqrt(1 - 8 / (Math.PI * Math.PI));
                        break;

                    default:
                        throw new ArgumentException("Unknown convolution kernel");
                    }
                    bw = width / fac;
                }
                else
                {
                    bwSel = widthSel;
                }
            }

            if (null != bwSel)
            {
                if (x.Count < 2)
                {
                    throw new ArgumentException("need at least 2 points to select a bandwidth automatically");
                }
                switch (bwSel.ToLowerInvariant())
                {
                case "nrd0":
                    //nrd0 = bw.nrd0(x),
                    break;

                case "nrd":
                    //nrd = bw.nrd(x),
                    break;

                case "ucv":
                    //ucv = bw.ucv(x),
                    break;

                case "bcv":
                    //bcv = bw.bcv(x),
                    break;

                case "sj":
                    //sj = , "sj-ste" = bw.SJ(x, method="ste"),
                    break;

                case "sj-dpi":
                    //"sj-dpi" = bw.SJ(x, method="dpi"),
                    break;

                default:
                    throw new ArgumentException("Unknown bandwith selection rule: " + bwSel.ToString());
                }
            }

            if (!RMath.IsFinite(bw))
            {
                throw new ArithmeticException("Bandwidth is not finite");
            }

            bw = adjust * bw;

            if (!(bw > 0))
            {
                throw new ArithmeticException("Bandwith is not positive");
            }

            if (from.IsNaN())
            {
                from = x.Min() - cut * bw;
            }
            if (to.IsNaN())
            {
                to = x.Max() + cut * bw;
            }

            if (!RMath.IsFinite(from))
            {
                throw new ArithmeticException("non-finite 'from'");
            }
            if (!to.IsFinite())
            {
                throw new ArithmeticException("non-finite 'to'");
            }
            double lo = from - 4 * bw;
            double up = to + 4 * bw;

            var y = new DoubleVector(2 * n);

            MassDistribution(x, weights, lo, up, y, n);
            y.Multiply(totMass);

            var kords = new DoubleVector(2 * n);

            kords.FillWithLinearSequenceGivenByStartAndEnd(0, 2 * (up - lo));

            for (int i = n + 1, j = n - 1; j >= 0; i++, j--)
            {
                kords[i] = -kords[j];
            }

            switch (kernel)
            {
            case ConvolutionKernel.Gaussian:
                kords.Map(new Probability.NormalDistribution(0, bw).PDF);
                break;

            case ConvolutionKernel.Rectangular:
                double a = bw * Math.Sqrt(3);
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? 0.5 / a : 0); });
                break;

            case ConvolutionKernel.Triangular:
                a = bw * Math.Sqrt(6);
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? (1 - Math.Abs(xx) / a) / a : 0); });
                break;

            case ConvolutionKernel.Epanechnikov:
                a = bw * Math.Sqrt(5);
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? 0.75 * (1 - RMath.Pow2(Math.Abs(xx) / a)) / a : 0); });
                break;

            case ConvolutionKernel.Biweight:
                a = bw * Math.Sqrt(7);
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? 15.0 / 16.0 * RMath.Pow2(1 - RMath.Pow2(Math.Abs(xx) / a)) / a : 0); });
                break;

            case ConvolutionKernel.Cosine:
                a = bw / Math.Sqrt(1.0 / 3 - 2 / RMath.Pow2(Math.PI));
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? (1 + Math.Cos(Math.PI * xx / a)) / (2 * a) : 0); });
                break;

            case ConvolutionKernel.Optcosine:
                a = bw / Math.Sqrt(1 - 8 / RMath.Pow2(Math.PI));
                kords.Map(delegate(double xx)
                          { return(Math.Abs(xx) < a ? Math.PI / 4 * Math.Cos(Math.PI * xx / (2 * a)) / a : 0); });
                break;

            default:
                throw new ArgumentException("Unknown convolution kernel");
            }

            var result = new DoubleVector(2 * n);

            Fourier.FastHartleyTransform.CyclicRealConvolution(y.GetInternalData(), kords.GetInternalData(), result.GetInternalData(), 2 * n, null);
            y.Multiply(1.0 / (2 * n));
            VectorMath.MaxOf(y, 0, y);
            var xords = VectorMath.CreateEquidistantSequenceByStartEndLength(lo, up, n);
            var xu    = VectorMath.CreateEquidistantSequenceByStartEndLength(from, to, n_user);

            double[] res2 = new double[xu.Length];
            Interpolation.LinearInterpolation.Interpolate(xords, result, n, xu, xu.Length, 0, out res2);

            return(new ProbabilityDensityResult()
            {
                X = xu, Y = VectorMath.ToROVector(res2), Bandwidth = bw
            });
        }