Latin Super Cubic sampling Latin hypercube Sampling,java code

Source: Internet
Author: User
Tags shuffle

When a random sample is generated, there is a problem of excessive data aggregation, which is solved by the Latin hypercube sampling.
The following diagram illustrates the difference between the two:

It can be seen that most of the data in the simple random sampling are in the middle, while the Latin cubic sampling is evenly generated in each small interval.

Latin cubic sampling must first explicitly generate the number of samples N, Latin cubic sampling of the steps:
1. [0, 1] divided into n equal parts, each small interval [i/n, (i+1)/n] in accordance with uniform distribution randomly generated a number.
2. The order of n random numbers is disturbed.
3. Each of these n numbers is the probability of each random sample, and the value of the random distribution is generated according to the inverse function of the probability distribution function.

The greater the value of N, the better the resulting data. Matlab has a special lhsdesign function to generate a Latin cubic sampling probability matrix. You have written a Java code as follows, and the example generates 1000 two-stage Poisson distribution samples.

Import Java.util.Arrays;
Import umontreal.ssj.probdist.Distribution;
Import umontreal.ssj.probdist.PoissonDist;
Import Umontreal.ssj.randvar.UniformGen;
Import Umontreal.ssj.randvar.UniformIntGen;
Import Umontreal.ssj.rng.MRG32k3aL;

Import Umontreal.ssj.rng.RandomStream;  /** * @author Chen Zhen * @version Date Created: April 11, 2018 PM 4:18:55 * @value class Description: Latin hypercube sampling/public class LHS



     {static Randomstream stream = new Mrg32k3al (); Generates a sample static double[][] Generatelhsamples for the specified distribution (distribution[] distributions, int samplenum) {int Periodnum       
        = Distributions.length; 

        Double[][] samples = new Double[samplenum][periodnum];  Generates a random probability in each [i/n, (i+1)/n], and then according to probability, the number of distributions specified for (int i = 0; i < periodnum; i++) for (int j = 0; j < Samplenum;
                J + +) {Double randomnum = uniformgen.nextdouble (stream, 0, 1.0/samplenum);
                Double lowbound = (double) j/(double) samplenum; SampLes[j][i] = Lowbound + randomnum;
            Samples[j][i] = Distributions[i].inversef (Samples[j][i]); Shuffle (samples);
    Upset array return samples; }//Disturb a two-dimensional array static double[][] Shuffle (double[][] samples) {for (int i = 0; i < samples[0].length; i++ for (int j = 0; J < Samples.length; J + +) {int mark = Uniformintgen.nextint (stream, 0, samples
            . length-1);
            Double temp = samples[j][i];
            Samples[j][i] = Samples[mark][i];
        Samples[mark][i] = temp;
    return to samples;
        public static void Main (string[] args) {int samplenum = 1000;  

        Double[] Meandemand = {20, 5};
        Poissondist[] distributions = new Poissondist[meandemand.length];


        for (int i = 0; i < meandemand.length i++) distributions[i] = new Poissondist (meandemand[i));

      double[][] Samples = generatelhsamples (distributions, samplenum);  System.out.println (arrays.deeptostring (samples)); 
            for (int i = 0; i < meandemand.length; i++) {double sum = 0;
            for (int j = 0; J < Samplenum; J + +) {sum + = Samples[j][i];
        } System.out.println (Sum/samplenum); }   
    }
}

The

Generates a sample with a mean value of 20.003, 4.998, very close to the mean of the desired Poisson distribution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.