Example usage for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector

List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector.

Prototype

public ArrayRealVector(double[] v1, double[] v2) 

Source Link

Document

Construct a vector by appending one vector to another vector.

Usage

From source file:gamlss.distributions.TF.java

/** Second cross derivative of likelihood function 
 * in respect to sigma and nu (d2ldmdd = d2l/dsigma*dnu).
 * @param y - vector of values of response variable
 * @return  a vector of Second cross derivative
 *///  www . jav  a 2s.co m
private ArrayRealVector d2ldsdn(final ArrayRealVector y) {

    //d2ldddv = function(sigma,nu)  2/(sigma*(nu+3)*(nu+1)),
    ArrayRealVector sigmaT = distributionParameters.get(DistributionSettings.SIGMA);
    ArrayRealVector nuT = distributionParameters.get(DistributionSettings.NU);

    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        out[i] = 2.0 / (sigmaT.getEntry(i) * (nuT.getEntry(i) + 3) * (nuT.getEntry(i) + 1));
    }
    return new ArrayRealVector(out, false);
}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

/** vertices is passed as a list because the Set iterator from JGraphT is slow */
public ArrayRealVector align(V nodeToAlign, ObjectDoubleHashMap<V> neighbors, V[] vertices) {

    double nodeSpeed = getSpeedFactor(nodeToAlign);

    ArrayRealVector originalPosition = new ArrayRealVector(new double[2], true);
    //getPosition(nodeToAlign);
    //getCurrentPosition(nodeToAlign);
    getPosition(nodeToAlign, originalPosition.getDataRef());

    if (nodeSpeed == 0)
        return originalPosition;

    // calculate equilibrium with neighbors
    ArrayRealVector position = (ArrayRealVector) originalPosition.mapMultiplyToSelf(1.0 / scale);

    getNeighbors(nodeToAlign, neighbors);

    ArrayRealVector delta = newVector();

    double radius = getRadius(nodeToAlign);
    double targetDistance = radius + equilibriumDistance;
    double learningRate = this.learningRate;

    ArrayRealVector tmpAttractVector = newVector();

    // align with neighbours
    neighbors.forEachKeyValue((neighbor, distToNeighbor) -> {

        ArrayRealVector attractVector = getThePosition(neighbor, tmpAttractVector).combineToSelf(1, -1,
                position);//w w w .  j  ava  2s  .c  o m

        double oldDistance = magnitude(attractVector);

        double newDistance;
        double factor = 0;
        double deltaDist = oldDistance - distToNeighbor;
        if (oldDistance > distToNeighbor) {
            newDistance = Math.pow(deltaDist, attractionStrength);

        } else {

            newDistance = -targetDistance * atanh((-deltaDist) / distToNeighbor);

            if (Math.abs(newDistance) > (Math.abs(deltaDist))) {
                newDistance = -targetDistance * (-deltaDist);
            }

        }

        newDistance *= learningRate;
        if (oldDistance != 0) {
            factor = newDistance / oldDistance;
        }

        add(delta, attractVector, factor);
    });

    ArrayRealVector repelVector = newVector();
    double maxEffectiveDistance = targetDistance * maxRepulsionDistance;

    ArrayRealVector nodePos = newVector();

    // calculate repulsion with all non-neighbors

    DistanceMetric distanceFunction = this.distanceFunction;
    double minDistance = this.minDistance;
    double repulsiveWeakness = this.repulsiveWeakness;

    for (V node : vertices) {

        if (node == null)
            continue;

        //vertices.forEach((Consumer<N>)node -> {
        //for (final N node : vertices) {
        if ((node == nodeToAlign) || (neighbors.containsKey(node)))
            continue;

        double oldDistance = distanceFunction.subtractIfLessThan(getThePosition(node, nodePos), position,
                repelVector, maxEffectiveDistance);

        if (oldDistance == Double.POSITIVE_INFINITY)
            continue; //too far to matter

        if (oldDistance < minDistance)
            oldDistance = minDistance; //continue;
        //throw new RuntimeException("invalid oldDistance");

        double newDistance = -targetDistance * Math.pow(oldDistance, -repulsiveWeakness);

        if (Math.abs(newDistance) > targetDistance) {
            newDistance = Math.copySign(targetDistance, newDistance);
        }
        newDistance *= learningRate;

        add(delta, repelVector, newDistance / oldDistance);
    }

    /*if (normalizeRepulsion)
    nodeSpeed/=delta.getNorm(); //TODO check when norm = 0*/

    if (nodeSpeed != 1.0) {
        delta.mapMultiplyToSelf(nodeSpeed);
    }

    double moveDistance = magnitude(delta);
    if (!Double.isFinite(moveDistance))
        throw new RuntimeException("invalid magnitude");

    if (moveDistance > targetDistance * acceptableMaxDistanceFactor) {
        double newLearningRate = ((targetDistance * acceptableMaxDistanceFactor) / moveDistance);
        if (newLearningRate < learningRate) {
            this.learningRate = newLearningRate;
        } else {
            this.learningRate *= LEARNING_RATE_INCREASE_FACTOR / vertices.length;
        }

        moveDistance = DEFAULT_TOTAL_MOVEMENT;
    } else {
        add(position, delta);
    }

    if (moveDistance > maxMovement) {
        maxMovement = moveDistance;
    }
    totalMovement += moveDistance;

    originalPosition.mapMultiplyToSelf(scale);
    move(nodeToAlign, originalPosition.getEntry(0), originalPosition.getEntry(1));
    return originalPosition;
}

