Represents a double range with minimum and maximum values.

The class represents a double range with inclusive limits - both minimum and maximum values of the range are included into it. Mathematical notation of such range is [min, max].

Sample usage:

// create [0.25, 1.5] range DoubleRange range1 = new DoubleRange( 0.25, 1.5 ); // create [1.00, 2.25] range DoubleRange range2 = new DoubleRange( 1.00, 2.25 ); // check if values is inside of the first range if ( range1.IsInside( 0.75 ) ) { // ... } // check if the second range is inside of the first range if ( range1.IsInside( range2 ) ) { // ... } // check if two ranges overlap if ( range1.IsOverlapping( range2 ) ) { // ... }
Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UniformGenerator"/> class
        /// </summary>
        /// 
        /// <param name="range">Random numbers range</param>
        /// <param name="seed">Seed value to initialize random numbers generator</param>
        /// 
        public UniformGenerator( DoubleRange range, int seed )
        {
            rand = new UniformOneGenerator( seed );

            min     = range.Min;
            length  = range.Length;
        }
        public ActivationFunctionView()
        {
            this.plotSteps = 1;
            this.plotDomain = new DoubleRange(-1, 1);

            InitializeComponent();
            CreateGraph(zedGraphControl);
        }
 public void ScaleTest2()
 {
     DoubleRange from = new DoubleRange(-100, 100);
     DoubleRange to = new DoubleRange(0, 50);
     double x = 0;
     double expected = 25;
     double actual = Tools.Scale(from, to, x);
     Assert.AreEqual(expected, actual);
 }
Beispiel #4
0
 public void AddColor(HSL selectedColor,IntRange Hue_range, DoubleRange Saturation_range, DoubleRange Luminance_range)
 {
     TreeNode node = new TreeNode(new String(' ',100));
         node.Tag = new LayerColor(selectedColor, Hue_range, Saturation_range, Luminance_range);
         node.Checked = true;
         new_color_parent.Nodes.Add(node);
         new_color_parent.ExpandAll();
         treeLayers.SelectedNode = node;
         UpdateFilter();
 }
Beispiel #5
0
        public LayerColor(HSL _color, IntRange _hue_range, DoubleRange _sat_range, DoubleRange _lum_range)
        {
            color = _color;
            hue_range = _hue_range;
            sat_range = _sat_range;
            lum_range = _lum_range;

            RGB rgb = new RGB();
            AForge.Imaging.ColorConverter.HSL2RGB(color, rgb);
            RGBColor = rgb.Color;
        }
        /// <summary>
        ///   Creates a new <see cref="GeneralContinuousDistribution"/> with the given PDF and CDF functions.
        /// </summary>
        /// 
        /// <param name="support">The distribution's support over the real line.</param>
        /// <param name="density">A probability density function.</param>
        /// <param name="distribution">A cumulative distribution function.</param>
        /// 
        public GeneralContinuousDistribution(DoubleRange support,
            Func<double, double> density, Func<double, double> distribution)
        {
            if (density == null)
                throw new ArgumentNullException("density");

            if (distribution == null)
                throw new ArgumentNullException("distribution");

            this.pdf = density;
            this.cdf = distribution;
            this.support = support;
        }
Beispiel #7
0
        /// <summary>
        ///   Estimates suitable values for the sigmoid kernel
        ///   by exploring the response area of the tanh function.
        /// </summary>
        /// 
        /// <param name="inputs">An input data set.</param>
        /// <param name="samples">The size of the subset to use in the estimation.</param>
        /// <param name="range">The interquartile range for the data.</param>
        /// 
        /// <returns>A Sigmoid kernel initialized with appropriate values.</returns>
        /// 
        public static Sigmoid Estimate(double[][] inputs, int samples, out DoubleRange range)
        {
            if (samples > inputs.Length)
                throw new ArgumentOutOfRangeException("samples");

            double[] distances = Products(inputs, samples);

            double q1 = Math.Sqrt(distances[(int)Math.Ceiling(0.15 * distances.Length)] / 2.0);
            double q9 = Math.Sqrt(distances[(int)Math.Ceiling(0.85 * distances.Length)] / 2.0);
            double qm = Math.Sqrt(Accord.Statistics.Tools.Median(distances, alreadySorted: true) / 2.0);

            range = new DoubleRange(q1, q9);

            double max = qm;

            double r = -Math.E;
            double a = -r / max;

            return new Sigmoid(a, r);
        }
