| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.glm; |
| 27 |
|
| 28 |
import cern.colt.matrix.impl.DenseDoubleMatrix2D; |
| 29 |
import cern.colt.matrix.linalg.SingularValueDecomposition; |
| 30 |
import dr.inference.distribution.DensityModel; |
| 31 |
import dr.inference.distribution.ParametricDistributionModel; |
| 32 |
import dr.inference.distribution.ParametricMultivariateDistributionModel; |
| 33 |
import dr.inference.loggers.LogColumn; |
| 34 |
import dr.inference.loggers.NumberColumn; |
| 35 |
import dr.inference.model.*; |
| 36 |
import dr.inferencexml.glm.GeneralizedLinearModelParser; |
| 37 |
import dr.math.MultivariateFunction; |
| 38 |
import dr.math.distributions.Distribution; |
| 39 |
import dr.util.Transform; |
| 40 |
import org.w3c.dom.Document; |
| 41 |
import org.w3c.dom.Element; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.List; |
| 45 |
import java.util.logging.Logger; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public final class GeneralizedLinearModel extends AbstractModelLikelihood { |
| 52 |
|
| 53 |
|
| 54 |
public enum LinkFunction { |
| 55 |
IDENTITY(new Transform.NoTransform()), |
| 56 |
LOG(new Transform.LogTransform()), |
| 57 |
LOGIT(new Transform.LogitTransform()); |
| 58 |
|
| 59 |
LinkFunction(Transform transform) { |
| 60 |
this.transform = transform; |
| 61 |
} |
| 62 |
|
| 63 |
public Transform getTransform() { |
| 64 |
return transform; |
| 65 |
} |
| 66 |
|
| 67 |
private final Transform transform; |
| 68 |
} |
| 69 |
|
| 70 |
private final Transform linkFunction; |
| 71 |
private final DensityModel density; |
| 72 |
private final boolean isMultivariateDensity; |
| 73 |
|
| 74 |
private final Parameter dependentParameter; |
| 75 |
private final List<Parameter> independentParameter = new ArrayList<Parameter>(); |
| 76 |
private final List<Parameter> independentParameterDelta = new ArrayList<Parameter>(); |
| 77 |
private final List<DesignMatrix> designMatrix = new ArrayList<DesignMatrix>(); |
| 78 |
|
| 79 |
private int numIndependentVariables = 0; |
| 80 |
private int numRandomEffects = 0; |
| 81 |
private int N; |
| 82 |
|
| 83 |
protected List<Parameter> randomEffects = null; |
| 84 |
|
| 85 |
private double[] transformedXBeta; |
| 86 |
private double[] storedTransformedXBeta; |
| 87 |
private boolean transformedXBetaKnown = false; |
| 88 |
|
| 89 |
private double[] Y; |
| 90 |
|
| 91 |
private double storedLogLikelihood; |
| 92 |
private double logLikelihood; |
| 93 |
private boolean likelihoodKnown = false; |
| 94 |
|
| 95 |
public GeneralizedLinearModel(Parameter dependentParameter, DensityModel density, LinkFunction linkFunction) { |
| 96 |
super(GeneralizedLinearModelParser.GLM_LIKELIHOOD); |
| 97 |
this.dependentParameter = dependentParameter; |
| 98 |
this.linkFunction = linkFunction.getTransform(); |
| 99 |
this.density = density; |
| 100 |
isMultivariateDensity = density instanceof ParametricMultivariateDistributionModel; |
| 101 |
|
| 102 |
addModel(density); |
| 103 |
|
| 104 |
if (dependentParameter != null) { |
| 105 |
addVariable(dependentParameter); |
| 106 |
N = dependentParameter.getDimension(); |
| 107 |
} else { |
| 108 |
N = 0; |
| 109 |
} |
| 110 |
|
| 111 |
transformedXBeta = new double[N]; |
| 112 |
storedTransformedXBeta = new double[N]; |
| 113 |
|
| 114 |
Y = dependentParameter.getParameterValues(); |
| 115 |
|
| 116 |
transformedXBetaKnown = false; |
| 117 |
likelihoodKnown = false; |
| 118 |
} |
| 119 |
|
| 120 |
public void addRandomEffectsParameter(Parameter effect) { |
| 121 |
if (randomEffects == null) { |
| 122 |
randomEffects = new ArrayList<Parameter>(); |
| 123 |
} |
| 124 |
if (N != 0 && effect.getDimension() != N) { |
| 125 |
throw new RuntimeException("Random effects have the wrong dimension"); |
| 126 |
} |
| 127 |
addVariable(effect); |
| 128 |
randomEffects.add(effect); |
| 129 |
numRandomEffects++; |
| 130 |
} |
| 131 |
|
| 132 |
public void addIndependentParameter(Parameter effect, DesignMatrix matrix, Parameter delta) { |
| 133 |
if (N == 0) { |
| 134 |
N = matrix.getRowDimension(); |
| 135 |
} |
| 136 |
|
| 137 |
designMatrix.add(matrix); |
| 138 |
independentParameter.add(effect); |
| 139 |
independentParameterDelta.add(delta); |
| 140 |
|
| 141 |
if (designMatrix.size() != independentParameter.size()) { |
| 142 |
throw new RuntimeException("Independent variables and their design matrices are out of sync"); |
| 143 |
} |
| 144 |
|
| 145 |
addVariable(effect); |
| 146 |
addVariable(matrix); |
| 147 |
|
| 148 |
if (delta != null) { |
| 149 |
addVariable(delta); |
| 150 |
} |
| 151 |
|
| 152 |
numIndependentVariables++; |
| 153 |
|
| 154 |
Logger.getLogger("dr.inference").info("\tAdding independent predictors '" + effect.getStatisticName() + "' with design matrix '" + matrix.getStatisticName() + "'"); |
| 155 |
} |
| 156 |
|
| 157 |
public boolean getAllIndependentVariablesIdentifiable() { |
| 158 |
|
| 159 |
int totalColDim = 0; |
| 160 |
for (DesignMatrix mat : designMatrix) { |
| 161 |
totalColDim += mat.getColumnDimension(); |
| 162 |
} |
| 163 |
|
| 164 |
double[][] grandDesignMatrix = new double[N][totalColDim]; |
| 165 |
|
| 166 |
int offset = 0; |
| 167 |
for (DesignMatrix mat : designMatrix) { |
| 168 |
final int length = mat.getColumnDimension(); |
| 169 |
for (int i = 0; i < N; ++i) { |
| 170 |
for (int j = 0; j < length; ++j) { |
| 171 |
grandDesignMatrix[i][offset + j] = mat.getParameterValue(i, j); |
| 172 |
} |
| 173 |
} |
| 174 |
offset += length; |
| 175 |
} |
| 176 |
|
| 177 |
double[][] mat = grandDesignMatrix; |
| 178 |
|
| 179 |
if (grandDesignMatrix.length < grandDesignMatrix[0].length) { |
| 180 |
mat = new double[grandDesignMatrix[0].length][grandDesignMatrix.length]; |
| 181 |
|
| 182 |
for (int i = 0; i < grandDesignMatrix.length; ++i) { |
| 183 |
for (int j = 0; j < grandDesignMatrix[i].length; ++j) { |
| 184 |
mat[j][i] = grandDesignMatrix[i][j]; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
SingularValueDecomposition svd = new SingularValueDecomposition(new DenseDoubleMatrix2D(mat)); |
| 190 |
|
| 191 |
int rank = svd.rank(); |
| 192 |
boolean isFullRank = (totalColDim == rank); |
| 193 |
Logger.getLogger("dr.inference").info("\tTotal # of predictors = " + totalColDim + " and rank = " + rank); |
| 194 |
return isFullRank; |
| 195 |
} |
| 196 |
|
| 197 |
public double[] getXBeta() { |
| 198 |
|
| 199 |
double[] xBeta = new double[N]; |
| 200 |
|
| 201 |
for (int j = 0; j < numIndependentVariables; j++) { |
| 202 |
Parameter beta = independentParameter.get(j); |
| 203 |
Parameter delta = independentParameterDelta.get(j); |
| 204 |
DesignMatrix X = designMatrix.get(j); |
| 205 |
final int K = beta.getDimension(); |
| 206 |
for (int k = 0; k < K; k++) { |
| 207 |
double betaK = beta.getParameterValue(k); |
| 208 |
if (delta != null) |
| 209 |
betaK *= delta.getParameterValue(k); |
| 210 |
for (int i = 0; i < N; i++) |
| 211 |
xBeta[i] += X.getParameterValue(i, k) * betaK; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
for (int j = 0; j < numRandomEffects; j++) { |
| 216 |
Parameter effect = randomEffects.get(j); |
| 217 |
for (int i = 0; i < N; i++) { |
| 218 |
xBeta[i] += effect.getParameterValue(i); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return xBeta; |
| 223 |
} |
| 224 |
|
| 225 |
public double[] getXBeta(int j) { |
| 226 |
|
| 227 |
double[] xBeta = new double[N]; |
| 228 |
|
| 229 |
Parameter beta = independentParameter.get(j); |
| 230 |
Parameter delta = independentParameterDelta.get(j); |
| 231 |
DesignMatrix X = designMatrix.get(j); |
| 232 |
final int K = beta.getDimension(); |
| 233 |
for (int k = 0; k < K; k++) { |
| 234 |
double betaK = beta.getParameterValue(k); |
| 235 |
if (delta != null) { |
| 236 |
betaK *= delta.getParameterValue(k); |
| 237 |
} |
| 238 |
for (int i = 0; i < N; i++) { |
| 239 |
xBeta[i] += X.getParameterValue(i, k) * betaK; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if (numRandomEffects != 0) { |
| 244 |
throw new RuntimeException("Attempting to retrieve fixed effects without controlling for random effects"); |
| 245 |
} |
| 246 |
|
| 247 |
return xBeta; |
| 248 |
} |
| 249 |
|
| 250 |
public int getNumberOfFixedEffects() { |
| 251 |
return numIndependentVariables; |
| 252 |
} |
| 253 |
|
| 254 |
public int getNumberOfRandomEffects() { |
| 255 |
return numRandomEffects; |
| 256 |
} |
| 257 |
|
| 258 |
public Parameter getFixedEffect(int j) { |
| 259 |
return independentParameter.get(j); |
| 260 |
} |
| 261 |
|
| 262 |
public Parameter getRandomEffect(int j) { |
| 263 |
return randomEffects.get(j); |
| 264 |
} |
| 265 |
|
| 266 |
public Parameter getDependentVariable() { |
| 267 |
return dependentParameter; |
| 268 |
} |
| 269 |
|
| 270 |
public int getEffectNumber(Parameter effect) { |
| 271 |
return independentParameter.indexOf(effect); |
| 272 |
} |
| 273 |
|
| 274 |
public double[][] getX(int j) { |
| 275 |
return designMatrix.get(j).getParameterAsMatrix(); |
| 276 |
} |
| 277 |
|
| 278 |
private void calculateTransformedXBeta() { |
| 279 |
double[] xBeta = getXBeta(); |
| 280 |
|
| 281 |
for (int i = 0; i < N; i++) { |
| 282 |
transformedXBeta[i] = linkFunction.inverse(xBeta[i]); |
| 283 |
} |
| 284 |
|
| 285 |
transformedXBetaKnown = true; |
| 286 |
} |
| 287 |
|
| 288 |
private double calculateLogLikelihood() { |
| 289 |
if (!transformedXBetaKnown) { |
| 290 |
calculateTransformedXBeta(); |
| 291 |
} |
| 292 |
|
| 293 |
double logL = 0.0; |
| 294 |
|
| 295 |
if (isMultivariateDensity) { |
| 296 |
|
| 297 |
} else { |
| 298 |
for (int i = 0; i < Y.length; i++) { |
| 299 |
density.getLocationVariable().setValue(0, transformedXBeta[i]); |
| 300 |
logL += density.logPdf(new double[] { Y[i] }); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return logL; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public double getLogLikelihood() { |
| 316 |
if (!likelihoodKnown) { |
| 317 |
logLikelihood = calculateLogLikelihood(); |
| 318 |
} |
| 319 |
return logLikelihood; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@Override |
| 327 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 328 |
|
| 329 |
likelihoodKnown = false; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 334 |
if (variable == dependentParameter) { |
| 335 |
Y = dependentParameter.getParameterValues(); |
| 336 |
} |
| 337 |
|
| 338 |
transformedXBetaKnown = false; |
| 339 |
likelihoodKnown = false; |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
protected void storeState() { |
| 344 |
storedLogLikelihood = logLikelihood; |
| 345 |
System.arraycopy(transformedXBeta, 0, storedTransformedXBeta, 0, transformedXBeta.length); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
protected void restoreState() { |
| 350 |
logLikelihood = storedLogLikelihood; |
| 351 |
|
| 352 |
|
| 353 |
System.arraycopy(storedTransformedXBeta, 0, transformedXBeta, 0, transformedXBeta.length); |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
protected void acceptState() { |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public Model getModel() { |
| 363 |
return this; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public String toString() { |
| 368 |
return super.toString() + ": " + getLogLikelihood(); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public void makeDirty() { |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public LogColumn[] getColumns() { |
| 402 |
LogColumn[] output = new LogColumn[N]; |
| 403 |
for (int i = 0; i < N; i++) |
| 404 |
output[i] = new NumberArrayColumn(getId() + i, i); |
| 405 |
return output; |
| 406 |
} |
| 407 |
|
| 408 |
private class NumberArrayColumn extends NumberColumn { |
| 409 |
|
| 410 |
private final int index; |
| 411 |
|
| 412 |
public NumberArrayColumn(String label, int index) { |
| 413 |
super(label); |
| 414 |
this.index = index; |
| 415 |
} |
| 416 |
|
| 417 |
public double getDoubleValue() { |
| 418 |
return getXBeta()[index]; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public Element createElement(Document d) { |
| 427 |
throw new RuntimeException("Not implemented yet!"); |
| 428 |
} |
| 429 |
|
| 430 |
} |