Example usage for org.apache.commons.math3.analysis.function Gaussian Gaussian

List of usage examples for org.apache.commons.math3.analysis.function Gaussian Gaussian

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.function Gaussian Gaussian.

Prototype

public Gaussian(double mean, double sigma) throws NotStrictlyPositiveException 

Source Link

Document

Normalized gaussian with given mean and standard deviation.

Usage

From source file:edu.snu.leader.hidden.personality.DirectionUpdateRulePersonalityCalculator.java

/**
 * Initializes the updater// ww  w.j a v a  2  s  .c  o  m
 *
 * @param simState The simulation's state
 * @see edu.snu.leader.hidden.personality.PersonalityCalculator#initialize(edu.snu.leader.hidden.SimulationState)
 */
@Override
public void initialize(SimulationState simState) {
    _LOG.trace("Entering initialize( simState )");

    // Call the superclass implementation
    super.initialize(simState);

    // Get the properties
    _simState = simState;
    Properties props = simState.getProps();

    // Import the preferred directions
    String directionsFileStr = props.getProperty(_DIRECTIONS_FILE_KEY);
    Validate.notEmpty(directionsFileStr,
            "Directions file (key=[" + _DIRECTIONS_FILE_KEY + "]) may not be empty");
    _LOG.info("Using directionsFileStr=[" + directionsFileStr + "]");
    loadDirectionChanges(directionsFileStr);

    // Get the first direction
    DirectionChange first = _directionChanges.peek();
    if ((first != null) && (first.simIndex <= 0)) {
        // Save the data
        _direction = first.direction;
        _sigma = first.sigma;
        _gaussian = new Gaussian(0.0f, _sigma);
        _maxGaussianValue = 1.0f / (_sigma * (float) Math.sqrt(2.0f * Math.PI));
        _LOG.debug("Initial direction [" + _direction + "]");

        // Pull it off the queue
        _directionChanges.poll();
    }

    // Get the modify winner discount flag
    String modifyWinnerDiscountStr = props.getProperty(_MODIFY_WINNER_DISCOUNT_KEY);
    Validate.notEmpty(modifyWinnerDiscountStr,
            "Modify winner discount flag (key=[" + _MODIFY_WINNER_DISCOUNT_KEY + "]) may not be empty");
    _modifyWinnerDiscount = Boolean.parseBoolean(modifyWinnerDiscountStr);
    _LOG.info("Using _modifyWinnerDiscount=[" + _modifyWinnerDiscount + "]");

    // Get the modify winner reward flag
    String modifyWinnerRewardStr = props.getProperty(_MODIFY_WINNER_REWARD_KEY);
    Validate.notEmpty(modifyWinnerRewardStr,
            "Modify winner discount flag (key=[" + _MODIFY_WINNER_REWARD_KEY + "]) may not be empty");
    _modifyWinnerReward = Boolean.parseBoolean(modifyWinnerRewardStr);
    _LOG.info("Using _modifyWinnerReward=[" + _modifyWinnerReward + "]");

    _LOG.trace("Leaving initialize( simState )");
}

From source file:edu.snu.leader.hidden.personality.DirectionUpdateRulePersonalityCalculator.java

/**
 * Calculate the new personality value/*from w  w  w. j  a va  2 s. co m*/
 *
 * @param currentPersonality The individual's current personality
 * @param updateType The type of update being applied
 * @param followers The number of followers in the initiation
 * @return The updated personality value
 * @see edu.snu.leader.hidden.personality.StandardUpdateRulePersonalityCalculator#calculatePersonality(edu.snu.leader.hidden.SpatialIndividual, edu.snu.leader.hidden.personality.PersonalityUpdateType, int)
 */
@Override
public float calculatePersonality(SpatialIndividual individual, PersonalityUpdateType updateType,
        int followers) {
    // Do we change the direction?
    DirectionChange next = _directionChanges.peek();
    if ((next != null) && (next.simIndex <= _simState.getSimIndex())) {
        // Save the data
        _direction = next.direction;
        _sigma = next.sigma;
        _gaussian = new Gaussian(0.0f, _sigma);
        _maxGaussianValue = 1.0f / (_sigma * (float) Math.sqrt(2.0f * Math.PI));
        _LOG.warn("New direction=[" + _direction + "] for next.simIndex=[" + next.simIndex
                + "] simStateSimIdx=[" + _simState.getSimIndex() + "] sigma=[" + _sigma + "] maxGaussianValue=["
                + _maxGaussianValue + "]");

        // Pull it off the queue
        _directionChanges.poll();
    }

    // Go ahead and use the superclass implementation
    return super.calculatePersonality(individual, updateType, followers);
}