Beispiel #8
0
        // Worker thread
        void SearchSolution()
        {
            // number of learning samples
            int samples = data.GetLength(0);

            // prepare learning data
            DoubleRange unit = new DoubleRange(-1, 1);
            double[][] input = Tools.Scale(from: xRange, to: unit, x: data.GetColumn(0)).ToArray();
            double[][] output = Tools.Scale(from: yRange, to: unit, x: data.GetColumn(1)).ToArray();


            // create multi-layer neural network
            ActivationNetwork network = new ActivationNetwork(
                new BipolarSigmoidFunction(sigmoidAlphaValue),
                1, neuronsInFirstLayer, 1);

            if (useNguyenWidrow)
            {
                new NguyenWidrow(network).Randomize();
            }

            // create teacher
            var teacher = new ParallelResilientBackpropagationLearning(network);

            // iterations
            int iteration = 1;

            // solution array
            double[,] solution = new double[samples, 2];


            // loop
            while (!needToStop)
            {
                // run epoch of learning procedure
                double error = teacher.RunEpoch(input, output) / samples;

                // calculate solution
                for (int j = 0; j < samples; j++)
                {
                    double x = input[j][0];
                    double y = network.Compute(new[] { x })[0];
                    solution[j, 0] = Tools.Scale(from: unit, to: xRange, x: x);
                    solution[j, 1] = Tools.Scale(from: unit, to: yRange, x: y);
                }

                chart.UpdateDataSeries("solution", solution);

                // calculate error
                double learningError = 0.0;
                for (int j = 0; j < samples; j++)
                {
                    double x = input[j][0];
                    double expected = data[j, 1];
                    double actual = network.Compute(new[] { x })[0];
                    learningError += Math.Abs(expected - actual);
                }

                // set current iteration's info
                SetText(currentIterationBox, iteration.ToString());
                SetText(currentErrorBox, learningError.ToString("F3"));

                // increase current iteration
                iteration++;

                // check if we need to stop
                if ((iterations != 0) && (iteration > iterations))
                    break;
            }


            // enable settings controls
            EnableControls(true);
        }
 public bool IsInside(DoubleRange range)
 {
     return(this.IsInside(range.min) && this.IsInside(range.max));
 }
Beispiel #10
0
        // Load data
        private void loadDataButton_Click(object sender, System.EventArgs e)
        {
            // show file selection dialog
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                data = null;

                try
                {
                    // open selected file
                    using (TextReader stream = new StreamReader(openFileDialog.FileName))
                    using (CsvReader reader = new CsvReader(stream, false))
                    {
                        data = reader.ToTable().ToMatrix(CultureInfo.InvariantCulture);
                    }

                }
                catch (Exception)
                {
                    MessageBox.Show("Failed reading the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                DoubleRange[] ranges = data.Range(dimension: 0);

                xRange = ranges[0];
                yRange = ranges[1];

                // update list and chart
                UpdateDataListView();
                chart.RangeX = new Range((float)ranges[0].Min, (float)ranges[0].Max);
                chart.UpdateDataSeries("data", data);
                chart.UpdateDataSeries("solution", null);

                // enable "Start" button
                startButton.Enabled = true;
            }
        }
Beispiel #11
0
        /// <summary>
        ///   Paints the control.
        /// </summary>
        /// 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            int clientWidth = ClientRectangle.Width;
            int clientHeight = ClientRectangle.Height;

            // fill with background
            g.FillRectangle(backgroundBrush, 0, 0, clientWidth - 1, clientHeight - 1);

            // draw borders
            g.DrawRectangle(bordersPen, 0, 0, clientWidth - 1, clientHeight - 1);

            if (DesignMode)
                return;

            DoubleRange rangeClientX = new DoubleRange(0, clientWidth);
            DoubleRange rangeClientY = new DoubleRange(clientHeight, 0);

            // walk through all data series
            foreach (Waveform waveform in waveTable.Values)
            {
                // get data of the waveform
                float[] data = waveform.data;

                // check for available data
                if (data == null) continue;


                using (Pen pen = new Pen(waveform.color, waveform.width))
                {
                    if (SimpleMode)
                    {
                        int blockSize = waveform.samples / clientWidth;
                        for (int x = 0; x < clientWidth; x++)
                        {
                            double max = data.RootMeanSquare(x * blockSize, blockSize);
                            int y = clientHeight / 2 + (int)(max * clientHeight);
                            g.DrawLine(pen, x, clientHeight - y, x, y);
                        }
                    }
                    else
                    {
                        int xPrev = 0;
                        int yPrev = (int)rangeY.Scale(rangeClientY, data[0]);

                        for (int x = 0; x < clientWidth; x++)
                        {
                            int index = (int)rangeClientX.Scale(rangeX, x);
                            if (index < 0 || index >= data.Length)
                                index = data.Length - 1;
                            int y = (int)rangeY.Scale(rangeClientY, data[index]);

                            g.DrawLine(pen, xPrev, yPrev, x, y);

                            xPrev = x;
                            yPrev = y;
                        }
                    }
                }
            }
        }
