TEXT   22

randomly generate a collection of points

Guest on 24th June 2022 02:46:08 AM

  1. # randomly generate a collection of points which are near either
  2. #  line_pts()      -  a line with randomly chosen slope and intercept  
  3. #  bad_line_pts()  -  a line with random slope and intercept, with outliers  
  4. #  quadratic_pts() -  a quadratic with randomly chosen coefficients
  5. #  cubic_pts()     -  a cubic with randomly chosen coefficients
  6. #  circle_pts()    -  a circle with randomly chosen center and radius.
  7. # example:
  8. #    plot(line_pts(), style = point, axes = boxed);
  9. #
  10. #  the data points are chosen from an appropriate uniform distribution,
  11. #  and the "noise" added is from a normal(0,1/2)  distribution.
  12. #   The randomization seed depends on the system clock.
  13. ##########
  14. #  uses the globals  points_wanted and  myrand(a,b)
  15. ##########
  16. #
  17. Seed:=randomize():
  18. with(stats):
  19.  
  20. #
  21. myrand := (a,b) -> evalf(a+(b-a)*(rand()/10^12)):
  22. points_wanted := 21:
  23. #
  24. line_pts:= proc()
  25.  local fuzz, m, b, x,i;
  26.    m:= myrand(-5,5);
  27.    b:= myrand(-20,20);
  28.    fuzz := [ random[normald[0,0.5]](points_wanted)];
  29.    x    := [ random[uniform[-10,10]](points_wanted)];
  30.    RETURN( [ [x[i], m*x[i]+b+fuzz[i]] $i=1..points_wanted]);
  31. end:
  32.  
  33. bad_line_pts:= proc()
  34.  local fuzz, m, b, x,i;
  35.    m:= myrand(0.3,3);
  36.    if (myrand(-10,10) < 0) then m:= m*(-1) fi;
  37.    b:= myrand(-20,20);
  38.    fuzz := [ random[normald[0 ,0.5]](2*points_wanted),
  39.              random[uniform[  3, 10]](4),
  40.              random[uniform[-10, -3]](4)];
  41.    x    := [ random[uniform[-10,10]](2*points_wanted),
  42.              random[uniform[-10,-5]](4),
  43.              random[uniform[  5,10]](4)];
  44.    x[2*points_wanted+8]:=9;
  45.    fuzz[2*points_wanted+8]:=200;
  46.    RETURN( [ [x[i], m*x[i]+b+fuzz[i]] $i=1..2*points_wanted+8]);
  47. end:
  48.  
  49. quadratic_pts := proc()
  50.  local fuzz, a, b, c,s, x,i;
  51.    a:= myrand(-5,5);
  52.    b:= myrand(-5,5);
  53.    c:= myrand(-10,10);
  54.    if (myrand(-10,10) > 0) then s:=1 else s:=-1 fi;
  55.    fuzz := [ random[normald[0,0.5]](points_wanted)];
  56.    x    := [ random[uniform[-5,5]](points_wanted)];
  57.    RETURN( [ [x[i], s*(x[i] - a)*(x[i]-b)+c+fuzz[i]] $i=1..points_wanted]);
  58. end:
  59.  
  60. cubic_pts := proc()
  61.  local fuzz, a, b, c, d,s,x,i;
  62.    a:= myrand(-3,3);
  63.    b:= myrand(-3,3);
  64.    c:= myrand(-3,3);
  65.    d:= myrand(-10,10);
  66.    if (myrand(-10,10) > 0) then s:=1 else s:=-1 fi;
  67.    fuzz := [ random[normald[0,0.5]](points_wanted)];
  68.    x    := [ random[uniform[-3,3]](points_wanted)];
  69.    RETURN( [ [x[i], s*(a-x[i])*(b-x[i])*(c-x[i])+d+fuzz[i]] $i=1..points_wanted]);
  70. end:
  71.  
  72. circle_pts := proc()
  73.      local dat, i, rad, rads, angles, a, b;
  74.       rad :=  myrand(3,10);
  75.       a := myrand(-10,10);
  76.       b:=  myrand(-10,10);
  77.       rads := [ random[normald[rad,rad/50]](points_wanted)];
  78.       angles := [ random[uniform[0,2*Pi]](points_wanted) ];
  79.       RETURN ( [ [a+rads[i]*cos(angles[i]), b+rads[i]*sin(angles[i])]  $ i=1..points_wanted  ]):
  80.  end:
  81.  
  82. printf(`defined line_pts(), bad_line_pts(), quadratic_pts(), cubic_pts(), and circle_pts()`):

Raw Paste


Login or Register to edit or fork this paste. It's free.