4 void ml_predict_noNA(double* X
, double* Y
, int* n_
, int* K_
, double* alpha_
, int* grad_
, double* weight
)
8 double alpha
= *alpha_
;
11 //at least two experts to combine: various inits
12 double initWeight
= 1. / K
;
13 for (int i
=0; i
<K
; i
++)
14 weight
[i
] = initWeight
;
15 double* error
= (double*)malloc(K
*sizeof(double));
16 double* cumDeltaError
= (double*)calloc(K
, sizeof(double));
17 double* regret
= (double*)calloc(K
, sizeof(double));
20 for (int t
=0; t
<n
; t
++ < n
)
25 for (int i
=0; i
<K
; i
++)
26 hatY
+= X
[t
*K
+i
] * weight
[i
];
27 for (int i
=0; i
<K
; i
++)
28 error
[i
] = 2. * (hatY
- Y
[t
]) * X
[t
*K
+i
];
32 for (int i
=0; i
<K
; i
++)
34 double delta
= X
[t
*K
+i
] - Y
[t
];
35 error
[i
] = delta
* delta
;
40 for (int i
=0; i
<K
; i
++)
41 hatError
+= error
[i
] * weight
[i
];
42 for (int i
=0; i
<K
; i
++)
44 double deltaError
= hatError
- error
[i
];
45 cumDeltaError
[i
] += deltaError
* deltaError
;
46 regret
[i
] += deltaError
;
47 double eta
= 1. / (1. + cumDeltaError
[i
]);
48 weight
[i
] = regret
[i
] > 0. ? eta
* regret
[i
] : 0.;
51 double sumWeight
= 0.0;
52 for (int i
=0; i
<K
; i
++)
53 sumWeight
+= weight
[i
];
54 for (int i
=0; i
<K
; i
++)
55 weight
[i
] /= sumWeight
;
56 //redistribute weights if alpha > 0 (all weights are 0 or more, sum > 0)
57 for (int i
=0; i
<K
; i
++)
58 weight
[i
] = (1. - alpha
) * weight
[i
] + alpha
/K
;