Beispiel #12
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="LinearFunction"/> class.
 /// </summary>
 /// 
 public LinearFunction(DoubleRange range)
 {
     this.Range = range;
 }
 /// <summary>
 ///   Estimate appropriate values for sigma given a data set.
 /// </summary>
 /// 
 /// <remarks>
 ///   This method uses a simple heuristic to obtain appropriate values
 ///   for sigma in a radial basis function kernel. The heuristic is shown
 ///   by Caputo, Sim, Furesjo and Smola, "Appearance-based object
 ///   recognition using SVMs: which kernel should I use?", 2002.
 /// </remarks>
 /// 
 /// <param name="inputs">The data set.</param>
 /// <param name="range">The range of suitable values for sigma.</param>
 /// 
 /// <returns>A Gaussian kernel initialized with an appropriate sigma value.</returns>
 /// 
 public static Gaussian Estimate(double[][] inputs, out DoubleRange range)
 {
     return Estimate(inputs, inputs.Length, out range);
 }
Beispiel #14
0
        /// <summary>
        ///   Gets the 95% confidence interval for the
        ///   Hazard Ratio for a given coefficient.
        /// </summary>
        /// 
        /// <param name="index">
        ///   The coefficient's index.
        /// </param>
        /// 
        public DoubleRange GetConfidenceInterval(int index)
        {
            double coeff = Coefficients[index];
            double error = StandardErrors[index];

            double upper = coeff + 1.9599 * error;
            double lower = coeff - 1.9599 * error;

            DoubleRange ci = new DoubleRange(Math.Exp(lower), Math.Exp(upper));

            return ci;
        }
 /// <summary>
 ///   Creates a new <see cref="GeneralContinuousDistribution"/> 
 ///   using only a cumulative distribution function definition.
 /// </summary>
 /// 
 /// <param name="support">The distribution's support over the real line.</param>
 /// <param name="cdf">A cumulative distribution function.</param>
 /// <param name="method">The integration method to use for numerical computations.</param>
 /// 
 /// <returns>A <see cref="GeneralContinuousDistribution"/> created from the 
 /// <paramref name="cdf"/> whose measures and functions are computed using 
 /// numerical integration and differentiation.</returns>
 /// 
 public static GeneralContinuousDistribution FromDistributionFunction(
     DoubleRange support, Func<double, double> cdf, IUnivariateIntegration method)
 {
     GeneralContinuousDistribution dist = new GeneralContinuousDistribution();
     dist.support = support;
     dist.cdf = cdf;
     dist.method = method;
     return dist;
 }
 /// <summary>
 ///   Creates a new <see cref="GeneralContinuousDistribution"/> 
 ///   using only a cumulative distribution function definition.
 /// </summary>
 /// 
 /// <param name="cdf">A cumulative distribution function.</param>
 /// 
 /// <returns>A <see cref="GeneralContinuousDistribution"/> created from the 
 /// <paramref name="cdf"/> whose measures and functions are computed using 
 /// numerical integration and differentiation.</returns>
 /// 
 public static GeneralContinuousDistribution FromDistributionFunction(Func<double, double> cdf)
 {
     var range = new DoubleRange(double.NegativeInfinity, double.PositiveInfinity);
     var method = createDefaultIntegrationMethod();
     return FromDistributionFunction(range, cdf, method);
 }