From source file:gamlss.algorithm.GlimFit.java

/**
 * Checks whether the values of the second derrivative greater 
 * than -1e-15d and if they are, sets these values = -1e-15d.
 * @return vector of second derivative values with respect to 
 * the fitted distribution parameter/*  w w  w. java 2  s. c  o m*/
 */
private ArrayRealVector d2ldp2Check() {
    //ifelse(d2ldp2 < -1e-15, d2ldp2,-1e-15)
    double value = -1e-15d;
    tempArr = new double[d2ldp2.getDimension()];
    for (int i = 0; i < tempArr.length; i++) {
        if (d2ldp2.getEntry(i) > value) {
            tempArr[i] = value;
        } else {
            tempArr[i] = d2ldp2.getEntry(i);
        }
    }
    return new ArrayRealVector(tempArr, false);
}

From source file:gamlss.utilities.MakeLinkFunction.java

/**
 * Calculates the values of distribution parameter  Eta vector according to identity link function
 * @param eta - vector of linear predictor values
 * @return muEta vector//from   w ww .  j av  a 2  s.co m
 */
public ArrayRealVector identityDistParameterEta(ArrayRealVector eta) {
    ArrayRealVector out = new ArrayRealVector(new double[eta.getDimension()], false);
    out.set(1);
    return out;
}

From source file:gamlss.distributions.GT.java

/** Second derivative d2ldt2= (d^2l)/(dtau^2),
 * where l - log-likelihood function.//from  w w w  . j  a  v a2s  .c  o m
 * @param y - vector of values of response variable
 * @return  a vector of second derivative d2ldt2= (d^2l)/(dtau^2)
 */
private ArrayRealVector d2ldt2(final ArrayRealVector y) {

    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        //d2ldt2 <- -dldt*dldt
        out[i] = -dldt[i] * dldt[i];

        //d2ldt2 <- ifelse(d2ldt2 < -1e-15, d2ldt2,-1e-15)
        if (out[i] > -1e-15) {
            out[i] = -1e-15;
        }
    }
    muV = null;
    sigmaV = null;
    nuV = null;
    tauV = null;
    return new ArrayRealVector(out, false);
}

From source file:gamlss.distributions.TF.java

/** Computes the global Deviance Increament.
 * @param y - vector of response variable values
 * @return vector of global Deviance Increament values 
 *//*from  w  ww  .  j  a v  a 2 s.com*/
public final ArrayRealVector globalDevianceIncreament(final ArrayRealVector y) {
    //G.dev.incr  = function(y,mu,sigma,nu,tau,...)  
    size = y.getDimension();
    double[] out = new double[size];

    double[] muArr = distributionParameters.get(DistributionSettings.MU).getDataRef();
    double[] sigmaArr = distributionParameters.get(DistributionSettings.SIGMA).getDataRef();
    double[] nuArr = distributionParameters.get(DistributionSettings.NU).getDataRef();

    for (int i = 0; i < size; i++) {

        out[i] = (-2) * dTF(y.getEntry(i), muArr[i], sigmaArr[i], nuArr[i], Controls.LOG_LIKELIHOOD);
    }
    return new ArrayRealVector(out, false);
}

From source file:gamlss.distributions.BCPE.java

/** First derivative dldtau = dl/dtau,
 *  where l - log-likelihood function.//  w  ww  .  j  a va  2 s .  c om
 * @param y - vector of values of response variable
 * @return  a vector of First derivative dldtau = dl/dtau
 */
