📄 regressionprior.java
字号:
for (int i = 0; i < numDimensions; ++i) log2Prior += log2Prior(beta.value(i),i); return log2Prior; } /** * Returns the log (base 2) prior density for the specified * array of coefficient vectors. * * @param betas The parameter vectors. * @return The log (base 2) prior density for the specified * @throws IllegalArgumentException If any of the specified * parameter vectors does not match the dimensionality of the * prior (if specified). */ public double log2Prior(Vector[] betas) { double log2Prior = 0.0; for (Vector beta : betas) log2Prior += log2Prior(beta); return log2Prior; } // package local on purpose void verifyNumberOfDimensions(int numDimensions) { // do nothing on purpose } private final static RegressionPrior NONINFORMATIVE_PRIOR = new NoninformativeRegressionPrior(); /** * Returns the noninformative or uniform prior to use for * maximum likelihood regression fitting. * * @return The noninformative prior. */ public static RegressionPrior noninformative() { return NONINFORMATIVE_PRIOR; } /** * Returns the Gaussian prior with the specified prior variance * and indication of whether the intercept is given a * noninformative prior. * * <p>If the noninformative-intercept flag is set to * <code>true</code>, the prior variance for dimension zero * (<code>0</code>) is set to {@link Double#POSITIVE_INFINITY}. * * <p>See the class documentation above for more inforamtion on * Gaussian priors. * * @param priorVariance Variance of the Gaussian prior for each * dimension. * @param noninformativeIntercept Flag indicating if intercept is * given a noninformative (uniform) prior. * @return The Gaussian prior with the specified parameters. * @throws IllegalArgumentException If the prior variance is not * a non-negative number. */ public static RegressionPrior gaussian(double priorVariance, boolean noninformativeIntercept) { verifyPriorVariance(priorVariance); return new VariableGaussianRegressionPrior(priorVariance,noninformativeIntercept); } /** * Returns the Gaussian prior with the specified priors for * each dimension. The number of dimensions is taken to be * the length of the variance array. * * <p>See the class documentation above for more inforamtion on * Gaussian priors. * * @param priorVariances Array of prior variances for dimensions. * @return The Gaussian prior with the specified variances. * @throws IllegalArgumentException If any of the variances are not * non-negative numbers. * */ public static RegressionPrior gaussian(double[] priorVariances) { verifyPriorVariances(priorVariances); return new GaussianRegressionPrior(priorVariances); } /** * Returns the Laplace prior with the specified prior variance * and number of dimensions and indication of whether the * intecept dimension is given a noninformative prior. * * <p>If the noninformative-intercept flag is set to * <code>true</code>, the prior variance for dimension zero * (<code>0</code>) is set to {@link Double#POSITIVE_INFINITY}. * * <p>See the class documentation above for more inforamtion on * Laplace priors. * * @param priorVariance Variance of the Laplace prior for each * dimension. * @param noninformativeIntercept Flag indicating if intercept is * given a noninformative (uniform) prior. * @return The Laplace prior with the specified parameters. * @throws IllegalArgumentException If the variance is not a non-negative * number. */ public static RegressionPrior laplace(double priorVariance, boolean noninformativeIntercept) { verifyPriorVariance(priorVariance); return new VariableLaplaceRegressionPrior(priorVariance,noninformativeIntercept); } /** * Returns the Laplace prior with the specified prior variances * for the dimensions. * * <p>See the class documentation above for more inforamtion on * Laplace priors. * * @param priorVariances Array of prior variances for dimensions. * @return The Laplace prior for the specified variances. * @throws IllegalArgumentException If any of the variances is not * a non-negative number. */ public static RegressionPrior laplace(double[] priorVariances) { verifyPriorVariances(priorVariances); return new LaplaceRegressionPrior(priorVariances); } /** * Returns the Cauchy prior with the specified prior squared * scales for the dimensions. * * <p>See the class documentation above for more information * on Cauchy priors. * * @param priorSquaredScale The square of the prior scae parameter. * @param noninformativeIntercept Flag indicating if intercept is * given a noninformative (uniform) prior. * @return The Cauchy prior for the specified squared scale and * intercept flag. * @throws IllegalArgumentException If the scale is not a non-negative * number. */ public static RegressionPrior cauchy(double priorSquaredScale, boolean noninformativeIntercept) { verifyPriorVariance(priorSquaredScale); return new VariableCauchyRegressionPrior(priorSquaredScale,noninformativeIntercept); } /** * Returns the Cauchy prior for the specified squared scales. * * <p>See the class documentation above for more information * on Cauchy priors. * * @param priorSquaredScales Prior squared scale parameters. * @return The Cauchy prior for the specified square scales. * @throws IllegalArgumentException If any of the prior squared * scales is not a non-negative number. */ public static RegressionPrior cauchy(double[] priorSquaredScales) { verifyPriorVariances(priorSquaredScales); return new CauchyRegressionPrior(priorSquaredScales); } static void verifyPriorVariance(double priorVariance) { if (priorVariance < 0 || Double.isNaN(priorVariance) || priorVariance == Double.NEGATIVE_INFINITY) { String msg = "Prior variance must be a non-negative number." + " Found priorVariance=" + priorVariance; throw new IllegalArgumentException(msg); } } static void verifyPriorVariances(double[] priorVariances) { for (int i = 0; i < priorVariances.length; ++i) { if (priorVariances[i] < 0 || Double.isNaN(priorVariances[i]) || priorVariances[i] == Double.NEGATIVE_INFINITY) { String msg = "Prior variances must be non-negative numbers." + " Found priorVariances[" + i + "]=" + priorVariances[i]; throw new IllegalArgumentException(msg); } } } static class NoninformativeRegressionPrior extends RegressionPrior implements Serializable { public double gradient(double beta, int dimension) { return 0.0; } public double log2Prior(double beta, int dimension) { return 0.0; // log2(1) = 0 } public double log2Prior(Vector beta) { return 0.0; } public double log2Prior(Vector[] betas) { return 0.0; } public String toString() { return "NoninformativeRegressionPrior"; } } static abstract class ArrayRegressionPrior extends RegressionPrior { final double[] mValues; ArrayRegressionPrior(double[] values) { mValues = values; } void verifyNumberOfDimensions(int numDimensions) { if (mValues.length != numDimensions) { String msg = "Prior and instances must match in number of dimensions." + " Found prior numDimensions=" + mValues.length + " instance numDimensions=" + numDimensions; throw new IllegalArgumentException(msg); } } public String toString(String priorName, String paramName) { StringBuilder sb = new StringBuilder(); sb.append(priorName + "\n"); sb.append(" dimensionality=" + mValues.length); for (int i = 0; i < mValues.length; ++i) sb.append(" " + paramName + "[" + i + "]=" + mValues[i] + "\n"); return sb.toString(); } } static class GaussianRegressionPrior extends ArrayRegressionPrior implements Serializable { GaussianRegressionPrior(double[] priorVariances) { super(priorVariances); } public double gradient(double beta, int dimension) { return beta / mValues[dimension]; } public double log2Prior(double beta, int dimension) { return -log2Sqrt2Pi - 0.5 * com.aliasi.util.Math.log2(mValues[dimension]) - beta * beta / (2.0 * mValues[dimension]); } public String toString() { return toString("GaussianRegressionPrior","Variance"); } private Object writeReplace() { return new Serializer(this); } private static class Serializer extends AbstractExternalizable { final GaussianRegressionPrior mPrior; public Serializer(GaussianRegressionPrior prior) { mPrior = prior; } public Serializer() { this(null); } public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(mPrior.mValues.length); for (int i = 0; i < mPrior.mValues.length; ++i) out.writeDouble(mPrior.mValues[i]); } public Object read(ObjectInput in) throws IOException, ClassNotFoundException { int numDimensions = in.readInt(); double[] priorVariances = new double[numDimensions]; for (int i = 0; i < numDimensions; ++i) priorVariances[i] = in.readDouble(); return new GaussianRegressionPrior(priorVariances);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -