Example usage for org.apache.commons.math3.distribution UniformRealDistribution sample

List of usage examples for org.apache.commons.math3.distribution UniformRealDistribution sample

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution UniformRealDistribution sample.

Prototype

@Override
public double sample() 

Source Link

Usage

From source file:edu.uiowa.javatm.UtilsTestDrive.java

public static void main(String[] args) throws IOException {

    double tau = 0.01;
    UniformRealDistribution uniform = new UniformRealDistribution(-1 * tau, tau);
    System.out.println(uniform.sample());

    /*/*from w ww .  ja  va 2  s.c om*/
     * 
      int a = +0;
      int b = +0;
      System.out.println(a/b);
            
      String asFile = "/Users/zhiyzuo/Documents/java-topic-model/data/AS.csv";
      ArrayList<int[]> AS = Utils.readAS(asFile);
      System.out.println(Arrays.deepToString(AS.toArray()));
            
    // random number genrator
    final Random random = new Random();
       System.out.println(random.ints(1, 0, 10).toArray()[0]);
            
      String asFile = "/Users/zhiyzuo/Documents/java-topic-model/data/AS.csv";
      ArrayList<int[]> AS = Utils.readAS(asFile);
      System.out.println(Arrays.deepToString(AS.toArray()));
    /*
     * 
    double[][] mydoubleArray = new double [][] { { 1, 2.},
                               { 0.02, 0.12},
                               { 4.97, 3.56} };
    Utils.write2DArray(mydoubleArray, "/Users/zhiyzuo/Desktop/test.csv");
            
    String[][] myStringArray = new String [][] { { "X0", "Y0"},
                               { "X1", "Y1"},
                               { "X2", "Y2"},
                               { "X3", "Y3"},
                               { "X4", "Y4"} };
    Utils.write2DArray(myStringArray, "/Users/zhiyzuo/Desktop/test.csv");
            
    int[][] array1 = {{1,2,3},{4,5,6},{7,8,9}};
    System.out.println("Test getColumn()");
    System.out.println(Arrays.toString(Utils.getColumn(array1, 0)));
            
    int[] array2 = {1,1,2,3,2,3,2};
    System.out.println("Test find()");
    System.out.println(Utils.find(array2, 2).toString());
            
    double[] array3 = {0.25, 0.3, 0.15, 0.2, 0.1};
    System.out.println("Test sortIndex(double[])");
    int[] sortedIndices = Utils.sortIndex(array3);
    double[] array3_sorted = new double[array3.length];
    for (int i = 0; i < array3.length; i++) {
    array3_sorted[i] = array3[sortedIndices[i]];
    }
    System.out.println(Arrays.toString(array3_sorted));
    */

    /*
    System.out.println("Test _sum()");
    double sum_ = Utils._sum(array3);
    System.out.println("Sum of array3 is: " + sum_);
            
    double[] array4 = {0.5, 0.7, 0.8};
    System.out.println("Test _normalize()");
    double[] array4_normed = Utils._normalize(array4);
    System.out.println("Normalized array4 is: " + Arrays.toString(array4_normed));
    */

    /*
    double[] array4 = {0.4, 0.05, 0.1};
    System.out.println("Test sample()");
    System.out.println("Probablity List: " + Arrays.toString(array4));
    int[] choices = new int[10];
    for (int i = 0; i < 10; i++) {
    choices[i] = Utils.sample(array4);
    }
    System.out.println(Arrays.toString(choices));
    /*
            
            
    .*
    String vocFileName = "../data/test_mat.csv.clabel";
    System.out.println("Test readVocabulary()");
    String[] vocArray = Utils.readVocabulary(vocFileName);
    System.out.println(Arrays.toString(vocArray));
    */

    /*
     String docFileName = "../data/WD.csv";
     System.out.println("Test readDocFile()");
     int[][] WD = Utils.readDocFile(docFileName);
     int[] DS = Utils.getColumn(WD, 0);
     int[] WS = Utils.getColumn(WD, 1);
     System.out.println(Arrays.deepToString(WD));
     System.out.println(Arrays.toString(DS));
     System.out.println(Arrays.toString(WS));
     System.out.println(DS[DS.length-1] + 1);
     */

    /*
    int[][] DA = Utils.readDA("data/DA.csv");
    System.out.println(Arrays.deepToString(DA));
            
    for (int j = 0; j < DA.length; j++) {
      System.out.println(j + " " + Arrays.toString(Utils.nonZeroIndex(DA[j])));
    }
    */

    /*
     double[][] citationMatrix = Utils.readCitationFile("../data/doc1Citation.csv");
     System.out.println(Arrays.deepToString(citationMatrix));
             
     double[] doubleArray = new double[] {1, 2, 3, 4};
     System.out.println("Mean of " + Arrays.toString(doubleArray) + " is: " + Utils.mean(doubleArray));
     System.out.println("Variance of " + Arrays.toString(doubleArray) + " is: " + Utils.variance(doubleArray));
     */

}

