1

I'm really new to java, I've created a code for simulating 100000 observations from Gaussian distribution with mean =0 and standard deviation=1 via inversion method. The code is slow compared to simulating with the rnorm command in R, or numpy.random in Python, ok I know that I've used a lot of for loops in my code, but aren't for loops in the core of the R and Python commands for random simulating? Anyway here is my code, and if you have any suggestions on how I could improve it please share (the class name is 'HelloWorld' since from printing the 'Hello World' message I started with java yesterday).

My code:

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class HelloWorld {

public static void main(String[] args) 
{
    double randomNum=0.0;
    for(int i=0;i<99999;i++) {
      //technically generating from uniform (0,1)
      randomNum=ThreadLocalRandom.current().nextDouble(0, 1); 
      System.out.println(nr(0,randomNum));}
}



public static double gaussian01(double x){ 
    //defining the gaussian (0,1)
     double par=(Math.sqrt(2*Math.PI));
     double ar=(Math.exp(-(x*x)/2));
     return ar/par;}

public static double integx2(double upper){  
// defining the integration for the gaussian function
      double lower=-9;
      double step= 0.001;
      int ran=(int)((upper-lower)/step);
      double[] xs=new double[ran+1];
      xs[0]=lower;
      for(int i=1; i<=ran;i++) xs[i]=xs[i-1]+step;
      double[] gin= new double[ran+1];
      for(int i=0; i<=ran;i++) gin[i]=step*(gaussian01(xs[i]));
      double sum=0;
      for(int i=0; i<=ran;i++) sum=sum+gin[i];
      return sum;}   


public static double fun(double upper1,double u){ 
//defining the equation which's solution follows the gaussian (0,1) if 'u' is taken from Uniform(0,1) 
      return integx2(upper1)-u;}


public static double grad(double x0, double uu){ 
  // computing the first derivative for the newton-raphson
   return (fun(x0+0.01,uu)-fun(x0,uu))/0.01;}

public static double nr(double str, double uuu1){ 
    // solving the equation above with the newton raphson method
     double x1=str-fun(str,uuu1)/grad(str,uuu1);
     double dif=x1-str;
     while(Math.abs(dif)>=0.001){
            str=x1;
            x1=str-fun(str,uuu1)/grad(str,uuu1);
            dif=x1-str;}
     return x1;}


}
Alex
  • 99
  • 1
  • 7
  • yes I'm aware of the marsaglia polar method, however I wanted to try out the inversion method and yes I'm pretty sure that there should exist standard classes for this purpose, just wanted to try it for myself and was wondering whether the code is fine as regard the perfomance – Alex Jul 14 '17 at 11:54
  • 1
    If you are asking about the performance, why don't you measure it? Hint: it's *horrible*. But that's not related to your Java skills, it's a consequence of the algorithm you've chosen. – Stefan Zobel Jul 14 '17 at 12:18
  • ok, so there is no way the inversion method could work as I guess, unless there is something really wasteful in my code. But as you mention is the algorithm as it is which causes the slow performance, thanks for your help. – Alex Jul 14 '17 at 12:33
  • 1
    Seems like this would be a better fit on [CodeReview](https://codereview.stackexchange.com). – pjs Jul 14 '17 at 16:10

0 Answers0