From source file:streaming.core.WindowOperation.java

@Override
public Object[][] process(Object[] event) {
    long day = (Long) event[0];
    String word = (String) event[1];
    long freqs = (Long) event[2];

    TreeMap<Long, Long> sortedFreq = map.get(word);
    if (sortedFreq == null) {
        sortedFreq = new TreeMap<Long, Long>();
        map.put(word, sortedFreq);/*from www.ja  va2 s.  com*/
    }

    Long t = sortedFreq.get(day);
    if (t != null) {
        freqs = freqs + t;
    }
    sortedFreq.put(day, freqs);

    Iterator<Entry<Long, Long>> iterator = sortedFreq.headMap(1 + day - numberOfDays).entrySet().iterator();
    while (iterator.hasNext()) {
        iterator.next();
        iterator.remove();
    }

    DescriptiveStatistics stats = new DescriptiveStatistics();
    long dayIndex = 1 + day - numberOfDays;
    for (Entry<Long, Long> e : sortedFreq.entrySet()) {
        while (e.getKey() > dayIndex) {
            dayIndex++;
            stats.addValue(0);
        }
        stats.addValue(e.getValue());
    }

    if (sortedFreq.size() > numberOfDays) {
        System.out.println(day + " size=" + sortedFreq.size() + " " + sortedFreq);
    }

    double mean = stats.getMean();
    double meadian = stats.getPercentile(50);
    mean = (mean == 0) ? 1 : mean;
    meadian = (meadian == 0) ? 1 : meadian;
    double stddev = stats.getStandardDeviation();
    stddev = (stddev == 0) ? 1 : stddev;
    double cov = stddev / mean;

    //double swna = Math.log(freqs)*freqs/stats.getMean();
    double swna1 = Math.log(meadian) * Math.abs(freqs - meadian) / stddev;
    if (Double.isNaN(swna1)) {
        System.out.println();
    }
    double swna2 = Math.abs(freqs - meadian) / stddev;
    double swna3 = freqs / (meadian * cov);

    Gaussian gaussian = new Gaussian(100, 50);

    double swna4 = (0.1 + 100 * gaussian.value(meadian)) * freqs / (meadian * cov);

    int percentageAvialableValues = Math.round(100 * sortedFreq.size() / numberOfDays);
    //System.out.println("#"+ word + " " + freqs + " "+ stats.getMean() + Arrays.toString(stats.getValues()));
    return new Object[][] { { day, word, swna1, freqs, stats.getMean(), meadian, stddev, swna2, swna3, swna4,
            cov, percentageAvialableValues } };

    //      if(freqs > 3 && swna> 5){
    //         return new Object[][]{{day, word, swna}};   
    //      }else{
    //         return null;
    //      }

}

From source file:tools.descartes.dlim.extractor.ModelExtractor.java

private static double[] createGaussianFilter(int width) {
    int filterWidth = width;
    if (filterWidth % 2 == 0) {
        filterWidth++;/*from  w w w.j  a  v a 2 s  .  c  o m*/
    }
    filterWidth = Math.max(1, filterWidth);
    double[] filter = new double[filterWidth];
    double sigma = Math.sqrt((filterWidth * filterWidth - 1.0) / 12.0);
    int mean = filterWidth / 2;
    double filterSum = 0.0;
    Gaussian gaussian = new Gaussian(mean, sigma);
    for (int i = 0; i < filterWidth; i++) {
        filter[i] = gaussian.value(i);
        filterSum += filter[i];
    }

    // normalize to 1
    for (int i = 0; i < filterWidth; i++) {
        filter[i] = filter[i] / filterSum;
    }

    return filter;
}

From source file:utils.TestGaussian.java

public static void main(String[] args) {
    Gaussian gaussian = new Gaussian(100, 50);
    for (int i = 0; i < 1000; i++) {
        int x = 10 * i;
        System.out.println(x + " = " + String.format("%.2f", 0.1 + 100 * gaussian.value(x)));
    }/*from w w  w . ja  v a 2 s.  c  o  m*/

}