/*
* NUKAF Framework for Computational Social Science
* Copyright (C) 2009 Santiago Nunez-Corrales <snunezcr@acm.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nu.entities;
import nu.transformations.NUAMDatumUtils;
import java.util.Vector;
import java.lang.Math;
public class NUAMScalarDatum implements NUAMDatum {
// Scalar version of datum
// We keep a reference of both normal and normalized/remapped data
// for consistency purposes
private double magnitude;
private double error;
private String annotation;
private Vector reference, normalized;
private boolean isNormalized;
private NUAMScalarDatum normDatum;
private NUAMDatumUtils utils;
//Constructor: no parameters
public NUAMScalarDatum(){
magnitude = 0;
error = 0;
annotation = " ";
reference = null;
normalized = null;
isNormalized = false;
normDatum = null;
utils = new NUAMDatumUtils();
}
// Constructor: parameters
public NUAMScalarDatum(double mg, double er, String an){
magnitude = mg;
error = er;
annotation = an;
reference = null;
normalized = null;
isNormalized = false;
normDatum = null;
utils = new NUAMDatumUtils();
}
// Getters
public double getMagnitude(){
return magnitude;
}
public double getError(){
return error;
}
public String getAnnotation(){
return annotation;
}
// Setters
public void setMagnitude(double mg){
magnitude = mg;
}
public void setError(double er){
error = er;
}
public void setAnnotation(String an){
annotation = an;
}
// This is the only point where we reference the
// vector of observations where I (datum) belong to
public void setVector(Vector r, Vector n){
reference = r;
normalized = n;
}
// Private functions, normalize me
// Assumes the vector is full and no more
// data will be added
@SuppressWarnings("unchecked")
private void normalize(){
// Fist step, if I am normalized, do not proceed
// the value we look for is alredy in the normalized set
if (isNormalized)
return;
// else
// Get the highest and lowest values we have
double xmax = utils.findScalarXmax(reference);
double xmin = utils.findScalarXmin(reference);
double emax = utils.findScalarEmax(reference);
double emin = utils.findScalarEmin(reference);
// Now, recalculate my values in a normalized way
double nmagnitude = (magnitude - (xmax - xmin) / 2) / (xmax - xmin + 1);
double nerror = (error - (emax - emin) / 2) / (emax - emin + 1);
// Create a new instance of datum
NUAMScalarDatum ndatum = new NUAMScalarDatum(nmagnitude, nerror, this.annotation);
// Tell the world I have been renormalized in the other vector
ndatum.isNormalized = true;
normalized.add((Object)ndatum);
normDatum = ndatum;
isNormalized = true;
}
public double weightedObservation() {
if (! isNormalized)
normalize();
// Remap the normalized values into the interval ]-1, 1[
double xmap = normDatum.magnitude/(1 + Math.abs(normDatum.magnitude));
double emap = normDatum.error/(1 + Math.abs(normDatum.error));
return xmap * Math.exp(emap);
}
}