private double logsumexp(DoubleMatrix1D x){ DoubleMatrix1D myX = x.copy(); double maxX = StatUtils.max(myX.toArray()); return Math.log(myX.assign(Functions.minus(maxX)).assign(Functions.exp).zSum()) + maxX; }
double v1 = params.theta.viewColumn(i).viewPart(lcumsum[j], l[j]).copy().assign(Functions.abs).zSum();
public MGMParams(DoubleMatrix1D vec, int p, int ltot){ int[] lens = {p*p, p, p*ltot, ltot*ltot, p, ltot}; int[] lenSums = new int[lens.length]; lenSums[0] = lens[0]; for(int i = 1; i < lenSums.length; i++){ lenSums[i] = lens[i] + lenSums[i-1]; } if(vec.size() != lenSums[5]) throw new IllegalArgumentException("Param vector dimension doesn't match: Found " + vec.size() + " need " + lenSums[5]); beta = DoubleFactory2D.dense.make(vec.viewPart(0, lens[0]).toArray(), p); betad = vec.viewPart(lenSums[0], lens[1]).copy(); theta = DoubleFactory2D.dense.make(vec.viewPart(lenSums[1], lens[2]).toArray(), ltot); phi = DoubleFactory2D.dense.make(vec.viewPart(lenSums[2], lens[3]).toArray(), ltot); alpha1 = vec.viewPart(lenSums[3], lens[4]).copy(); alpha2 = vec.viewPart(lenSums[4], lens[5]).copy(); }
/** * Replaces all cell values of the receiver with the values of another matrix. * Both matrices must have the same size. * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>. * * @param other the source matrix to copy from (may be identical to the receiver). * @return <tt>this</tt> (for convenience only). * @throws IllegalArgumentException if <tt>size() != other.size()</tt>. */ public DoubleMatrix1D assign(DoubleMatrix1D other) { if (other==this) return this; checkSize(other); if (haveSharedCells(other)) other = other.copy(); for (int i=size; --i >= 0;) { setQuick(i,other.getQuick(i)); } return this; } /**
/** * Replaces all cell values of the receiver with the values of another matrix. * Both matrices must have the same size. * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>. * * @param other the source matrix to copy from (may be identical to the receiver). * @return <tt>this</tt> (for convenience only). * @throws IllegalArgumentException if <tt>size() != other.size()</tt>. */ public DoubleMatrix1D assign(DoubleMatrix1D other) { if (other==this) return this; checkSize(other); if (haveSharedCells(other)) other = other.copy(); for (int i=size; --i >= 0;) { setQuick(i,other.getQuick(i)); } return this; } /**
/** * Convert discrete data (in yDat) to a matrix of dummy variables (stored in dDat) */ private void makeDummy(){ dDat = factory2D.make(n, lsum); for(int i = 0; i < q; i++){ for(int j = 0; j < l[i]; j++){ DoubleMatrix1D curCol = yDat.viewColumn(i).copy().assign(Functions.equals(j+1)); if(curCol.zSum() == 0) throw new IllegalArgumentException("Discrete data is missing a level: variable " + i + " level " + j); dDat.viewColumn(lcumsum[i]+j).assign(curCol); } } }
DoubleMatrix1D b_x = new Algebra().mult(GZt, x); DoubleMatrix1D xPred = new Algebra().mult(Z, b_x); DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus); double SSE = xRes.aggregate(Functions.plus, Functions.square);
DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
DoubleMatrix1D xRes = xPred.copy().assign(x, Functions.minus);
private void calcWeights(){ weights = factory1D.make(p+q); for(int i = 0; i < p; i++){ weights.set(i, StatUtils.sd(xDat.viewColumn(i).toArray())); } for(int j = 0; j < q; j++){ double curWeight = 0; for(int k = 0; k < l[j] ; k++){ double curp = yDat.viewColumn(j).copy().assign(Functions.equals(k+1)).zSum()/(double) n; curWeight += curp*(1-curp); } weights.set(p+j, Math.sqrt(curWeight)); } }
public void drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s) { x.checkSize(y); DoubleMatrix1D tmp = x.copy(); x.assign(F.mult(c)); x.assign(y,F.plusMult(s)); y.assign(F.mult(c)); y.assign(tmp,F.minusMult(s)); } public void drotg(double a, double b, double rotvec[]) {
public void drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s) { x.checkSize(y); DoubleMatrix1D tmp = x.copy(); x.assign(F.mult(c)); x.assign(y,F.plusMult(s)); y.assign(F.mult(c)); y.assign(tmp,F.minusMult(s)); } public void drotg(double a, double b, double rotvec[]) {
protected double computeJSDivergence(DoubleMatrix1D v1, DoubleMatrix1D v2) { assert(v1.size() == v2.size()); double sum = 0.0; for(int i = 0; i < v2.size(); ++i ) { sum += v2.getQuick(i); } DoubleMatrix1D v3 = v2.copy(); for(int i = 0; i < v2.size(); ++i ) { v3.set(i, v2.getQuick(i)/sum); } double divergence = 0.0; double[] meanVector = averageDist(v1, v2); divergence = computeKLDivergence(v1, meanVector) + computeKLDivergence(v2, meanVector); return divergence; }
gAugm = gATQh; }else{ gAugm = g.copy();