-
Notifications
You must be signed in to change notification settings - Fork 0
/
mix2.c
260 lines (234 loc) · 6.35 KB
/
mix2.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
########################################################################
# Netflix Prize Tools
# Copyright (C) 2007-8 Ehud Ben-Reuven
# udi@benreuven.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
########################################################################
*/
#define LAMBDA (20.)
#define HOLDOUT
/*
mix scr.bin files generated with the "-se" flag in utestX
we want to find mixture coefficents Bj
Ri = sumj Rij Bj
that minimize the L2 norm of the error
sumi Ri^2
on the training data under the constrain that sumj Bj=1
sumi Ri^2 + L sumj Bj
difreniating gives
sumi 2 Rij (sumj Rij Bj) + L =0
or replacing L with l=-L/2:
A B = l where Ajk= sumi Rji Rki
since this is a linear equation we can replace l with 1 and normalize B latter.
these equations are solved using LAPACK library.
You need to use Cygwin's setup tool and download the lapack package from the
Math category.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "basic.h"
#include "netflix.h"
#include "utest.h"
#define NSCORES (5)
void openfiles(FILE *fp[],char *fnames[], int nscores)
{
int f;
for(f=0;f<nscores;f++) {
lg("Mixing %s\n",fnames[f]);
if(NULL==(fp[f]=fopen(fnames[f],"rb")))
error("Cant open");
}
}
void seekfiles(FILE *fp[], int nscores, int d)
{
if(!d) return;
int f;
for(f=0;f<nscores;f++)
if(fseek(fp[f],d*sizeof(float),SEEK_CUR))
error("Failed to seek\n");
}
void readfiles(FILE *fp[], float *s, int nscores)
{
int f;
for(f=0;f<nscores;f++)
if(1!=fread(&s[f],sizeof(float),1,fp[f])) {
lg("Error\n");
exit(1);
}
}
void closefiles(FILE *fp[], int nscores)
{
int f;
for(f=0;f<nscores;f++)
fclose(fp[f]);
}
computemix(char *fnames[], int nscores, double *xty)
{
#ifdef HOLDOUT
lg("With holdout\n");
#endif
int ns2=nscores+2;
int ns1=nscores+1;
FILE *fp[NSCORES];
openfiles(fp,fnames,nscores);
double xtx[NSCORES+2][NSCORES+2];
ZERO(xtx);
int u;
for(u=0; u<NUSERS; u++) {
PROGRESS(u,NUSERS);
int base=useridx[u][0];
#ifdef HOLDOUT
if(aopt) error("cant do holdout with -a");
int d0=UNTRAIN(u);
int d1=UNALL(u)-d0;
#else
int d0=0;
int d1=UNTRAIN(u);
#endif
seekfiles(fp,nscores, d0);
base+=d0;
int j;
for(j=0;j<d1;j++) {
unsigned int dd=userent[base+j];
int r = (dd>>USER_LMOVIEMASK)&7;
float s[NSCORES+2];
readfiles(fp,s,nscores);
int f;
for(f=0;f<nscores;f++)
s[f]=r-s[f];
s[nscores]=1.;
s[nscores+1]=r;
int ff;
for(f=0;f<ns2;f++) {
for(ff=0;ff<ns2;ff++)
xtx[f][ff] +=s[f]*s[ff];
}
}
int d2=UNTOTAL(u)-(d1+d0);
seekfiles(fp,nscores, d2);
}
closefiles(fp,nscores);
int count=xtx[nscores][nscores];
int j1,j2;
for(j1=0;j1<nscores;j1++)
lg("File %d RMSE %f\n",j1,sqrt((xtx[j1][j1]+xtx[ns1][ns1]-2*xtx[ns1][j1])/count));
double avgs[NSCORES+2],std[NSCORES+2];
for(j1=0;j1<ns2;j1++) {
avgs[j1]=xtx[nscores][j1]/count;
std[j1]=sqrt(xtx[j1][j1]/count-avgs[j1]*avgs[j1]);
}
for(j1=0;j1<ns2;j1++)
lg("%f\t",avgs[j1]);
lg("\n");
for(j1=0;j1<ns2;j1++)
lg("%f\t",std[j1]);
lg("\n");
lg("-------------------------------------------------\n");
for(j1=0;j1<ns2;j1++) {
for(j2=0;j2<ns2;j2++) {
lg("%f\t",(xtx[j1][j2]/count-avgs[j1]*avgs[j2])/(std[j1]*std[j2]+1.e-6));
}
lg("\n");
}
lg("-------------------------------------------------\n");
double eavgs[NSCORES],estd[NSCORES];
for(j1=0;j1<nscores;j1++) {
eavgs[j1]=avgs[ns1]-avgs[j1];
estd[j1]=sqrt((xtx[ns1][ns1]+ xtx[j1][j1]-2*xtx[j1][ns1])/count);
}
for(j1=0;j1<nscores;j1++)
lg("%f\t",eavgs[j1]);
lg("\n");
for(j1=0;j1<nscores;j1++)
lg("%f\t",estd[j1]);
lg("\n");
lg("-------------------------------------------------\n");
for(j1=0;j1<nscores;j1++) {
for(j2=0;j2<nscores;j2++) {
lg("%f\t",((xtx[j1][j2]+xtx[ns1][ns1]-xtx[ns1][j1]-xtx[ns1][j2])/count-eavgs[j1]*eavgs[j2])/(estd[j1]*estd[j2]+1.e-6));
}
lg("\n");
}
char TRANS='N';
char UFLO='U';
int M=ns1;
int N=ns1;
int NRHS=1;
double A[NSCORES+1][NSCORES+1];
int LDA=NSCORES+1;
double B[NSCORES+1];
int LDB=NSCORES+1;
double S[NSCORES+1];
double RCOND=0.00001; // singular values below this are treated as zero.
int RANK;
double WORK[1000];
int LWORK=1000;
int INFO;
for(j1=0;j1<ns1;j1++) {
B[j1]=xtx[ns1][j1];
for(j2=0;j2<ns1;j2++)
A[j1][j2]=xtx[j1][j2];
}
for(j1=0;j1<ns1;j1++) A[j1][j1]+=LAMBDA;
/*dgesv_(&N,&NRHS,A,&LDA,IPIV,B,&LDB,&INFO);*/
/*dgels_(&TRANS,&M,&N,&NRHS,A,&LDA,B,&LDB,WORK,&LWORK,&INFO);*/
/*dgelss_( &M, &N, &NRHS, A, &LDA, B, &LDB, S, &RCOND, &RANK, WORK, &LWORK, &INFO );*/
dposv_(&UFLO,&N,&NRHS,A,&LDA,B,&LDB,&INFO);
if(INFO) error("failed %d\n",INFO);
for(j1=0;j1<=nscores;j1++)
xty[j1]=B[j1];
lg("Check that the matrix inversion worked:\n");
for(j1=0;j1<=nscores;j1++) {
double sum=LAMBDA*B[j1];
for(j2=0;j2<=nscores;j2++)
sum+=xtx[j1][j2]*B[j2];
lg("%f\t%f\n",sum,xtx[nscores+1][j1]);
}
}
loadmix(char *fnames[], int nscores, double *weights) {
if(nscores<2 || nscores>NSCORES) error("Bad number of files\n");
double xty[NSCORES+1];
if(weights) {
int j;
for(j=0;j<=nscores;j++)
xty[j]=weights[j];
} else
computemix(fnames, nscores, xty);
lg("Mixing coeeficients\n");
int f;
for(f=0;f<=nscores;f++)
lg("-lew %f ",xty[f]);
lg("\n");
FILE *fp[NSCORES];
openfiles(fp,fnames,nscores);
int i;
for(i=0; i<NENTRIES; i++) {
PROGRESS(i,NENTRIES);
int r=(userent[i]>>USER_LMOVIEMASK)&7;
float s[NSCORES];
readfiles(fp,s,nscores);
float stotal=0.;
int j;
for(j=0;j<nscores;j++)
stotal+=xty[j]*(r-s[j]);
stotal+=xty[nscores];
err[i]=r-stotal;
}
closefiles(fp,nscores);
}