1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 23 package org.hipparchus.optim.nonlinear.vector.leastsquares; 24 25 import java.awt.geom.Point2D; 26 27 import org.hipparchus.random.RandomDataGenerator; 28 29 /** 30 * Factory for generating a cloud of points that approximate a straight line. 31 */ 32 public class RandomStraightLinePointGenerator { 33 /** Slope. */ 34 private final double slope; 35 /** Intercept. */ 36 private final double intercept; 37 /** Lowest value of the x-coordinate. */ 38 private final double lo; 39 /** Highest value of the x-coordinate. */ 40 private final double hi; 41 /** Standard deviation on the y-coordinate of the point. */ 42 private final double sigma; 43 /** Source of random data */ 44 private final RandomDataGenerator randomDataGenerator; 45 46 /** 47 * The generator will create a cloud of points whose x-coordinates 48 * will be randomly sampled between {@code xLo} and {@code xHi}, and 49 * the corresponding y-coordinates will be computed as 50 * <pre><code> 51 * y = a x + b + N(0, error) 52 * </code></pre> 53 * where {@code N(mean, sigma)} is a Gaussian distribution with the 54 * given mean and standard deviation. 55 * 56 * @param a Slope. 57 * @param b Intercept. 58 * @param sigma Standard deviation on the y-coordinate of the point. 59 * @param lo Lowest value of the x-coordinate. 60 * @param hi Highest value of the x-coordinate. 61 * @param seed RNG seed. 62 */ 63 public RandomStraightLinePointGenerator(double a, 64 double b, 65 double sigma, 66 double lo, 67 double hi, 68 long seed) { 69 randomDataGenerator = new RandomDataGenerator(seed); 70 slope = a; 71 intercept = b; 72 this.lo = lo; 73 this.hi = hi; 74 this.sigma = sigma; 75 } 76 77 /** 78 * Point generator. 79 * 80 * @param n Number of points to create. 81 * @return the cloud of {@code n} points. 82 */ 83 public Point2D.Double[] generate(int n) { 84 final Point2D.Double[] cloud = new Point2D.Double[n]; 85 for (int i = 0; i < n; i++) { 86 cloud[i] = create(); 87 } 88 return cloud; 89 } 90 91 /** 92 * Create one point. 93 * 94 * @return a point. 95 */ 96 private Point2D.Double create() { 97 final double abscissa = randomDataGenerator.nextUniform(lo, hi); 98 final double yModel = slope * abscissa + intercept; 99 final double ordinate = yModel + randomDataGenerator.nextNormal(0, sigma); 100 101 return new Point2D.Double(abscissa, ordinate); 102 } 103 }