Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1.  
  2. package myUtil;
  3.  
  4. /**
  5. * Class of Normal Distributions
  6. * @author Chung-Chih Li
  7. *
  8. */
  9. public class NormalDistribution {
  10. private double mu, roh;
  11. private double xs; // x less than xs is too small to be considered for sampling
  12. private double xe; // x greater than xe is too big to be considered for sampling
  13. private double dx;
  14. private double[] dist = new double[50];
  15. private double iI; // the initial segment of the internal
  16.  
  17. /**
  18. * Normal Distribution
  19. * @param mu is the mean
  20. * @param roh is the variance
  21. */
  22. public NormalDistribution(double mu, double roh) {
  23. this.mu = mu;
  24. this.roh = roh; // roh will be the reference to determine the precision.
  25. // roh/100,000 will be considered too small for sampling.
  26. dx = roh/10000.0;
  27.  
  28. double x,y = 1/100000.0;
  29.  
  30. xs = mu-5*Math.abs(mu); //xs = mu - rpdf(y); // rpdf is the reverse pdf;
  31. xe = mu+5*Math.abs(mu); //xe = mu + rpdf(y);
  32.  
  33. double I=dx*y;
  34. x = xs;
  35. iI = 0;
  36. while (I-iI > 1.0E-25) {
  37. iI = I;
  38. x = x-dx;
  39. I += dx*pdf(x);
  40. }
  41. x=xs;
  42. int i = 1;
  43. dist[0] = 0;
  44. while (x<xe) {
  45. if (I > (double)i/(double)dist.length) {
  46. dist[i] = x;
  47. i++;
  48. if (i == dist.length) break;
  49.  
  50. }
  51. x += dx;
  52. I += dx*pdf(x);
  53. }
  54. }
  55.  
  56. /**
  57. * This is the density function
  58. * @param x
  59. * @return density at x
  60. */
  61. public double pdf(double x) {
  62. return 1/roh
  63. /Math.pow(2*Math.PI, 0.5)
  64. /Math.exp((Math.pow((x-mu)/roh,2))/2);
  65. }
  66.  
  67.  
  68. /**
  69. * The reverse function of the density function (pdf)
  70. * @param y
  71. * @return x
  72. */
  73. public double rpdf(double y) {
  74. return mu + roh*Math.pow(-2*Math.log(y*roh*Math.pow(2*Math.PI,0.5)),0.5);
  75. }
  76.  
  77.  
  78. /**
  79. * A numerical approximation is used
  80. * @return A random sample in this distribution;
  81. */
  82.  
  83. public double sample() {
  84. double I, x, p=Math.random();
  85. // found x that the integral from - infinity to x is bigger than p
  86.  
  87. int i = (int)(p*50);
  88. if (i==0) {
  89. x = xs;
  90. I =iI;
  91. }
  92. else {
  93. x = dist[i];
  94. I = (double)i/50.0;
  95. }
  96. while (I < p && x < xe) {
  97. x += dx;
  98. I += dx*pdf(x);
  99. }
  100. return x;
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. package myUtil;
  111.  
  112. /**
  113. * Class of Exponential distributions
  114. * @author Chung-Chih Li
  115. *
  116. */
  117. public class ExpDistribution {
  118. private double theta;
  119.  
  120. /**
  121. * @param theta is the mean of this distribution
  122. */
  123. public ExpDistribution(double theta) {
  124. this.theta = theta;
  125. }
  126.  
  127. /**
  128. * This is the density function
  129. * @param x
  130. * @return density at y
  131. */
  132. public double pdf(double x) {
  133. return (1/Math.exp(x/theta))/theta;
  134. }
  135.  
  136. /**
  137. * @param x
  138. * @return the probability that there is no event before time x
  139. */
  140. public double no(double x) {
  141. return 1/Math.exp(x/theta);
  142. }
  143.  
  144. /**
  145. * Time to next random event
  146. * @return
  147. */
  148. public double next() {
  149. double p = Math.random();
  150. return Math.log(1-p)*(-theta);
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement