下面列出了java.lang.Math#exp ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void softmax(double[] x) {
double max = 0.0;
double sum = 0.0;
for(int i=0; i < x.length; i++)
{
if(max < x[i])
{
max = x[i];
}
}
for(int i=0; i < x.length; i++) {
x[i] = Math.exp(x[i] - max);
sum += x[i];
}
for(int i=0; i<x.length; i++)
{
x[i] /= sum;
}
}
/**
* Converts an array containing the natural logarithms of
* probabilities stored in a vector back into probabilities.
* The probabilities are assumed to sum to one.
*
* @param a an array holding the natural logarithms of the probabilities
* @return the converted array
*/
public static double[] logs2probs(double[] a) {
double max = a[maxIndex(a)];
double sum = 0.0;
double[] result = new double[a.length];
for(int i = 0; i < a.length; i++) {
result[i] = Math.exp(a[i] - max);
sum += result[i];
}
normalize(result, sum);
return result;
}
/**
* Converts an array containing the natural logarithms of
* probabilities stored in a vector back into probabilities.
* The probabilities are assumed to sum to one.
*
* @param a an array holding the natural logarithms of the probabilities
* @return the converted array
*/
public static double[] logs2probs(double[] a) {
double max = a[maxIndex(a)];
double sum = 0.0;
double[] result = new double[a.length];
for(int i = 0; i < a.length; i++) {
result[i] = Math.exp(a[i] - max);
sum += result[i];
}
normalize(result, sum);
return result;
}
/**
* Converts an array containing the natural logarithms of
* probabilities stored in a vector back into probabilities.
* The probabilities are assumed to sum to one.
*
* @param a an array holding the natural logarithms of the probabilities
* @return the converted array
*/
public static double[] logs2probs(double[] a) {
double max = a[maxIndex(a)];
double sum = 0.0;
double[] result = new double[a.length];
for(int i = 0; i < a.length; i++) {
result[i] = Math.exp(a[i] - max);
sum += result[i];
}
normalize(result, sum);
return result;
}
public Object exp(Object param)
throws ParseException
{
if (param instanceof Complex)
{
Complex z = (Complex) param;
double x = z.re();
double y = z.im();
double mod = Math.exp(x);
return new Complex(mod*Math.cos(y),mod*Math.sin(y));
}
else if (param instanceof Number)
{
return new Double(Math.exp(((Number)param).doubleValue()));
}
throw new ParseException("Invalid parameter type");
}
double getIterCount() {
// IES - remove interaction
if (speedBar.getValue() == 0)
return 0;
return .1*Math.exp((speedBar.getValue()-61)/24.);
}
/**
* <p>
* Main method for SA, that generates random neighbors and checks their inconsistency ratio
* </p>
*/
private void runSA(){
boolean currentSolution[] = randomSolution();
boolean candidate[];
double t_init = params.tInit;
double t_actual = t_init;
long cooldowns = params.maxLoops;
double lambda;
int current_cooldown = 1;
long neighbors = params.neighbors;
while(current_cooldown < cooldowns){
for(int i=0; i<neighbors; i++){
candidate = neighbor(currentSolution);
lambda = data.measureIEP(candidate) - data.measureIEP(currentSolution);
if(Randomize.Rand() < Math.exp(-lambda / t_actual) || lambda < 0){
//We keep the candidate
System.arraycopy(candidate, 0, features, 0, candidate.length);
System.arraycopy(candidate, 0, currentSolution, 0, candidate.length);
}
}
//Cooling down
t_actual = Cauchy(t_init, current_cooldown);
current_cooldown +=1;
}
/* checks if a subset satisfies the condition (more than 0 selected features) */
if(features==null){
System.err.println("ERROR: It couldn't be possible to find any solution.");
System.exit(0);
}
}
/**
* <p>
* Main method for SA, that generates random neighbors and checks their inconsistency ratio
* </p>
*/
private void runSA(){
boolean currentSolution[] = randomSolution();
boolean candidate[];
double t_init = params.tInit;
double t_actual = t_init;
long cooldowns = params.maxLoops;
double lambda;
int current_cooldown = 1;
long neighbors = params.neighbors;
while(current_cooldown < cooldowns){
for(int i=0; i<neighbors; i++){
candidate = neighbor(currentSolution);
lambda = data.medidaInconsistencia(candidate) - data.medidaInconsistencia(currentSolution);
if(Randomize.Rand() < Math.exp(-lambda / t_actual) || lambda < 0){
//We keep the candidate
System.arraycopy(candidate, 0, features, 0, candidate.length);
System.arraycopy(candidate, 0, currentSolution, 0, candidate.length);
}
}
//Cooling down
t_actual = Cauchy(t_init, current_cooldown);
current_cooldown +=1;
}
/* checks if a subset satisfies the condition (more than 0 selected features) */
if(features==null){
System.err.println("ERROR: It couldn't be possible to find any solution.");
System.exit(0);
}
}
public Object cosh(Object param)
throws ParseException
{
if (param instanceof Complex)
{
return ((Complex)param).cosh();
}
else if (param instanceof Number)
{
double value = ((Number)param).doubleValue();
return new Double((Math.exp(value) + Math.exp(-value))/2);
}
throw new ParseException("Invalid parameter type");
}
public Object tanh(Object param)
throws ParseException
{
if (param instanceof Complex)
{
return ((Complex)param).tanh();
}
else if (param instanceof Number)
{
double value = ((Number)param).doubleValue();
return new Double((Math.exp(value)-Math.exp(-value))/(Math.pow(Math.E,value)+Math.pow(Math.E,-value)));
}
throw new ParseException("Invalid parameter type");
}
public Object sinh(Object param)
throws ParseException
{
if (param instanceof Complex)
{
return ((Complex)param).sinh();
}
else if (param instanceof Number)
{
double value = ((Number)param).doubleValue();
return new Double((Math.exp(value)-Math.exp(-value))/2);
}
throw new ParseException("Invalid parameter type");
}
/**
* <p>
* Main method for SA, that generates random neighbors and checks their inconsistency ratio
* </p>
*/
private void runSA(){
double I[];
double IMV[][];
/* loads the arrays with the Mutual Information */
I = data.obtenerIMVarsClase();
IMV = data.obtenerIMVars();
boolean currentSolution[] = randomSolution();
boolean candidate[];
double t_init = params.tInit;
double t_actual = t_init;
long cooldowns = params.maxLoops;
double lambda;
int current_cooldown = 1;
long neighbors = params.neighbors;
while(current_cooldown < cooldowns){
for(int i=0; i<neighbors; i++){
candidate = neighbor(currentSolution);
lambda = medidaBattiti(modifiedFeat,candidate,I, IMV) - medidaBattiti(modifiedFeat,currentSolution,I, IMV);
if(Randomize.Rand() < Math.exp(-lambda / t_actual) || lambda < 0){
//We keep the candidate
System.arraycopy(candidate, 0, features, 0, candidate.length);
System.arraycopy(candidate, 0, currentSolution, 0, candidate.length);
}
}
//Cooling down
t_actual = Cauchy(t_init, current_cooldown);
current_cooldown +=1;
}
/* checks if a subset satisfies the condition (more than 0 selected features) */
if(features==null){
System.err.println("ERROR: It couldn't be possible to find any solution.");
System.exit(0);
}
}
private void Mutacion () {
int n_hijo, i, j, nq, n1, n2;
double z0, z1, x1, x2, si, co;
for (n_hijo=0; n_hijo<Landa; n_hijo++) {
/* Mutation of sigma */
if (n_sigma==1) /* if we use only a sigma, the sigma is adapted with Tau_1 */
Hijos[n_hijo].Gene[tabla.n_variables] *= ValorNormal (Tau_1);
else {
z0 = ValorNormal (Tau_0);
for (i=tabla.n_variables; i<tabla.n_variables + n_sigma; i++) {
z1 = ValorNormal (Tau);
Hijos[n_hijo].Gene[i] *= Math.exp (z1+z0);
/* The standard desviation is Epsilon_sigma if it becomes 0 */
if (Hijos[n_hijo].Gene[i]==0.0)
Hijos[n_hijo].Gene[i] = Epsilon_sigma;
}
}
/* Mutation of alfa */
for (i = tabla.n_variables + n_sigma; i<tabla.n_variables + n_sigma + n_alfa; i++) {
z0 = ValorNormal (Beta);
Hijos[n_hijo].Gene[i] += z0;
/* Si el valor mutado se sale del intervalo [-i,i], se proyecta
circularmente el valor a dicho intervalo */
if (Math.abs(Hijos[n_hijo].Gene[i])>i)
Hijos[n_hijo].Gene[i] -= 2 *i * signo (Hijos[n_hijo].Gene[i]);
}
/* Mutation of x */
/* we calculate the uncorrelated vector of mutations */
for (i=0; i<tabla.n_variables; i++)
if (tabla.n_variables + i < tabla.n_variables + n_sigma)
Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+i]);
else /* if there aren't more tipical desviations we use the latest */
Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+n_sigma-1]);
/* Correlation of the vector if we use the angles */
if (n_alfa!=0) {
nq = n_alfa;
for (j=nl_alfa; j<=nm_alfa; ++j) {
n1 = tabla.n_variables - j;
n2 = tabla.n_variables;
for (i=1; i<=j; ++i) {
x1 = Z[n1-1];
x2 = Z[n2-1];
si = Math.sin(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
co = Math.cos(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
Z[n2-1] = x1*si + x2*co;
Z[n1-1] = x1*co - x2*si;
--n2;
--nq;
}
}
}
/* Final mutation of X */
for (i=0; i<tabla.n_variables; i++) {
Hijos[n_hijo].Gene[i] += Z[i];
if (Hijos[n_hijo].Gene[i] < -(Math.PI/2.0))
Hijos[n_hijo].Gene[i] = -(Math.PI/2.0) + 1E-10;
if (Hijos[n_hijo].Gene[i] > (Math.PI/2.0))
Hijos[n_hijo].Gene[i] = (Math.PI/2.0) - 1E-10;
}
}
}
private void Mutacion () {
int n_hijo, i, j, nq, n1, n2;
double z0, z1, x1, x2, si, co;
for (n_hijo=0; n_hijo<Landa; n_hijo++) {
/* Mutation of sigma */
if (n_sigma==1) /* if we use only a sigma, the sigma is adapted with Tau_1 */
Hijos[n_hijo].Gene[tabla.n_variables] *= ValorNormal (Tau_1);
else {
z0 = ValorNormal (Tau_0);
for (i=tabla.n_variables; i < tabla.n_variables + n_sigma; i++) {
z1 = ValorNormal (Tau);
Hijos[n_hijo].Gene[i] *= Math.exp (z1+z0);
/* The standard desviation is Epsilon_sigma if it becomes 0 */
if (Hijos[n_hijo].Gene[i]==0.0)
Hijos[n_hijo].Gene[i] = Epsilon_sigma;
}
}
/* Mutation of alfa */
for (i = tabla.n_variables + n_sigma; i<tabla.n_variables + n_sigma + n_alfa; i++) {
z0 = ValorNormal (Beta);
Hijos[n_hijo].Gene[i] += z0;
/* Si el valor mutado se sale del intervalo [-i,i], se proyecta
circularmente el valor a dicho intervalo */
if (Math.abs(Hijos[n_hijo].Gene[i])>i) Hijos[n_hijo].Gene[i] -= 2.0 * PI * signo (Hijos[n_hijo].Gene[i]);
}
/* Mutation of x */
/* we calculate the uncorrelated vector of mutations */
for (i=0; i<tabla.n_variables; i++) {
if (tabla.n_variables + i < tabla.n_variables + n_sigma) Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+i]);
else Z[i] = ValorNormal (Hijos[n_hijo].Gene[tabla.n_variables+n_sigma-1]); /* if there aren't more tipical desviations we use the latest */
}
/* Correlation of the vector if we use the angles */
if (n_alfa!=0) {
nq = n_alfa;
for (j=nl_alfa; j<=nm_alfa; ++j) {
n1 = tabla.n_variables - j;
n2 = tabla.n_variables;
for (i=1; i<=j; ++i) {
x1 = Z[n1-1];
x2 = Z[n2-1];
si = Math.sin(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
co = Math.cos(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq - 1]);
Z[n2-1] = x1*si + x2*co;
Z[n1-1] = x1*co - x2*si;
--n2;
--nq;
}
}
}
/* Final mutation of X */
for (i=0; i<tabla.n_variables; i++) {
Hijos[n_hijo].Gene[i] += Z[i];
if (Hijos[n_hijo].Gene[i] < (-1.0 * (PI/2.0))) Hijos[n_hijo].Gene[i] = (-1.0 * (PI/2.0)) + 1E-10;
if (Hijos[n_hijo].Gene[i] > (PI/2.0)) Hijos[n_hijo].Gene[i] = (PI/2.0) - 1E-10;
}
}
}
private void Mutacion() {
int n_hijo, i, j, nq, n1, n2;
double z0, z1, x1, x2, si, co;
for (n_hijo = 0; n_hijo < Landa; n_hijo++) {
/* Mutation of sigma */
if (n_sigma == 1)
/* if we use only a sigma, the sigma is adapted with Tau_1 */
{
Hijos[n_hijo].Gene[tabla.n_variables] *= ValorNormal(Tau_1);
}
else {
z0 = ValorNormal(Tau_0);
for (i = tabla.n_variables; i < tabla.n_variables + n_sigma; i++) {
z1 = ValorNormal(Tau);
Hijos[n_hijo].Gene[i] *= Math.exp(z1 + z0);
/* The standard desviation is Epsilon_sigma if it becomes 0 */
if (Hijos[n_hijo].Gene[i] == 0.0) {
Hijos[n_hijo].Gene[i] = Epsilon_sigma;
}
}
}
/* Mutation of alfa */
for (i = tabla.n_variables + n_sigma;
i < tabla.n_variables + n_sigma + n_alfa; i++) {
z0 = ValorNormal(Beta);
Hijos[n_hijo].Gene[i] += z0;
/* Si el valor mutado se sale del intervalo [-i,i], se proyecta
circularmente el valor a dicho intervalo */
if (Math.abs(Hijos[n_hijo].Gene[i]) > i) {
Hijos[n_hijo].Gene[i] -= 2 * PI * signo(Hijos[n_hijo].Gene[i]);
}
}
/* Mutation of x */
/* we calculate the uncorrelated vector of mutations */
for (i = 0; i < tabla.n_variables; i++) {
if (tabla.n_variables + i < tabla.n_variables + n_sigma) {
Z[i] = ValorNormal(Hijos[n_hijo].Gene[tabla.n_variables + i]);
}
else
/* if there aren't more tipical desviations we use the latest */
{
Z[i] = ValorNormal(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma - 1]);
}
}
/* Correlation of the vector if we use the angles */
if (n_alfa != 0) {
nq = n_alfa;
for (j = nl_alfa; j <= nm_alfa; ++j) {
n1 = tabla.n_variables - j;
n2 = tabla.n_variables;
for (i = 1; i <= j; ++i) {
x1 = Z[n1 - 1];
x2 = Z[n2 - 1];
si = Math.sin(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq -
1]);
co = Math.cos(Hijos[n_hijo].Gene[tabla.n_variables + n_sigma + nq -
1]);
Z[n2 - 1] = x1 * si + x2 * co;
Z[n1 - 1] = x1 * co - x2 * si;
--n2;
--nq;
}
}
}
/* Final mutation of X */
for (i = 0; i < tabla.n_variables; i++) {
Hijos[n_hijo].Gene[i] += Z[i];
if (Hijos[n_hijo].Gene[i] < - (PI / 2.0)) {
Hijos[n_hijo].Gene[i] = - (PI / 2.0) + 1E-10;
}
if (Hijos[n_hijo].Gene[i] > (PI / 2.0)) {
Hijos[n_hijo].Gene[i] = (PI / 2.0) - 1E-10;
}
}
}
}
public boolean FitAndMarkDirty(double[] processed, double[] dirtyness, double[] quality) {
boolean allClean = true;
//double [] predUVar = new double[stream.length+1];
//double [] predLVar = new double[stream.length+1];
//double [] predValue = new double[stream.length+1];
// will them with NaNs until the windowSize is reached
for (int i = 0; i < windowSize; i++) {
//predUVar[i] = Double.NaN;
//predLVar[i] = Double.NaN;
//predValue[i] = Double.NaN;
processed[i] = stream[i];
dirtyness[i] = 0;
}
// Sliding Window
double[] tseries = new double[windowSize];
for (int i = 0; i <= (stream.length - windowSize - 1); i++) {
int currIdx = i + windowSize;
System.arraycopy(stream, i, tseries, 0, windowSize);
/*
for (double t : tseries) {
System.out.print(t + ",");
}
*/
//System.out.println();
//System.out.println(i + windowSize);
// create and execute AR model
ARModel ar = new ARModel(tseries);
ar.run();
// predict next value from AR model
double[] arPred = ar.getArPreds();
double predValue = arPred[0]; // estimated
// Get residuals from AR model and give them to GARCH model
double[] arResid = ar.getArResiduals();
GarchModel gm = new GarchModel(arResid);
gm.run();
// Predict +ve and -ve variance from GARCH model.
double predUVar = gm.getPredUVar(); // sigma
double predLVar = gm.getPredLVar();
//System.out.println(gm.getPredUVar());
//System.out.println(gm.getPredLVar());
double quality_metric = 0;
if (predUVar != 0.0)
quality_metric = 1 / Math.sqrt(2 * Math.PI * predUVar * predUVar) * Math.exp(-((stream[currIdx] - predValue) * (stream[currIdx] - predValue)) / (2 * predUVar * predUVar));
quality[currIdx] = quality_metric;
logger.warn("quality : " + currIdx + " : " + quality_metric + "U-var: " + predUVar + "L-var: " + predLVar);
if (predUVar > minVar) {
if ((stream[currIdx] <= predValue + errorBound * Math.sqrt(predUVar)) &&
(stream[currIdx] >= predValue + errorBound * Math.sqrt(predLVar))) {
processed[currIdx] = stream[currIdx];
dirtyness[currIdx] = 0;
} else {
processed[currIdx] = predValue;
dirtyness[currIdx] = 1;
allClean = false;
}
} else {
processed[currIdx] = stream[currIdx];
dirtyness[currIdx] = 0;
}
}
return allClean;
}
public static MR_float exp ( MR_float n ) { return new MR_float(Math.exp(n.get())); }
public static MR_double exp ( MR_double n ) { return new MR_double(Math.exp(n.get())); }