Beispiel #17
0
        public void MeshTest()
        {
            DoubleRange rowRange = new DoubleRange(-1, 1);
            DoubleRange colRange = new DoubleRange(-1, 1);
            double rowSteps = 0.5f;
            double colSteps = 0.5f;
            double[][] expected = 
            {
                new double[] { -1.0, -1.0 },
                new double[] { -1.0, -0.5 },
                new double[] { -1.0,  0.0 },
                new double[] { -1.0,  0.5 },
                new double[] { -1.0,  1.0 },

                new double[] { -0.5, -1.0 },
                new double[] { -0.5, -0.5 },
                new double[] { -0.5,  0.0 },
                new double[] { -0.5,  0.5 },
                new double[] { -0.5,  1.0 },
                
                new double[] {  0.0, -1.0 },
                new double[] {  0.0, -0.5 },
                new double[] {  0.0,  0.0 },
                new double[] {  0.0,  0.5 },
                new double[] {  0.0,  1.0 },

                new double[] {  0.5, -1.0 },
                new double[] {  0.5, -0.5 },
                new double[] {  0.5,  0.0 },
                new double[] {  0.5,  0.5 },
                new double[] {  0.5,  1.0 },

                new double[] {  1.0, -1.0 },
                new double[] {  1.0, -0.5 },
                new double[] {  1.0,  0.0 },
                new double[] {  1.0,  0.5 },
                new double[] {  1.0,  1.0 },
            };

            double[][] actual = Matrix.Mesh(rowRange, colRange, rowSteps, colSteps);

            Assert.IsTrue(expected.IsEqual(actual));
        }
Beispiel #18
0
 /// <summary>
 /// Check if the specified range is inside of the range.
 /// </summary>
 ///
 /// <param name="range">Range to check.</param>
 ///
 /// <returns><b>True</b> if the specified range is inside of the range or
 /// <b>false</b> otherwise.</returns>
 ///
 public bool IsInside(DoubleRange range)
 {
     return((IsInside(range.min)) && (IsInside(range.max)));
 }
Beispiel #19
0
 /// <summary>
 /// Check if the specified range overlaps with the range.
 /// </summary>
 ///
 /// <param name="range">Range to check for overlapping.</param>
 ///
 /// <returns><b>True</b> if the specified range overlaps with the range or
 /// <b>false</b> otherwise.</returns>
 ///
 public bool IsOverlapping(DoubleRange range)
 {
     return((IsInside(range.min)) || (IsInside(range.max)) ||
            (range.IsInside(min)) || (range.IsInside(max)));
 }
        /// <summary>
        ///   Creates a new <see cref="GeneralContinuousDistribution"/> with the given PDF and CDF functions.
        /// </summary>
        /// 
        /// <param name="support">The distribution's support over the real line.</param>
        /// <param name="density">A probability density function.</param>
        /// <param name="distribution">A cumulative distribution function.</param>
        /// 
        public GeneralContinuousDistribution(DoubleRange support,
            Func<double, double> density, Func<double, double> distribution)
        {
            if (density == null)
                throw new ArgumentNullException("density");

            if (distribution == null)
                throw new ArgumentNullException("distribution");

            this.method = new InfiniteAdaptiveGaussKronrod(100);
            this.pdf = density;
            this.cdf = distribution;
            this.support = support;
        }
