repicea.stats.sampling.SamplingUtility Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of repicea-mathstats Show documentation
Show all versions of repicea-mathstats Show documentation
Mathematical and statistical methods
/*
* This file is part of the repicea-statistics library.
*
* Copyright (C) 2009-2019 Mathieu Fortin for Rouge-Epicea
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed with the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* Please see the license at http://www.gnu.org/copyleft/lesser.html.
*/
package repicea.stats.sampling;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import repicea.stats.StatisticalUtility;
public class SamplingUtility {
/**
* Returns a sample from a population.
*
* @param the type of object in the population and the sample
* @param population a List of instances T standing for the population
* @param sampleSize the sample size (int)
* @param withReplacement a boolean true if sampling with replacement or false otherwise
* @return a List of instances T standing for the sample
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getSample(List population, int sampleSize, boolean withReplacement) {
if (sampleSize <= 0 || sampleSize > population.size()) {
throw new InvalidParameterException("The sample size must be greater than 0 and smaller than or equal to the population size!");
}
List sampleIndex = getSampleIndex(sampleSize, population.size(), withReplacement);
List sample = new ArrayList();
for (Integer ind : sampleIndex) {
sample.add(population.get(ind));
}
return sample;
}
/**
* Returns a sample from a population drawn without replacement.
*
* @param the type of object in the population and the sample
* @param population a List of instances T standing for the population
* @param sampleSize the sample size (int)
* @return a List of instances T standing for the sample
*/
public static List getSample(List population, int sampleSize) {
return getSample(population, sampleSize, false);
}
/**
* This method returns the list of different observations and their frequency.
* @param list a population or a sample
* @return a Map with observation as keys and frequencies as values
*/
@SuppressWarnings("rawtypes")
public static Map