/** * Inner Vector product between two Vectors * * * @param other * the Vector with which to compute the dot product with this, * must be the same dimension as this * @return dot product, (0,\infty) */ public double dotProduct( final AbstractMTJVector other) { this.assertSameDimensionality(other); return this.internalVector.dot( other.internalVector ); }
/** * Inner Vector product between two Vectors * * * @param other * the Vector with which to compute the dot product with this, * must be the same dimension as this * @return dot product, (0,\infty) */ public double dotProduct( final AbstractMTJVector other) { this.assertSameDimensionality(other); return this.internalVector.dot( other.internalVector ); }
/** * Inner Vector product between two Vectors * * * @param other * the Vector with which to compute the dot product with this, * must be the same dimension as this * @return dot product, (0,\infty) */ public double dotProduct( final AbstractMTJVector other) { this.assertSameDimensionality(other); return this.internalVector.dot( other.internalVector ); }
/** * Computes standard deviation for given instance, without transforming target * back into original space. */ protected double computeStdDev(Instance inst, Vector k) throws Exception { double kappa = m_actualKernel.eval(-1, -1, inst) + m_deltaSquared; double s = m_L.mult(k, new DenseVector(k.size())).dot(k); double sigma = m_delta; if (kappa > s) { sigma = Math.sqrt(kappa - s); } return sigma; }
/** * Computes standard deviation for given instance, without transforming target * back into original space. */ protected double computeStdDev(Instance inst, Vector k) throws Exception { double kappa = m_actualKernel.eval(-1, -1, inst) + m_deltaSquared; double s = m_L.mult(k, new DenseVector(k.size())).dot(k); double sigma = m_delta; if (kappa > s) { sigma = Math.sqrt(kappa - s); } return sigma; }
/** * Returns natural logarithm of density estimate for given value based on * given instance. * * @param inst the instance to make the prediction for. * @param value the value to make the prediction for. * @return the natural logarithm of the density estimate * @exception Exception if the density cannot be computed */ @Override public double logDensity(Instance inst, double value) throws Exception { inst = filterInstance(inst); // Build K vector (and Kappa) Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double estimate = k.dot(m_t) + m_avg_target; double sigma = computeStdDev(inst, k); // transform to GP space value = value * m_Alin + m_Blin; // center around estimate value = value - estimate; double z = -Math.log(sigma * Math.sqrt(2 * Math.PI)) - value * value / (2.0 * sigma * sigma); return z + Math.log(m_Alin); }
/** * Returns natural logarithm of density estimate for given value based on * given instance. * * @param inst the instance to make the prediction for. * @param value the value to make the prediction for. * @return the natural logarithm of the density estimate * @exception Exception if the density cannot be computed */ @Override public double logDensity(Instance inst, double value) throws Exception { inst = filterInstance(inst); // Build K vector (and Kappa) Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double estimate = k.dot(m_t) + m_avg_target; double sigma = computeStdDev(inst, k); // transform to GP space value = value * m_Alin + m_Blin; // center around estimate value = value - estimate; double z = -Math.log(sigma * Math.sqrt(2 * Math.PI)) - value * value / (2.0 * sigma * sigma); return z + Math.log(m_Alin); }
/** * Returns the log of the density value for the given vector. * * @param valuePassed input vector * @return log density based on given distribution */ @Override public double logDensity(double[] valuePassed) { // calculate mean subtractions Vector x = new DenseVector(valuePassed); return lnconstant - 0.5 * x.dot(covarianceInverse.mult(x.add(-1.0, mean), new DenseVector(x.size()))); }
@Override public IndependentPair<double[], PerceptronClass> generate() { double decide = Math.signum(rng.nextDouble() - 0.5); if(decide == 0) decide = 1; PerceptronClass dec = PerceptronClass.fromSign(decide); while(true){ double[] randomPoint = new double[this.dims]; for (int i = 0; i < randomPoint.length; i++) { randomPoint[i] = nextRandomValue(); } Vector v = new DenseVector(randomPoint); v.add(-1, origin); Vector d = direction.copy(); double dot = v.dot(d); double sgn = Math.signum(dot); if(sgn == 0) sgn = 1; if(rng.nextDouble() <this.error){ sgn = -sgn; } PerceptronClass sgnClass = PerceptronClass.fromSign(sgn); if(sgnClass.equals(dec)) { return IndependentPair.pair(randomPoint, sgnClass); } } }
/** * Returns the log of the density value for the given vector. * * @param valuePassed input vector * @return log density based on given distribution */ @Override public double logDensity(double[] valuePassed) { // calculate mean subtractions Vector x = new DenseVector(valuePassed); return lnconstant - 0.5 * x.dot(covarianceInverse.mult(x.add(-1.0, mean), new DenseVector(x.size()))); }
/** * Classifies a given instance. * * @param inst the instance to be classified * @return the classification * @throws Exception if instance could not be classified successfully */ @Override public double classifyInstance(Instance inst) throws Exception { // Filter instance inst = filterInstance(inst); // Build K vector Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double result = (k.dot(m_t) + m_avg_target - m_Blin) / m_Alin; return result; }
/** * Classifies a given instance. * * @param inst the instance to be classified * @return the classification * @throws Exception if instance could not be classified successfully */ @Override public double classifyInstance(Instance inst) throws Exception { // Filter instance inst = filterInstance(inst); // Build K vector Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double result = (k.dot(m_t) + m_avg_target - m_Blin) / m_Alin; return result; }
/** * Output class "probabilities". These need to be calibrated. */ public double[] distributionForInstance(Instance inst) throws Exception { // Filter instance m_RemoveUseless.input(inst); inst = m_RemoveUseless.output(); // Convert instance to matrix Vector instM = new DenseVector(inst.numAttributes() - 1); int index = 0; for (int i = 0; i < inst.numAttributes(); i++) { if (i != m_Data.classIndex()) { instM.set(index++, inst.value(i)); } } // Pipe output through sigmoid double[] dist = new double[2]; dist[1] = 1/(1 + Math.exp(instM.dot(m_Weights) - m_Threshold)); dist[0] = 1 - dist[1]; return dist; }
/** * Computes a prediction interval for the given instance and confidence level. * * @param inst the instance to make the prediction for * @param confidenceLevel the percentage of cases the interval should cover * @return a 1*2 array that contains the boundaries of the interval * @throws Exception if interval could not be estimated successfully */ @Override public double[][] predictIntervals(Instance inst, double confidenceLevel) throws Exception { inst = filterInstance(inst); // Build K vector (and Kappa) Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double estimate = k.dot(m_t) + m_avg_target; double sigma = computeStdDev(inst, k); confidenceLevel = 1.0 - ((1.0 - confidenceLevel) / 2.0); double z = Statistics.normalInverse(confidenceLevel); double[][] interval = new double[1][2]; interval[0][0] = estimate - z * sigma; interval[0][1] = estimate + z * sigma; interval[0][0] = (interval[0][0] - m_Blin) / m_Alin; interval[0][1] = (interval[0][1] - m_Blin) / m_Alin; return interval; }
/** * Computes a prediction interval for the given instance and confidence level. * * @param inst the instance to make the prediction for * @param confidenceLevel the percentage of cases the interval should cover * @return a 1*2 array that contains the boundaries of the interval * @throws Exception if interval could not be estimated successfully */ @Override public double[][] predictIntervals(Instance inst, double confidenceLevel) throws Exception { inst = filterInstance(inst); // Build K vector (and Kappa) Vector k = new DenseVector(m_NumTrain); for (int i = 0; i < m_NumTrain; i++) { k.set(i, m_weights[i] * m_actualKernel.eval(-1, i, inst)); } double estimate = k.dot(m_t) + m_avg_target; double sigma = computeStdDev(inst, k); confidenceLevel = 1.0 - ((1.0 - confidenceLevel) / 2.0); double z = Statistics.normalInverse(confidenceLevel); double[][] interval = new double[1][2]; interval[0][0] = estimate - z * sigma; interval[0][1] = estimate + z * sigma; interval[0][0] = (interval[0][0] - m_Blin) / m_Alin; interval[0][1] = (interval[0][1] - m_Blin) / m_Alin; return interval; }
public Vector solve(Matrix A, Vector b, Vector x) throws IterativeSolverNotConvergedException { checkSizes(A, b, x); double alpha = 0, beta = 0, rho = 0, rho_1 = 0; A.multAdd(-1, x, r.set(b)); for (iter.setFirst(); !iter.converged(r, x); iter.next()) { M.apply(r, z); rho = r.dot(z); if (iter.isFirst()) p.set(z); else { beta = rho / rho_1; p.scale(beta).add(z); } A.mult(p, q); alpha = rho / p.dot(q); x.add(alpha, p); r.add(-alpha, q); rho_1 = rho; } return x; }
public Vector solve(Matrix A, Vector b, Vector x) throws IterativeSolverNotConvergedException { checkSizes(A, b, x); double alpha = 0, beta = 0, rho = 0, rho_1 = 0; A.multAdd(-1, x, r.set(b)); for (iter.setFirst(); !iter.converged(r, x); iter.next()) { M.apply(r, z); rho = r.dot(z); if (iter.isFirst()) p.set(z); else { beta = rho / rho_1; p.scale(beta).add(z); } A.mult(p, q); alpha = rho / p.dot(q); x.add(alpha, p); r.add(-alpha, q); rho_1 = rho; } return x; }