Statistiques
| Révision :

root / tmp / org.txm.analec.rcp / src / JamaPlus / SparseAFC.java @ 2033

Historique | Voir | Annoter | Télécharger (2,27 ko)

1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5
package JamaPlus;
6

    
7
import JamaPlus.SparseSVD.SparseSVDException;
8

    
9
/**
10
 /**
11
 *
12
 * SparseAFC analyse factorielle des correspondances (matrices creuses)
13
 * mat :  matrice binaire (0. ou 1.) profil des lignes (un individu par ligne, un caractère par colonne)
14
 * CP: composantes principales,
15
 * VP: valeurs d'inertie, 
16
 * CG: centres de gravité des CP contenant un même caractère (non calculé dans cette version)
17
 * [p,q] = size(mat); p:nb de lignes, q:nb de col
18
 * 
19
 * @author Bernard
20
 */
21
public class SparseAFC {
22
  private double[][] CP;
23
  private double[] VP;
24
  private int p, q, n;
25

    
26
  SparseAFC(SparseMatrix Arg) throws SparseSVDException {
27
    SparseMatrix mat = Arg.copy();
28
    mat.nulColumnSuppression();
29
    p = mat.getRowDimension();
30
    q = mat.getColumnDimension();
31
    n = Math.min(Math.min(p, q), 5); 
32
    // n : nombre de composantes principales espérées (pas plus de 5)
33
    if (n == 0) return;
34
    if (n == 1) {
35
      CP = new double[p][1];
36
      VP = new double[1];
37
      return;
38
    }
39
    double f = mat.elementSum();
40
    if (f == 0) return;
41
    mat = mat.timesEquals(1 / f);
42
    Matrix r = mat.lineSum();
43
    Matrix r1 = r.arrayInverse();
44
    Matrix r2 = r.arraySqrt();
45
    Matrix r3 = r2.arrayInverse();
46
    Matrix c = mat.columnSum();
47
    Matrix c1 = c.arrayInverse();
48
    Matrix c2 = c.arraySqrt();
49
    Matrix c3 = c2.arrayInverse();
50
    mat = mat.firstColumnTimesEqual(r3);
51
    mat = mat.firstLineTimesEqual(c3);
52
    SparseSVD svd = mat.svd(n);
53
    Matrix cp = svd.getU();
54
    Matrix vp = new Matrix(svd.getSingularValues(), 1);
55
    n = vp.getColumnDimension();   
56
    // peut-être moins de composantes principales effectivement calculées
57
     if (n == 0) return;
58
    if (n == 1) {
59
      CP = new double[p][1];
60
      VP = new double[1];
61
      return;
62
    }
63
    cp = cp.getMatrix(0, p - 1, 1, n - 1);
64
    cp = cp.firstColumnTimesEqual(r2);
65
    vp = vp.getMatrix(0, 0, 1, n - 1);
66
    cp = cp.firstLineTimesEqual(vp);
67
    cp.fixColumnSigns();
68
    cp = cp.firstColumnTimesEqual(r1);
69
    CP = cp.getArray();
70
    VP = vp.getColumnPackedCopy();
71
  }
72

    
73
  public Matrix getCP() {
74
    if (CP == null) return null;
75
    return new Matrix(CP, p, n - 1);
76
  }
77

    
78
  public double[] getVP() {
79
    return VP;
80
  }
81
}
82