Beispiel #21
0
        /// <summary>
        ///   Computes the circular quartiles of the given circular angles.
        /// </summary>
        /// 
        /// <param name="angles">A double array containing the angles in radians.</param>
        /// <param name="range">The sample quartiles, as an out parameter.</param>
        /// <param name="median">The angular median, if already known.</param>
        /// <param name="wrap">
        ///   Whether range values should be wrapped to be contained in the circle. If 
        ///   set to false, range values could be returned outside the [+pi;-pi] range.
        /// </param>
        /// 
        /// <returns>The median of the given angles.</returns>
        /// 
        public static double Quartiles(double[] angles, out DoubleRange range, double median, bool wrap = true)
        {
            double q1, q3;
            double q2 = Quartiles(angles, out q1, out q3, median, wrap);

            System.Diagnostics.Debug.Assert(q2 == median);

            range = new DoubleRange(q1, q3);
            return median;
        }
 /// <summary>
 ///   Creates a new <see cref="GeneralContinuousDistribution"/> 
 ///   using only a cumulative distribution function definition.
 /// </summary>
 /// 
 /// <param name="support">The distribution's support over the real line.</param>
 /// <param name="cdf">A cumulative distribution function.</param>
 /// 
 /// <returns>A <see cref="GeneralContinuousDistribution"/> created from the 
 /// <paramref name="cdf"/> whose measures and functions are computed using 
 /// numerical integration and differentiation.</returns>
 /// 
 public static GeneralContinuousDistribution FromDistributionFunction(
     DoubleRange support, Func<double, double> cdf)
 {
     var method = createDefaultIntegrationMethod();
     return FromDistributionFunction(support, cdf, method);
 }
Beispiel #23
0
        /// <summary>
        /// Update Y range.
        /// </summary>
        private void UpdateYRange( )
        {
            double	minY = double.MaxValue;
            double	maxY = double.MinValue;

            // walk through all data series
            IDictionaryEnumerator en = seriesTable.GetEnumerator( );
            while ( en.MoveNext( ) )
            {
                DataSeries series = (DataSeries) en.Value;
                // get data of the series
                double[,] data = series.data;

                if ( ( series.updateYRange ) && ( data != null ) )
                {
                    for ( int i = 0, n = data.GetLength( 0 ); i < n; i++ )
                    {
                        double v = data[i, 1];
                        // check for max
                        if ( v > maxY )
                            maxY = v;
                        // check for min
                        if ( v < minY )
                            minY = v;
                    }
                }
            }

            // update Y range, if there are any data
            if ( ( minY != double.MaxValue ) || ( maxY != double.MinValue ) )
            {
                rangeY = new DoubleRange( minY, maxY );
            }
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref="KCurvature"/> class.
 /// </summary>
 /// <param name="k">The number K of previous and posterior
 ///   points to consider when find local extremum points.</param>
 /// <param name="theta">The theta angle range (in
 ///   degrees) used to define extremum points..</param>
 public KCurvature(int k, DoubleRange theta)
 {
     this.K = k;
     this.Theta = theta;
     this.Suppression = k;
 }
Beispiel #25
0
 /// <summary>
 /// Check if the specified range is inside of the range.
 /// </summary>
 /// 
 /// <param name="range">Range to check.</param>
 /// 
 /// <returns><b>True</b> if the specified range is inside of the range or
 /// <b>false</b> otherwise.</returns>
 /// 
 public bool IsInside( DoubleRange range )
 {
     return ( ( IsInside( range.min ) ) && ( IsInside( range.max ) ) );
 }
Beispiel #26
0
 public static double ConvertRange(int value, DoubleRange from, DoubleRange to)
 {
     return ((value - from.Min) * (to.Length) / (from.Length)) + to.Min;
 }
Beispiel #27
0
 /// <summary>
 /// Check if the specified range overlaps with the range.
 /// </summary>
 /// 
 /// <param name="range">Range to check for overlapping.</param>
 /// 
 /// <returns><b>True</b> if the specified range overlaps with the range or
 /// <b>false</b> otherwise.</returns>
 /// 
 public bool IsOverlapping( DoubleRange range )
 {
     return ( ( IsInside( range.min ) ) || ( IsInside( range.max ) ) ||
              ( range.IsInside( min ) ) || ( range.IsInside( max ) ) );
 }
Beispiel #28
0
        /// <summary>
        ///   Estimate appropriate values for sigma given a data set.
        /// </summary>
        /// 
        /// <remarks>
        ///   This method uses a simple heuristic to obtain appropriate values
        ///   for sigma in a radial basis function kernel. The heuristic is shown
        ///   by Caputo, Sim, Furesjo and Smola, "Appearance-based object
        ///   recognition using SVMs: which kernel should I use?", 2002.
        /// </remarks>
        /// 
        /// <param name="inputs">The data set.</param>
        /// <param name="samples">The number of random samples to analyze.</param>
        /// <param name="range">The range of suitable values for sigma.</param>
        /// 
        /// <returns>A Laplacian kernel initialized with an appropriate sigma value.</returns>
        /// 
        public static Laplacian Estimate(double[][] inputs, int samples, out DoubleRange range)
        {
            if (samples > inputs.Length)
                throw new ArgumentOutOfRangeException("samples");

            double[] distances = Gaussian.Distances(inputs, samples);

            double q1 = distances[(int)Math.Ceiling(0.15 * distances.Length)];
            double q9 = distances[(int)Math.Ceiling(0.85 * distances.Length)];
            double qm = Accord.Statistics.Tools.Median(distances, alreadySorted: true);

            range = new DoubleRange(q1, q9);

            return new Laplacian(sigma: qm);
        }
 /// <summary>
 ///   Constructs a new <see cref="RombergMethod">Romberg's integration method</see>.
 /// </summary>
 /// 
 /// <param name="steps">The number of steps used in Romberg's method. Default is 6.</param>
 /// 
 public RombergMethod(int steps)
 {
     this.s = new double[steps];
     Range = new DoubleRange(0, 1);
 }
Beispiel #30
0
        /// <summary>
        ///   Update Y range.
        /// </summary>
        /// 
        private void UpdateYRange()
        {
            float minY = float.MaxValue;
            float maxY = float.MinValue;

            // walk through all data series
            foreach (Waveform wave in waveTable.Values)
            {
                // get data of the series
                float[] data = wave.data;

                if ((wave.updateYRange) && (data != null))
                {
                    // Let the compiler perform optimizations.
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i] > maxY)
                            maxY = data[i];
                        if (data[i] < minY)
                            minY = data[i];
                    }
                }
            }

            // update Y range, if there are any data
            if ((minY != float.MaxValue) || (maxY != float.MinValue))
            {
                rangeY = new DoubleRange(minY, maxY);
            }
        }
        /// <summary>
        ///   Constructs a new <see cref="RombergMethod">Romberg's integration method</see>.
        /// </summary>
        /// 
        /// <param name="steps">The number of steps used in Romberg's method. Default is 6.</param>
        /// <param name="function">The unidimensional function whose integral should be computed.</param>
        ///
        public RombergMethod(int steps, Func<double, double> function)
        {
            if (function == null)
                throw new ArgumentNullException("function");

            Range = new DoubleRange(0, 1);
            Function = function;
            this.s = new double[steps];
        }
Beispiel #32
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="LinearFunction"/> class.
 /// </summary>
 /// 
 public LinearFunction(double alpha, DoubleRange range)
 {
     this.Alpha = alpha;
     this.Range = range;
 }
        /// <summary>
        ///   Constructs a new <see cref="RombergMethod">Romberg's integration method</see>.
        /// </summary>
        /// 
        /// <param name="steps">The number of steps used in Romberg's method. Default is 6.</param>
        /// <param name="function">The unidimensional function whose integral should be computed.</param>
        /// <param name="a">The beginning of the integration interval.</param>
        /// <param name="b">The ending of the integration interval.</param>
        ///
        public RombergMethod(int steps, Func<double, double> function, double a, double b)
        {
            if (Double.IsInfinity(a) || Double.IsNaN(a))
                throw new ArgumentOutOfRangeException("a");

            if (Double.IsInfinity(b) || Double.IsNaN(b))
                throw new ArgumentOutOfRangeException("b");

            Function = function;
            Range = new DoubleRange(a, b);
            this.s = new double[steps];
        }