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