From source file:gamlss.distributions.GA.java

/** Generates a random value from this distribution.
 * @param mu -  value of mu distribution parameters
 * @param sigma -  value of sigma distribution parameters
 * @param uDist -  object of UniformRealDistribution class;
 * @return random value//from  w  w  w.j a va 2 s. co m
 */
private double rGA(final double mu, final double sigma, final UniformRealDistribution uDist) {
    //r <- qST3(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    //p <- runif(n)
    return qGA(uDist.sample(), mu, sigma, true, false);
}

From source file:gamlss.distributions.SST.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param tau -  vector of tau distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector//ww w.j  a  va  2s.  c  om
 */
public final double rSST(final double mu, final double sigma, final double nu, final double tau,
        final UniformRealDistribution uDist) {

    //if (any(tau <= 2)) stop(paste("tau must be greater than 2")
    if (tau <= 2) {
        System.err.println("tau must be greater than 2");
        return -1.0;
    }
    //r <- qST3(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qSST(uDist.sample(), mu, sigma, nu, tau, true, false);
}

From source file:gamlss.distributions.JSUo.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param tau -  vector of tau distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector//from ww w  .j av  a  2s .  c  om
 */
public final double rJSUo(final double mu, final double sigma, final double nu, final double tau,
        final UniformRealDistribution uDist) {

    // {  if (any(sigma <= 0))stop(paste("sigma must be positive",))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }

    //n <- ceiling(n)
    //temp = FastMath.ceil(n.getEntry(i));       
    //r <- qST3(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qJSUo(uDist.sample(), mu, sigma, nu, tau, true, false);
}

From source file:gamlss.distributions.TF.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector/*from ww  w .j a v  a2s  . c o m*/
 */
public final double rTF(final double mu, final double sigma, final double nu,
        final UniformRealDistribution uDist) {
    // {  if (any(sigma <= 0))stop(paste("sigma must be positive",))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }

    //if (any(nu <= 0))  stop(paste("nu must be positive", "\n", ""))
    if (nu <= 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }

    //r <- qTF(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qTF(uDist.sample(), mu, sigma, nu, true, false);
}

From source file:gamlss.distributions.PE.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector/*  ww  w  .  j av a  2s .  co m*/
 */
public final double rPE(final double mu, final double sigma, final double nu,
        final UniformRealDistribution uDist) {

    // {if (any(sigma <= 0))stop(paste("sigma must be positive"))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }
    //if (any(nu <= 0))  stop(paste("nu must be positive", "\n", ""))
    if (nu <= 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }

    //r <- qPE(p,mu=mu,sigma=sigma,nu=nu)
    return qPE(uDist.sample(), mu, sigma, nu, true, false);
}

From source file:gamlss.distributions.TF2.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector/*from  w ww. j a va 2  s  .c o m*/
 */
public final double rTF2(final double mu, final double sigma, final double nu,
        final UniformRealDistribution uDist) {

    // {  if (any(sigma <= 0))stop(paste("sigma must be positive",))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }

    //if (any(nu <= 0))  stop(paste("nu must be positive", "\n", ""))
    if (nu <= 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }
    //r <- qST3(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qTF2(uDist.sample(), mu, sigma, nu, true, false);
}

From source file:gamlss.distributions.ST1.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param tau -  vector of tau distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector/*from ww  w.  j av  a2 s  .co  m*/
 */
public final double rST1(final double mu, final double sigma, final double nu, final double tau,
        final UniformRealDistribution uDist) {

    // {if (any(sigma <= 0))stop(paste("sigma must be positive"))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }
    //r <- qST1(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qST1(uDist.sample(), mu, sigma, nu, tau, true, false);
}

From source file:com.vsthost.rnd.jdeoptim.DE.java

/**
 * Computes and returns a new population.
 *
 * @return A new population./*w  w w  .  java2 s  . c o  m*/
 */
