Debian Codegraph @

src/dr/inference/model/LatentFactorModel.java

raw symbols SCIP
1 /*
2 * LatentFactorModel.java
3 *
4 * Copyright (c) 2002-2016 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.model;
27
28 import dr.inference.distribution.LatentFactorModelInterface;
29 import dr.math.matrixAlgebra.Matrix;
30 import dr.util.Citable;
31 import dr.util.Citation;
32 import dr.util.CommonCitations;
33
34 import java.util.*;
35
36
37 /**
38 * @author Max Tolkoff
39 * @author Marc Suchard
40 */
41
42 public class LatentFactorModel extends AbstractModelLikelihood implements Citable, LatentFactorModelInterface {
43 // private Matrix data;
44 // private Matrix factors;
45 // private Matrix loadings;
46
47 private final MatrixParameterInterface data;
48 private final MatrixParameterInterface factors;
49 private final MatrixParameterInterface loadings;
50 private MatrixParameterInterface sData;
51 private final DiagonalMatrix rowPrecision;
52 private final DiagonalMatrix colPrecision;
53 private final Parameter continuous;
54
55 private final boolean scaleData;
56
57 private final int dimFactors;
58 private final int dimData;
59 private final int nTaxa;
60
61 private boolean newModel;
62
63 private boolean likelihoodKnown = false;
64 private boolean isDataScaled = false;
65 private boolean storedLikelihoodKnown;
66 private boolean residualKnown = false;
67 private boolean dataKnown = false;
68 private boolean storedDataKnown;
69 private boolean LxFKnown = false;
70 private boolean storedResidualKnown = false;
71 private boolean storedLxFKnown;
72 private boolean traceKnown = false;
73 private boolean storedTraceKnown;
74 private boolean logDetColKnown = false;
75 private boolean storedLogDetColKnown;
76 private double trace;
77 private double storedTrace;
78 private double logLikelihood;
79 private double storedLogLikelihood;
80 private double logDetCol;
81 private double storedLogDetCol;
82 private boolean[][] changed;
83 private boolean[][] storedChanged;
84 private boolean RecomputeResiduals;
85 private boolean RecomputeFactors;
86 private boolean RecomputeLoadings;
87 private Vector<Integer> changedValues;
88 private Vector<Integer> storedChangedValues;
89 private boolean factorsKnown = false;
90 private boolean storedFactorsKnown = false;
91 private boolean loadingsKnown = false;
92 private boolean storedLoadingsKnown = false;
93 private boolean totalRecompute = true;
94 private boolean storedTotalRecompute = false;
95
96 private double[] residual;
97 private double[] LxF;
98 private double[] storedResidual;
99 private double[] storedLxF;
100
101 private double pathParameter = 1.0;
102
103 private final Parameter missingIndicator;
104 private final int[] rowCount;
105 private final int nmeasurements;
106
107 public LatentFactorModel(MatrixParameterInterface data, MatrixParameterInterface factors, MatrixParameterInterface loadings,
108 DiagonalMatrix rowPrecision, DiagonalMatrix colPrecision,
109 Parameter missingIndicator,
110 boolean scaleData, Parameter continuous, boolean newModel, boolean recomputeResiduals,
111 boolean recomputeFactors, boolean recomputeLoadings
112 ) {
113 super("");
114 this.RecomputeResiduals = recomputeResiduals;
115 this.RecomputeFactors = recomputeFactors;
116 this.RecomputeLoadings = recomputeLoadings;
117 changedValues = new Vector<Integer>();
118 for (int i = 0; i < data.getDimension(); i++) {
119 changedValues.add(i);
120 }
121 storedChangedValues = new Vector<Integer>();
122 // data = new Matrix(dataIn.getParameterAsMatrix());
123 // factors = new Matrix(factorsIn.getParameterAsMatrix());
124 // loadings = new Matrix(loadingsIn.getParameterAsMatrix());
125 this.newModel = newModel;
126 this.scaleData = scaleData;
127 this.data = data;
128 this.factors = factors;
129 // Put default bounds on factors
130 if(factors instanceof MatrixParameter){
131 for (int i = 0; i < factors.getColumnDimension(); ++i) {
132 Parameter p = factors.getParameter(i);
133 System.err.println(p.getId() + " " + p.getDimension());
134 p.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, p.getDimension()));
135 }
136 }
137 this.continuous=continuous;
138
139 this.loadings = loadings;
140
141 this.missingIndicator = missingIndicator;
142 rowCount = new int[colPrecision.getRowDimension()];
143 if(missingIndicator != null){
144 for (int i = 0; i < data.getRowDimension(); i++) {
145 for (int j = 0; j < data.getColumnDimension(); j++) {
146 if(missingIndicator.getParameterValue(j * data.getRowDimension() + i) == 0){
147 rowCount[i]++;
148 }
149 }
150
151 }
152 }
153 else{
154 for (int i = 0; i < data.getRowDimension(); i++) {
155 rowCount[i] = data.getColumnDimension();
156 }
157
158 }
159 int nmeasurements = 0;
160 for (int i = 0; i < rowCount.length ; i++) {
161 nmeasurements += rowCount[i];
162 }
163 this.nmeasurements = nmeasurements;
164
165 // storedData=new MatrixParameter(null);
166 // for (int i = 0; i <continuous.getDimension(); i++) {
167 // if(continuous.getParameterValue(i)==0)
168 // storedData.addParameter(new Parameter.Default(data.getColumnDimension()));
169 //
170 // }
171
172 // Put default bounds on loadings
173 // loadings.addBounds();
174
175
176 changed = new boolean[loadings.getRowDimension()][factors.getColumnDimension()];
177 storedChanged = new boolean[loadings.getRowDimension()][factors.getColumnDimension()];
178
179 for (int i = 0; i < loadings.getRowDimension(); i++) {
180 for (int j = 0; j < factors.getColumnDimension(); j++) {
181 changed[i][j] = true;
182 }
183 }
184
185 this.rowPrecision = rowPrecision;
186 this.colPrecision = colPrecision;
187
188 addVariable(data);
189 addVariable(factors);
190 addVariable(loadings);
191 addVariable(rowPrecision);
192 addVariable(colPrecision);
193
194 dimFactors = factors.getRowDimension();
195 dimData = loadings.getRowDimension();
196 // nTaxa = factors.getParameterCount();
197 // nTaxa = factors.getParameter(0).getDimension();
198 nTaxa = factors.getColumnDimension();
199
200 // System.out.print(nTaxa);
201 // System.out.print("\n");
202 // System.out.print(dimData);
203 // System.out.print("\n");
204 // System.out.println(dimFactors);
205 // System.out.println(data.getDimension());
206 // System.out.println(data.getRowDimension());
207 // System.out.println(data.getColumnDimension());
208
209 // System.out.println(new Matrix(data.getParameterAsMatrix()));
210 // System.out.println(new Matrix(factors.getParameterAsMatrix()));
211
212
213 if (nTaxa != data.getColumnDimension()) {
214 throw new RuntimeException("DATA COLUMNS MUST HAVE THE SAME DIMENSION AS FACTOR COLUMNS\n");
215 // System.exit(10);
216 }
217 if (dimData != data.getRowDimension()) {
218 System.out.println(dimData);
219 System.out.println(data.getRowDimension());
220 System.out.println(loadings.getRowDimension());
221 throw new RuntimeException("DATA ROWS MUST HAVE THE SAME DIMENSION AS LOADINGS ROWS\n");
222 // System.exit(10);
223 }
224 if (factors.getRowDimension() != loadings.getColumnDimension()) {
225 System.out.println(getModelName());
226 throw new RuntimeException("LOADINGS AND FACTORS MUST HAVE THE SAME NUMBER OF FACTORS\n");
227 // System.exit(10);
228 }
229 if (dimData < dimFactors) {
230 throw new RuntimeException("MUST HAVE FEWER FACTORS THAN DATA POINTS\n");
231 }
232
233 residual = new double[loadings.getRowDimension() * factors.getColumnDimension()];
234 LxF = new double[loadings.getRowDimension() * factors.getColumnDimension()];
235 storedResidual = new double[residual.length];
236 storedLxF = new double[LxF.length];
237
238 if (!isDataScaled & !scaleData) {
239 sData = this.data;
240 isDataScaled = true;
241 }
242 if (!isDataScaled) {
243 sData = computeScaledData();
244 isDataScaled = true;
245 for (int i = 0; i < sData.getRowDimension(); i++) {
246 for (int j = 0; j < sData.getColumnDimension(); j++) {
247 this.data.setParameterValue(i, j, sData.getParameterValue(i, j));
248 // System.out.println(this.data.getParameterValue(i,j));
249 }
250
251 }
252 data.fireParameterChangedEvent();
253 }
254
255 double sum = 0;
256 for (int i = 0; i < sData.getRowDimension(); i++) {
257 for (int j = 0; j < sData.getColumnDimension(); j++) {
258 if (continuous.getParameterValue(i) == 1 && sData.getParameterValue(i, j) != 0) {
259 sum += -.5 * Math.log(2 * StrictMath.PI) - .5 * sData.getParameterValue(i, j) * sData.getParameterValue(i, j);
260 }
261 }
262 }
263 System.out.println("Constant Value for Path Sampling (normal 0,1): " + -1 * sum);
264
265 computeResiduals();
266 // System.out.print(new Matrix(residual.toComponents()));
267 // System.out.print(calculateLogLikelihood());
268 }
269
270
271 // public Matrix getData(){
272 // Matrix ans=data;
273 // return ans;
274 // }
275 //
276 // public Matrix getFactors(){
277 // Matrix ans=factors;
278 // return ans;
279 // }
280 //
281 // public Matrix getLoadings(){
282 // Matrix ans=loadings;
283 // return ans;
284 // }
285 //
286 // public Matrix getResidual(){
287 // Matrix ans=residual;
288 // return ans;
289 // }
290 public MatrixParameterInterface getFactors(){
291 return factors;
292 }
293
294 public MatrixParameter getColumnPrecision() {
295 return colPrecision;
296 }
297
298 public MatrixParameterInterface getLoadings(){
299 return loadings;
300 }
301
302 public MatrixParameterInterface getData(){
303 return data;
304 }
305
306 public Parameter returnIntermediate() {
307 if (!residualKnown && checkLoadings()) {
308 computeResiduals();
309 }
310 return data;
311 }
312
313 // public Parameter returnIntermediate(int PID)
314 // { //residualKnown=false;
315 // if(!residualKnown && checkLoadings()){
316 // computeResiduals();
317 // }
318 // return data.getParameter(PID);
319 // }
320
321
322 public MatrixParameterInterface getScaledData(){
323 return data;
324 }
325
326 public Parameter getContinuous() {
327 return continuous;
328 }
329
330 public int getFactorDimension() {
331 return factors.getRowDimension();
332 }
333
334 public double[] getResidual() {
335 computeResiduals();
336 return residual;
337 }
338
339 public Parameter getMissingIndicator() {
340 return missingIndicator;
341 }
342
343 public int getRowCount(int i){
344 return rowCount[i];
345 }
346
347 private void Multiply(MatrixParameterInterface Left, MatrixParameterInterface Right, double[] answer){
348 int dim = Left.getColumnDimension();
349 int n = Left.getRowDimension();
350 int p = Right.getColumnDimension();
351
352 if(((factorsKnown == false && !RecomputeFactors) || (!dataKnown && !RecomputeResiduals) || (!loadingsKnown && !RecomputeLoadings)) && !totalRecompute){
353 double sum;
354 ListIterator<Integer> li = changedValues.listIterator();
355 while (li.hasNext()) {
356 int index = li.next();
357 int i = index % n;
358 int j = index / n;
359 if(missingIndicator == null || missingIndicator.getParameterValue(j * n + i) != 1){
360 sum = 0;
361 for (int k = 0; k < dim; k++) {
362 // System.out.println(data.getColumnDimension());
363 // System.out.println(index);
364 sum += Left.getParameterValue(i, k) *
365 Right.getParameterValue(k, j);
366 }
367 answer[j * n + i] = sum;
368 }
369 }
370 } else {
371 for (int i = 0; i < answer.length; i++) {
372 answer[i] = 0;
373 }
374 for (int i = 0; i < n; i++) {
375 for (int k = 0; k < dim; k++){
376 if (Left.getParameterValue(i, k) != 0){
377 for (int j = 0; j < p; j++) {
378 if(missingIndicator == null || missingIndicator.getParameterValue(j * n + i) != 1){
379 if ((changed[i][j] == true && continuous.getParameterValue(i) != 0) || newModel) {
380 double sum = 0;
381
382 sum += Left.getParameterValue(i, k) * Right.getParameterValue(k, j);
383 answer[j * n + i] += sum;
384 //changed[i][j]=false;
385 }
386 }
387 }
388 }
389 }
390 }
391 }
392 }
393
394 private void add(MatrixParameter Left, MatrixParameter Right, double[] answer) {
395 int row = Left.getRowDimension();
396 int col = Left.getColumnDimension();
397 for (int i = 0; i < row; i++) {
398 for (int j = 0; j < col; j++) {
399 answer[i * col + j] = Left.getParameterValue(i, j) + Right.getParameterValue(i, j);
400 }
401
402 }
403 }
404
405 private void subtract(MatrixParameterInterface Left, double[] Right, double[] answer) {
406 int row = Left.getRowDimension();
407 int col = Left.getColumnDimension();
408 if(((!RecomputeResiduals && !dataKnown) || (!RecomputeFactors && !factorsKnown) || (!RecomputeLoadings && !loadingsKnown)) && !totalRecompute) {
409 int size = changedValues.size();
410 for (int i = 0; i < size; i++) {
411 int id = changedValues.get(i);
412 int tcol=id / row;
413 int trow=id % row;
414 // System.out.println(Left.getParameterValue(id)==Left.getParameterValue(tcol,trow));
415 if(missingIndicator == null || missingIndicator.getParameterValue(tcol * row + trow) != 1)
416 answer[tcol * row + trow] = Left.getParameterValue(id) - Right[tcol * row + trow];
417 }
418 } else {
419 for (int i = 0; i < row; i++) {
420 if (continuous.getParameterValue(i) != 0 || newModel) {
421 for (int j = 0; j < col; j++) {
422 if(missingIndicator == null || missingIndicator.getParameterValue(j * row + i) != 1)
423 answer[j * row + i] = Left.getParameterValue(i, j) - Right[j * row + i];
424 }
425 }
426 // else{
427 // for (int j = 0; j <col; j++) {
428 // Left.setParameterValueQuietly(i,j, Right[i*col+j]);
429 // }
430 // containsDiscrete=true;
431 // }
432
433 }
434 }
435 changedValues.clear();
436 // if(containsDiscrete){
437 // Left.fireParameterChangedEvent();}
438 }
439
440 private double TDTTrace(double[] array, DiagonalMatrix middle) {
441 int innerDim = middle.getRowDimension();
442 int outerDim = array.length / innerDim;
443 double sum = 0;
444 for (int j = 0; j < innerDim; j++) {
445 if (continuous.getParameterValue(j) != 0 || newModel) {
446 for (int i = 0; i < outerDim; i++) {
447 if(missingIndicator == null || missingIndicator.getParameterValue(i * innerDim + j) != 1){
448 double s1 = array[i * innerDim + j];
449 double s2 = middle.getParameterValue(j, j);
450 sum += s1 * s1 * s2;
451 }
452 }
453 }
454 }
455 return sum;
456 }
457
458
459 private MatrixParameter computeScaledData() {
460 MatrixParameter answer = new MatrixParameter(data.getParameterName() + ".scaled");
461 answer.setDimensions(data.getRowDimension(), data.getColumnDimension());
462 // Matrix answer=new Matrix(data.getRowDimension(), data.getColumnDimension());
463 double[][] aData = data.getParameterAsMatrix();
464 double[] meanList = new double[data.getRowDimension()];
465 double[] varList = new double[data.getRowDimension()];
466 double[] count = new double[data.getRowDimension()];
467 for (int i = 0; i < data.getColumnDimension(); i++) {
468 for (int j = 0; j < data.getRowDimension(); j++) {
469 if (data.getParameterValue(j, i) != 0) {
470 meanList[j] += data.getParameterValue(j, i);
471 count[j]++;
472 }
473 }
474 }
475 for (int i = 0; i < data.getRowDimension(); i++) {
476 if (continuous.getParameterValue(i) == 1)
477 meanList[i] = meanList[i] / count[i];
478 else
479 meanList[i] = 0;
480 }
481
482 double[][] answerTemp = new double[data.getRowDimension()][data.getColumnDimension()];
483 for (int i = 0; i < data.getColumnDimension(); i++) {
484 for (int j = 0; j < data.getRowDimension(); j++) {
485 if (aData[j][i] != 0) {
486 answerTemp[j][i] = aData[j][i] - meanList[j];
487 }
488 }
489 }
490 // System.out.println(new Matrix(answerTemp));
491
492 for (int i = 0; i < data.getColumnDimension(); i++) {
493 for (int j = 0; j < data.getRowDimension(); j++) {
494 varList[j] += answerTemp[j][i] * answerTemp[j][i];
495 }
496 }
497
498 for (int i = 0; i < data.getRowDimension(); i++) {
499 if (continuous.getParameterValue(i) == 1) {
500 varList[i] = varList[i] / (count[i] - 1);
501 varList[i] = StrictMath.sqrt(varList[i]);
502 } else {
503 varList[i] = 1;
504 }
505 }
506 // System.out.println(data.getColumnDimension());
507 // System.out.println(data.getRowDimension());
508
509 for (int i = 0; i < data.getColumnDimension(); i++) {
510 for (int j = 0; j < data.getRowDimension(); j++) {
511 answer.setParameterValue(j, i, answerTemp[j][i] / varList[j]);
512 }
513 }
514 // System.out.println(new Matrix(answerTemp));
515 // computeResiduals();
516 return answer;
517 }
518
519 private Matrix copy(CompoundParameter parameter, int dimMajor, int dimMinor) {
520 return new Matrix(parameter.getParameterValues(), dimMajor, dimMinor);
521 }
522
523 private void computeResiduals() {
524 // LxFKnown=false;
525
526
527 // if(firstTime || (!factorVariablesChanged.empty() && !loadingVariablesChanged.empty())){
528 if (!LxFKnown) {
529 Multiply(loadings, factors, LxF);
530
531 }
532 subtract(data, LxF, residual);
533 LxFKnown = true;
534 residualKnown = true;
535 factorsKnown = true;
536 loadingsKnown = true;
537 dataKnown = true;
538 totalRecompute = false;
539 // firstTime=false;}
540 // else{
541 // while(!factorVariablesChanged.empty()){
542 //
543 // }
544 // while(!loadingVariablesChanged.empty()){
545 //
546 // }
547 // }
548 //
549
550
551 }
552
553
554 @Override
555 protected void handleModelChangedEvent(Model model, Object object, int index) {
556 // Do nothing
557 }
558
559 /**
560 * Additional state information, outside of the sub-model is stored by this call.
561 */
562 @Override
563 protected void storeState() {
564 // data.storeParameterValues();
565 // loadings.storeValues();
566 // factors.storeValues();
567 storedLogLikelihood = logLikelihood;
568 storedLikelihoodKnown = likelihoodKnown;
569 storedLogDetColKnown = logDetColKnown;
570 storedLogDetCol = logDetCol;
571 storedTrace = trace;
572 storedTraceKnown = traceKnown;
573 storedResidualKnown = residualKnown;
574 storedLxFKnown = LxFKnown;
575 storedFactorsKnown = factorsKnown;
576 storedLoadingsKnown = loadingsKnown;
577 storedDataKnown = dataKnown;
578 storedTotalRecompute = totalRecompute;
579 System.arraycopy(residual, 0, storedResidual, 0, residual.length);
580
581 System.arraycopy(LxF, 0, storedLxF, 0, residual.length);
582 System.arraycopy(changed, 0, storedChanged, 0, changed.length);
583 // for (int i = 0; i <changedValues.size() ; i++) {
584 // storedChangedValues.addElement(changedValues.elementAt(i)); ;
585 // }
586 storedChangedValues = (Vector<Integer>) changedValues.clone();
587
588 }
589
590 /**
591 * After this call the model is guaranteed to have returned its extra state information to
592 * the values coinciding with the last storeState call.
593 * Sub-models are handled automatically and do not need to be considered in this method.
594 */
595 @Override
596 protected void restoreState() {
597 changed = storedChanged;
598 // data.restoreParameterValues();
599 // loadings.restoreValues();
600 // factors.restoreValues();
601 logLikelihood = storedLogLikelihood;
602 likelihoodKnown = storedLikelihoodKnown;
603 trace = storedTrace;
604 traceKnown = storedTraceKnown;
605 residualKnown = storedResidualKnown;
606 LxFKnown = storedLxFKnown;
607 double[] temp = residual;
608 residual = storedResidual;
609 storedResidual = temp;
610 temp = LxF;
611 LxF = storedLxF;
612 storedLxF = temp;
613 logDetCol = storedLogDetCol;
614 logDetColKnown = storedLogDetColKnown;
615 factorsKnown = storedFactorsKnown;
616 loadingsKnown = storedLoadingsKnown;
617 dataKnown = storedDataKnown;
618 totalRecompute = storedTotalRecompute;
619 changedValues = storedChangedValues;
620 // changedValues=storedChangedValues;
621 // storedChangedValues=new Vector<Integer>();
622
623 // System.out.println(data.getParameterValue(10, 19));
624
625
626 // int index=0;
627 // for (int i = 0; i <continuous.getDimension() ; i++) {
628 // if(continuous.getParameterValue(i)==0){
629 // for (int j = 0; j <data.getParameter(i).getDimension() ; j++) {
630 // data.getParameter(i).setParameterValueQuietly(j, storedData.getParameter(index).getParameterValue(j));
631 // }
632 // index++;
633 // }
634 // }
635 }
636
637 /**
638 * This call specifies that the current state is accept. Most models will not need to do anything.
639 * Sub-models are handled automatically and do not need to be considered in this method.
640 */
641 @Override
642 protected void acceptState() {
643 // Do nothing
644 }
645
646 /**
647 * This method is called whenever a parameter is changed.
648 * <p/>
649 * It is strongly recommended that the model component sets a "dirty" flag and does no
650 * further calculations. Recalculation is typically done when the model component is asked for
651 * some information that requires them. This mechanism is 'lazy' so that this method
652 * can be safely called multiple times with minimal computational cost.
653 */
654 @Override
655 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
656 if (variable == getScaledData()) {
657 residualKnown = false;
658 traceKnown = false;
659 likelihoodKnown = false;
660 if (!RecomputeResiduals) {
661 if (index != -1 && !changedValues.contains(index))
662 changedValues.add(index);
663 else{
664 totalRecompute = true;
665 changedValues.clear();
666 }
667 dataKnown = false;
668 }
669 }
670 if (variable == factors) {
671
672
673 // for (int i = 0; i <loadings.getRowDimension() ; i++) {
674 // changed[i][index/factors.getRowDimension()]=true;
675 // }
676
677 if (!RecomputeFactors) {
678 // System.out.println("index");
679 // System.out.println(index);
680 factorsKnown = false;
681 int row = index / factors.getRowDimension();
682 if (index != -1)
683 for (int i = 0; i < data.getRowDimension(); i++) {
684 if(!changedValues.contains(row * data.getRowDimension() + i))
685 changedValues.add(row * data.getRowDimension() + i);
686 }
687 else{
688 totalRecompute = true;
689 changedValues.clear();
690 }
691
692 }
693
694
695 // factorVariablesChanged.push(index);
696
697
698 LxFKnown = false;
699 residualKnown = false;
700 traceKnown = false;
701 likelihoodKnown = false;
702 }
703 if (variable == loadings) {
704 if (!RecomputeLoadings) {
705 loadingsKnown = false;
706 int col = index % loadings.getRowDimension();
707 if (index != -1) {
708 for (int i = 0; i < data.getColumnDimension(); i++) {
709 if(!changedValues.contains(i * data.getRowDimension() + col))
710 changedValues.add(i * data.getRowDimension() + col);
711 }
712 }
713 else{totalRecompute = true;
714 changedValues.clear();}
715 }
716 // System.out.println("Loadings Changed");
717 // System.out.println(index);
718 // System.out.println(index/loadings.getRowDimension());
719
720
721 // for (int i = 0; i <factors.getColumnDimension(); i++) {
722 // changed[index%loadings.getRowDimension()][i]=true;
723 // }
724
725
726 // factorVariablesChanged.push(index);
727
728
729 LxFKnown = false;
730 residualKnown = false;
731 traceKnown = false;
732 likelihoodKnown = false;
733 }
734 if (variable == colPrecision) {
735 logDetColKnown = false;
736 traceKnown = false;
737 likelihoodKnown = false;
738 }
739
740 }
741
742 @Override
743 public Citation.Category getCategory() {
744 return Citation.Category.TRAIT_MODELS;
745 }
746
747 @Override
748 public String getDescription() {
749 return "Latent factor model";
750 }
751
752 @Override
753 public List<Citation> getCitations() {
754 return Collections.singletonList(CommonCitations.CYBIS_2015_ASSESSING);
755 }
756
757 /**
758 * Get the model.
759 *
760 * @return the model.
761 */
762 @Override
763 public Model getModel() {
764 return this;
765 }
766
767 /**
768 * Get the log likelihood.
769 *
770 * @return the log likelihood.
771 */
772 @Override
773 public double getLogLikelihood() {
774 likelihoodKnown = false;
775 if (!likelihoodKnown) {
776 logLikelihood = calculateLogLikelihood();
777 likelihoodKnown = true;
778 }
779 return logLikelihood;
780 }
781
782 /**
783 * Forces a complete recalculation of the likelihood next time getLikelihood is called
784 */
785 @Override
786 public void makeDirty() {
787 likelihoodKnown = false;
788
789 factorsKnown = false;
790 loadingsKnown = false;
791 residualKnown = false;
792 totalRecompute = true;
793 changedValues.clear();
794 LxFKnown = false;
795 logDetColKnown = false;
796 traceKnown = false;
797 dataKnown = false;
798
799 }
800
801 private boolean checkLoadings() {
802 for (int i = 0; i < StrictMath.min(loadings.getRowDimension(), loadings.getColumnDimension()); i++) {
803 if (loadings.getParameterValue(i, i) < 0) {
804 return false;
805 }
806 }
807 return true;
808 }
809
810 private double calculateLogLikelihood() {
811 // if(!checkLoadings()){
812 // if(pathParameter==1)
813 // return Double.NEGATIVE_INFINITY;
814 // else{
815 // return Math.log(1-pathParameter);}}
816
817 // Matrix tRowPrecision= new Matrix(rowPrecision.getParameterAsMatrix());
818 // Matrix tColPrecision= new Matrix(colPrecision.getParameterAsMatrix());
819
820
821 // residualKnown=false;
822 if (!residualKnown) {
823 computeResiduals();
824 }
825
826
827 // expPart = residual.productInPlace(rowPrecision.productInPlace(residual.transposeThenProductInPlace(colPrecision, TResidualxC), RxTRxC), expPart);
828 // logDetRow=StrictMath.log(rowPrecision.getDeterminant());
829 // logDetColKnown=false;
830 if (!logDetColKnown) {
831 logDetColKnown = true;
832 logDetCol = 0;
833 for (int i = 0; i < colPrecision.getRowDimension(); i++) {
834 if (continuous.getParameterValue(i) != 0)
835 logDetCol += Math.log(colPrecision.getParameterValue(i, i)) * rowCount[i];
836 }
837 }
838 // System.out.println(logDetCol);
839 // System.out.println(logDetRow);
840 // traceKnown=false;
841 if (!traceKnown) {
842 traceKnown = true;
843 trace = TDTTrace(residual, colPrecision);
844 }
845 // if(expPart.getRowDimension()!=expPart.getColumnDimension())
846 // {
847 // System.err.print("Matrices are not conformable");
848 // System.exit(0);
849 // }
850
851 // else{
852 // for(int i=0; i<expPart.getRowDimension(); i++){
853 // trace+=expPart.getParameterValue(i, i);
854 // }
855 // }
856 // System.out.println(expPart);
857
858
859 return -.5 * trace + .5 * logDetCol //+ .5 * data.getRowDimension()
860
861 - .5 * nmeasurements * Math.log(2.0 * StrictMath.PI);
862 }
863
864 // public void setPathParameter(double beta){
865 // pathParameter=beta;
866 // data.product(pathParameter);
867 // }
868
869 // @Override
870 // public double getLikelihoodCorrection() {
871 // return 0;
872 // }
873
874 public double[] getLxF(){
875 if(!LxFKnown)
876 computeResiduals();
877 return LxF;
878 }
879 }