samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.models;
|
samer@0
|
11 import samer.core.*;
|
samer@0
|
12 import samer.maths.*;
|
samer@0
|
13 import samer.core.types.*;
|
samer@0
|
14
|
samer@0
|
15 /**
|
samer@0
|
16 Similar to GaussianStats:
|
samer@0
|
17 Collect statistics about a vector: accumulates
|
samer@0
|
18 sums and sums of products, then computes mean
|
samer@0
|
19 and covariance.
|
samer@0
|
20
|
samer@0
|
21 This is NOT a model! Computing energies and probabilities
|
samer@0
|
22 directly from the first and second moments is horrible, since
|
samer@0
|
23 you need the inverse of the covariance matrix. You could,
|
samer@0
|
24 however, use this to train a Gaussian model.
|
samer@0
|
25
|
samer@0
|
26 BUT uses a moving average with a learning rate to track changes
|
samer@0
|
27 */
|
samer@0
|
28
|
samer@0
|
29 public class GaussianStatsOnline extends GaussianStats
|
samer@0
|
30 {
|
samer@0
|
31 VDouble rate1,rate2;
|
samer@0
|
32
|
samer@0
|
33 public GaussianStatsOnline(VVector vec) throws Exception
|
samer@0
|
34 {
|
samer@0
|
35 super(vec);
|
samer@0
|
36 rate1 = new VDouble("mean.rate",0.0001);
|
samer@0
|
37 rate2 = new VDouble("cov.rate",0.0001);
|
samer@0
|
38 sumx.addSaver();
|
samer@0
|
39 count=1; // should stay that way.
|
samer@0
|
40 }
|
samer@0
|
41
|
samer@0
|
42 public void reset() { super.reset(); count=1; }
|
samer@0
|
43 public void dispose() { rate2.dispose(); rate1.dispose(); super.dispose(); }
|
samer@0
|
44 public void run()
|
samer@0
|
45 {
|
samer@0
|
46 double r;
|
samer@0
|
47
|
samer@0
|
48 r=rate1.value;
|
samer@0
|
49 for (int i=0; i<n; i++) _sumx[i] += r*(x[i]-_sumx[i]);
|
samer@0
|
50
|
samer@0
|
51 r=rate2.value;
|
samer@0
|
52 for (int i=0; i<n; i++) {
|
samer@0
|
53 double [] a=_sumxx[i];
|
samer@0
|
54 double k=x[i];
|
samer@0
|
55 for (int j=0; j<=i; j++) {
|
samer@0
|
56 a[j] += r*(k*x[j]-a[j]);
|
samer@0
|
57 }
|
samer@0
|
58 }
|
samer@0
|
59 sumx.changed();
|
samer@0
|
60 sumxx.changed();
|
samer@0
|
61 }
|
samer@0
|
62 }
|
samer@0
|
63
|