private void generatePopulation() {
    // Declare and initialize the new population:
    double[][] newPopulation = new double[this.populationSize][];

    // Declare and initialize the new population scores:
    double[] newScores = new double[this.populationSize];

    // Define a uniform distribution:
    UniformIntegerDistribution distributionForCol = new UniformIntegerDistribution(this.randomGenerator, 0,
            this.dimension - 1);
    UniformIntegerDistribution distributionForRow = new UniformIntegerDistribution(this.randomGenerator, 0,
            this.populationSize - 1);
    UniformRealDistribution distributionForCO = new UniformRealDistribution(this.randomGenerator, 0, 1);

    // Iterate over the current population:
    for (int c = 0; c < this.populationSize; c++) {
        // Get the candidate as the base of the next candidate (a.k.a. trial):
        double[] trial = Arrays.copyOf(this.population[c], this.dimension);

        // TODO: Implement all strategies.

        // Pick a random index from the dimension to start with:
        int index = distributionForCol.sample();

        // Get 2 elements from the population which are distinct:
        int r1 = -1, r2 = -1;
        while (c == r1 || c == r2 || r1 == r2) {
            r1 = distributionForRow.sample();
            r2 = distributionForRow.sample();
        }

        // Iterate over dimensions and do stuff:
        for (int _i = 0; _i < this.dimension; _i++) {
            // To crossover or not to crossover:
            if (distributionForCO.sample() < this.crossoverProbability) {
                // Using strategy "DE/best/1/bin with jitter" as in R's DEoptim package (strategy==3):
                trial[index] = this.bestCandidate[index] + this.weight * (distributionForCO.sample() + 0.0001)
                        * (this.population[r1][index] - this.population[r2][index]);

                // Using strategy "DE/local-to-best/1/bin" as in R's DEoptim package (strategy==2):
                // trial[index] += this.weight *  (this.population[r1][index] - this.population[r2][index]) + this.weight * (this.bestCandidate[index] - trial[index]);
            }

            // Update index:
            index = (index + 1) % dimension;
        }

        //            // Apply limits with reflection:
        //            for (int i = 0; i < this.lowerLimit.length; i++) {
        //                // Check lower limit:
        //                if (trial[i] < this.lowerLimit[i]) {
        //                    trial[i] = this.lowerLimit[i] + trial[i];
        //                }
        //                // Check upper limit:
        //                else if (trial[i] > this.upperLimit[i]) {
        //                    trial[i] = this.upperLimit[i] - trial[i];
        //                }
        //            }

        // Apply limits:
        for (int i = 0; i < this.lowerLimit.length; i++) {
            // Check lower limit:
            if (trial[i] < this.lowerLimit[i]) {
                trial[i] = this.lowerLimit[i];
            }
            // Check upper limit:
            else if (trial[i] > this.upperLimit[i]) {
                trial[i] = this.upperLimit[i];
            }
        }

        // Compute the score of the trial:
        final double score = this.function.apply(trial);

        // Check the score of this candidate:
        if (score < this.scores[c]) {
            newPopulation[c] = trial;
            newScores[c] = score;
        } else {
            newPopulation[c] = this.population[c];
            newScores[c] = this.scores[c];
        }
    }

    // Now, reset the population and scores:
    this.population = newPopulation;
    this.scores = newScores;
}

From source file:gamlss.distributions.ST3.java

/** Generates a random sample from this distribution.
 * @param mu -  vector of mu distribution parameters values
 * @param sigma -  vector of sigma distribution parameters values
 * @param nu -  vector of nu distribution parameters values
 * @param tau -  vector of tau distribution parameters values
 * @param uDist -  object of UniformRealDistribution class;
 * @return random sample vector//from w w w . j  a va 2 s .  c o m
 */
public final double rST3(final double mu, final double sigma, final double nu, final double tau,
        final UniformRealDistribution uDist) {
    // {if (any(sigma <= 0))stop(paste("sigma must be positive"))
    if (sigma <= 0) {
        System.err.println("sigma must be positive");
        return -1.0;
    }
    //if (any(nu <= 0))  stop(paste("nu must be positive", "\n", ""))
    if (nu <= 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }
    //if (any(tau <=0))  stop(paste("tau must be positive", "\n", ""))
    if (tau <= 0) {
        System.err.println("nu must be positive");
        return -1.0;
    }

    //n <- ceiling(n)
    //temp = FastMath.ceil(n.getEntry(i));             
    //r <- qST3(p,mu=mu,sigma=sigma,nu=nu,tau=tau)
    return qST3(uDist.sample(), mu, sigma, nu, tau, true, false);
}