public final ArrayRealVector dldt(final ArrayRealVector y) {

    dldt = new double[size];
    for (int i = 0; i < size; i++) {

        //j <- (log(F.T(1/(sigma*abs(nu)),tau+0.001))
        //-log(F.T(1/(sigma*abs(nu)),tau)))/0.001
        final double j = (FastMath
                .log(f2T(1 / (sigmaV.getEntry(i) * FastMath.abs(nuV.getEntry(i))), tauV.getEntry(i) + 0.001, i))
                - FastMath.log(
                        f2T(1 / (sigmaV.getEntry(i) * FastMath.abs(nuV.getEntry(i))), tauV.getEntry(i), i)))
                / 0.001;

        //dldt <- (1/tau)-0.5*(log(abs(z/c)))*
        //(abs(z/c))^tau+(1/tau^2)*(log(2)+digamma(1/tau))+((tau/2)*
        //((abs(z/c))^tau)-1)*dlogc.dt-j
        dldt[i] = (1 / tauV.getEntry(i))
                - 0.5 * (FastMath.log(FastMath.abs(z[i] / c[i])))
                        * (FastMath.pow(FastMath.abs(z[i] / c[i]), tauV.getEntry(i)))
                + (1 / (tauV.getEntry(i) * tauV.getEntry(i)))
                        * (FastMath.log(2) + Gamma.digamma(1 / tauV.getEntry(i)))
                + ((tauV.getEntry(i) / 2) * (FastMath.pow(FastMath.abs(z[i] / c[i]), tauV.getEntry(i))) - 1)
                        * dlogcDt[i]
                - j;
    }
    c = null;
    logC = null;
    z = null;
    return new ArrayRealVector(dldt, false);
}

From source file:gamlss.utilities.MakeLinkFunction.java

/**
 * Calculates the values of  distribution parameter  Eta vector according to log link function
 * @param eta - vector of linear predictor values
 * @return muEta vector/*from  ww  w. ja  v  a 2 s.  c  om*/
 */
public ArrayRealVector logDistParameterEta(ArrayRealVector eta) {
    int size = eta.getDimension();
    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        out[i] = FastMath.exp(eta.getEntry(i));
        if (out[i] < 2.220446e-16) //!!!!    .Machine$double.eps
        {
            out[i] = 2.220446e-16;

        }
    }
    return new ArrayRealVector(out, false);
}

From source file:gamlss.distributions.TF2.java

/** Second cross derivative of likelihood function 
 * in respect to sigma and nu (d2ldmdd = d2l/dsigma*dnu).
 * @param y - vector of values of response variable
 * @return  a vector of Second cross derivative
 *///from ww w.ja  v  a 2  s  .c om
private ArrayRealVector d2ldsdn(final ArrayRealVector y) {

    sigmaV = distributionParameters.get(DistributionSettings.SIGMA);
    nuV = distributionParameters.get(DistributionSettings.NU);

    size = y.getDimension();
    double[] sigma1T = new double[size];
    double[] ds1ddT = new double[size];
    double[] ds1dvT = new double[size];
    double[] d2ldddvT = new double[size];
    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        //sigma1 <- (sqrt((nu-2)/nu))*sigma
        sigma1T[i] = FastMath.sqrt((nuV.getEntry(i) - 2.0) / nuV.getEntry(i)) * sigmaV.getEntry(i);

        //ds1dd <-  (sqrt((nu-2)/nu)) 
        ds1ddT[i] = (FastMath.sqrt((nuV.getEntry(i) - 2.0) / nuV.getEntry(i)));

        // ds1dv <-  (sqrt(nu/(nu-2)))*sigma/(nu^2) 
        ds1dvT[i] = (FastMath.sqrt(nuV.getEntry(i) / (nuV.getEntry(i) - 2))) * sigmaV.getEntry(i)
                / (nuV.getEntry(i) * nuV.getEntry(i));

        //d2ldddv <-  ds1dd*2/(sigma1*(nu+3)*(nu+1)) 
        d2ldddvT[i] = ds1ddT[i] * 2 / (sigma1T[i] * (nuV.getEntry(i) + 3) * (nuV.getEntry(i) + 1));
    }

    //d2ldddv <- d2ldddv + ds1dd*ds1dv*TF()$d2ldd2(sigma1, nu)
    tf.setDistributionParameter(DistributionSettings.SIGMA, new ArrayRealVector(sigma1T, false));
    tf.setDistributionParameter(DistributionSettings.NU, nuV);

    tf.firstDerivative(DistributionSettings.SIGMA, y);
    tempV = tf.secondDerivative(DistributionSettings.SIGMA, y);

    for (int i = 0; i < size; i++) {
        out[i] = d2ldddvT[i] + ds1ddT[i] * ds1dvT[i] * tempV.getEntry(i);
    }
    tempV = null;
    sigmaV = null;
    nuV = null;
    return new ArrayRealVector(out, false);
}

From source file:gamlss.utilities.MakeLinkFunction.java

/**
 * Calculates the values of distribution parameter Eta vector according to log link function
 * @param eta - vector of linear predictor values
 * @return muEta vector//from ww  w . j a  v a  2  s  .c o  m
 */
public ArrayRealVector logShiftTo2DistParameterEta(ArrayRealVector eta) {
    int size = eta.getDimension();
    double[] out = new double[size];
    for (int i = 0; i < size; i++) {
        //mu.eta <- function(eta) pmax(.Machine$double.eps, exp(eta))
        out[i] = FastMath.exp(eta.getEntry(i));
        if (out[i] < Double.MIN_VALUE) //!!!!    .Machine$double.eps
        {
            out[i] = Double.MIN_VALUE;
        }
    }
    return new ArrayRealVector(out, false);
}