/*
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the GPL License.
* See the accompanying LICENSE file for terms.
*/
// ListUtils provides some basic math operation on numerical lists
package com.yahoo.egads.utilities;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ListUtils {
public static float sumQ(List Q) {
float res = 0;
for (float f : Q) {
res += f;
}
return res;
}
public static float sum2Q(List Q) {
float res = 0;
for (float f : Q) {
res += (f * f);
}
return res;
}
public static void repQ(LinkedList Q, float value, int repitition) {
Q.clear();
for (int i = 0; i < repitition; ++i) {
Q.addLast(value);
}
}
public static float kernelSum(float x, List mean, List sds) {
float res = 0;
float sd = 1;
Iterator it = sds.iterator();
for (float m : mean) {
if (it.hasNext()) {
sd = it.next();
}
res += Math.exp(-Math.pow(x - m, 2) / (2 * Math.pow(sd, 2))) / (sd * Math.sqrt(2 * Math.PI));
}
return res;
}
public static float kernelSubSum(float x, List mean, List sds, int from, int to) {
float res = 0;
float sd = 1;
Iterator it = sds.iterator();
int i = 0;
for (float m : mean) {
if (it.hasNext()) {
sd = it.next();
}
if (i >= from && i <= to) {
res += Math.exp(-Math.pow(x - m, 2) / (2 * Math.pow(sd, 2))) / (sd * Math.sqrt(2 * Math.PI));
}
i++;
if (i > to) {
break;
}
}
return res;
}
public static float sumLog(List Q) {
float res = 0;
for (float f : Q) {
res += Math.log(f);
}
return res;
}
public static LinkedList kernelQ(List Q, List mean, List sds) {
LinkedList res = new LinkedList();
float sd = 1;
float m = 0;
Iterator it1 = mean.iterator();
Iterator it2 = sds.iterator();
for (float x : Q) {
if (it1.hasNext()) {
m = it1.next();
}
if (it2.hasNext()) {
sd = it2.next();
}
res.addLast((float) (Math.exp(-Math.pow(x - m, 2) / (2 * Math.pow(sd, 2))) / (sd * Math.sqrt(2 * Math.PI))));
}
return res;
}
public static void addQ(LinkedList Q, List P) {
Iterator it = P.iterator();
float p = 0;
LinkedList temp = new LinkedList();
for (float q : Q) {
if (it.hasNext()) {
p = it.next();
}
temp.addLast(p + q);
}
Q.clear();
Q.addAll(temp);
}
public static void subtractQ(LinkedList Q, List P) {
Iterator it = P.iterator();
float p = 0;
LinkedList temp = new LinkedList();
for (float q : Q) {
if (it.hasNext()) {
p = it.next();
}
temp.addLast(q - p);
}
Q.clear();
Q.addAll(temp);
}
public static void multiplyQ(LinkedList Q, List P) {
Iterator it = P.iterator();
float p = 0;
LinkedList temp = new LinkedList();
for (float q : Q) {
if (it.hasNext()) {
p = it.next();
}
temp.addLast(q * p);
}
Q.clear();
Q.addAll(temp);
}
public static LinkedList maxQ(List Q, float m) {
LinkedList temp = new LinkedList();
for (float q : Q) {
temp.addLast(Math.max(q, m));
}
return temp;
}
public static float quantile(List Q, float probability) {
Collections.sort(Q);
int n = Q.size();
float index = (n - 1) * probability;
int lo = (int) Math.floor(index);
int hi = (int) Math.ceil(index);
float h = index - lo;
float qs = (1 - h) * Q.get(lo) + h * Q.get(hi);
return qs;
}
}
Ads help maintain this website.