-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
718 lines (570 loc) · 17.5 KB
/
matrix.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "matrix.h"
/* TODO:
1. Migrate shape variables from int to size_t or unsigned.
2. Make a helper function for null checking.
3. Add checks for shape conformity.
4. Add checks in Gaussian elimination for singularity.
5. Better way to share and/or free matrices with regression_fit?
*/
/************************************
* Private implementation functions *
************************************/
static int
index(int i, int j, int row_length){
return (i * row_length) + j;
}
static int
m_init_matrix(double init_val, matrix *m){
for(int i=0; i < m->rows * m->cols; i++){
m->data[i] = init_val;
}
return 1;
}
static int
m_free_data(matrix *m){
free(m->data);
return 1;
}
static int
m_free_matrix(matrix *m){
free(m);
return 1;
}
static int
m_print_matrix(matrix *m){
printf("[%d x %d]\n[", m->rows, m->cols);
for(int i=0; i < m->rows; i++){
// Prepend space for left alignment.
if(i > 0){
printf(" ");
}
// Print each entry for a given row.
for(int j=0; j < m->cols; j++){
printf("%.5f", at(i, j, m));
// If at the end of data, print a closing bracket.
if(i == m->rows-1 && j == m->cols-1){
printf("]");
}
// Otherwise print a comma delimiter.
else {
printf(", ");
}
}
// End every row with a newline.
printf("\n");
}
return 1;
}
static int
m_print_regression_fit(regression_fit *rfit){
printf("-------------------------"
"Summary of Regression Analysis"
"-------------------------\n\n");
printf("Number of Observations: %d\n", rfit->n);
printf("Number of Degrees of Freedom: %d\n\n", rfit->dof);
printf("R-squared: %.4f\n", rfit->r2);
printf("Adj R-squared: %.4f\n\n", rfit->adjusted_r2);
printf("-----------------------"
"Summary of Estimated Coefficients"
"------------------------\n");
printf(" Variable Coef t-stat \n");
printf("---------------------------"
"---------------------------"
"--------------------------\n");
for(int i=0; i < rfit->beta->rows; i++){
printf(" Column_%d %.4f %.4f \n",
i,
at(i, 0, rfit->beta),
at(i, 0, rfit->beta_tstats));
}
printf("---------------------------------"
"End of Summary"
"---------------------------------\n\n");
return 1;
}
static int
m_transpose(matrix *m, matrix *m_t){
m_t->rows = m->cols;
m_t->cols = m->rows;
int position = 0;
for(int j=0; j < m->cols; j++){
for(int i=0; i < m->rows; i++){
m_t->data[position] = at(i, j, m);
position++;
}
}
return 1;
}
static int
m_dot(matrix *a, matrix *b, matrix *c){
double val = 0.0;
for(int i=0; i < a->rows; i++){
for(int k=0; k < b->cols; k++){
val = 0.0;
for(int j=0; j < b->rows; j++){
val += at(i, j, a) * at(j, k, b);
}
*ptr_at(i, k, c) = val;
}
}
return 1;
}
static int
m_matrix_add(matrix *a, matrix *b, matrix *c){
for(int i=0; i < a->rows; i++){
for(int j=0; j < a->cols; j++){
*ptr_at(i, j, c) = at(i, j, a) + at(i, j, b);
}
}
return 1;
}
static int
m_matrix_subtract(matrix *a, matrix *b, matrix *c){
for(int i=0; i < a->rows; i++){
for(int j=0; j < a->cols; j++){
*ptr_at(i, j, c) = at(i, j, a) - at(i, j, b);
}
}
return 1;
}
static int
m_matrix_scalar_multiply(matrix *a, double b, matrix *c){
for(int i=0; i < a->rows; i++){
for(int j=0; j < a->cols; j++){
*ptr_at(i, j, c) = b * at(i, j, a);
}
}
return 1;
}
static double
m_matrix_mean(matrix *a, int col){
double result = 0.0;
/* Tabulate the sum along column 'col'.*/
for(int i=0; i < a->rows; i++){
result += at(i, col, a);
}
return result / ( (double) a->rows);
}
static int
m_gauss_solve(matrix *a_in, matrix *b, matrix *x){
int i; /* Used for loops over rows and columns. */
int j; /* Used for loops over columns. */
int k; /* Used for loops over rows. */
int m; /* Used for identifying the pivot row at each step. */
double row_multiplier; /* Multiple of row to be subtracted at each step. */
double temp; /* Placeholder for swap/pivot operations. */
double pivot_value; /* Container for max pivot value. */
int n = a_in->rows;
/* Allocate and initialize a copy, so that the process of
* solving Ax=b doesn't mutate A's data. Be sure to free. */
matrix *a = copy_matrix(a_in);
/* Forward steps to pivot rows and perform subtraction of row
* multiples.
*
* k tracks columns in forward order.
* m is used to track the row with maximal pivot entry in kth column.
* i is used to track rows that follow the kth row.
* j is used to track columns that follow the kth column. */
for (k=0; k < n-1; k++) {
pivot_value = (double) fabs(at(k, k, a));
m = k; /* m is pivot row, assumed equal to current row. */
/* Find the remaining row with largest pivot */
for (i=k+1; i < n; i++){
temp = (double) fabs(at(i, k, a));
if(temp > pivot_value){
pivot_value = temp;
m = i; /* m is set to row with largest pivot in kth column. */
}
}
// If location, m, of largest pivot element is not row k, then
// swap row m and row k in a and b.
if(m != k) {
/* Swap entries of b vector. */
temp = at(k, 0, b);
*ptr_at(k, 0, b) = at(m, 0, b);
*ptr_at(m, 0, b) = temp;
/* Here j is used for swapping the columns between row k and row m.
* This operation can effectively ignore swapping entries in the
* lower triangle, which is why j goes from k to n-1. The nature
* of the Gaussian elimination algorithm is such that by the end,
* all lower triangle elements must be zero, and are ignored. */
for(j=k; j < n; j++) {
temp = at(k, j, a);
*ptr_at(k, j, a) = at(m, j, a);
*ptr_at(m, j, a) = temp;
}
}
/* For each remaining row after the kth row, we are going to subtract a
* multiple of the pivot row from that row. The only columns affected
* are columns (k+1) through the final column. */
for (i=k+1; i < n; i++) {
row_multiplier = at(i, k, a) / at(k, k, a);
/* Notice how the loop below only affects columns to the right of
* the current column (k). This is because the formula would
* implicitly zero-out the entry at (j,k) -- the multiplier is
* a[i, k] / a[k, k] -- and when multiplied by a[k, k] it leaves
* just a[i, k]. So when j=k, the subtraction would result in
* a[i, k] - a[i, k] = 0.
*
* Rather than doing this redundant computation. The values in the
* lower triangle of a are simply ignored and never used during the
* back-substitution phase, treating them as 0 without computing
* with them. */
for (j=k+1; j < n; j++) {
*ptr_at(i, j, a) = at(i, j, a) - row_multiplier * at(k, j, a);
}
/* Adjust the RHS b vector by the same operation.*/
*ptr_at(i, 0, b) = at(i, 0, b) - row_multiplier * at(k, 0, b);
}
}
/* Back substitution to get final solution.
* j tracks the number of rows.
* k is used to iterate the rows in reverse.
* i is used to iterate upper-triangle columns of a given row. */
for (j=0; j < n; j++) {
/* k tracks from the final row back to the first row. */
k = n - j - 1;
/* Begin by setting the value of x[k] to b[k]. */
*ptr_at(k, 0, x) = at(k, 0, b);
/* By the time we get to column i, the value of x is already solved for
* row i, so the value x[i] is available to use. The scalar multiple of
* x[i] in the equation we want to solve for x[k] will be a[k, i], so we
* subtract a[k,i] * x[i] from the running solution for x[k] */
for(i=k+1; i < n; i++) {
*ptr_at(k, 0, x) = at(k, 0, x) - at(k, i, a) * at(i, 0, x);
}
/* Finally, we divide by the pivot entry in row k, since everything
* else has correctly been accounted for in x[k]. */
*ptr_at(k, 0, x) = at(k, 0, x) / at(k, k, a);
/* Note that the above loop would be skipped when k=n-1, for example
* and x[n-1] would just be set to the value b[n-1]/a[n-1, n-1] at that
* point. Then the loop would only have one iteration for k=n-2, and
* x[n-2] would be set to:
* (b[n-2] - a[n-2, n-1] * x[n-1]) / a[n-2, n-2]
* ... and so forth back up to k=0. */
}
free_matrix(a);
return 1;
}
static int
m_inverse(matrix *m, matrix *m_inv){
matrix *x = new_matrix(m->rows, 1);
matrix *b = new_matrix(m->rows, 1);
// Solve Ax=b once for each column.
for(int c=0; c < m->cols; c++){
// Initialize to c-th coordinate vector,
// in case pivoting changed it.
for(int i=0; i < m->rows; i++){
if(i == c){
*ptr_at(i, 0, b) = 1.0;
} else {
*ptr_at(i, 0, b) = 0.0;
}
}
m_gauss_solve(m, b, x);
for(int i=0; i < m->rows; i++){
*ptr_at(i, c, m_inv) = at(i, 0, x);
}
}
free_matrix(x);
free_matrix(b);
return 1;
}
static int
m_lstsq(matrix *y, matrix *x, regression_fit *rfit){
matrix *xT = new_matrix(x->cols, x->rows);
matrix *xTy = new_matrix(x->cols, y->cols);
matrix *xTx = new_matrix(x->cols, x->cols);
matrix *xTx_inv = new_matrix(x->cols, x->cols);
matrix *eps = new_matrix(y->rows, y->cols);
matrix *epsT = new_matrix(y->cols, y->rows);
matrix *y_hat = new_matrix(y->rows, y->cols);
matrix *epsTeps = new_matrix(y->cols, y->cols);
int n = x->rows;
int degrees_of_freedom = x->cols;
double y_bar = matrix_mean(y, 0);
double ssr = 0.0;
double tss = 0.0;
double n_m_1;
double n_m_p_1;
double std_err;
rfit->n = n;
rfit->dof = degrees_of_freedom;
// For regression fit.
rfit->beta = new_matrix(x->cols, y->cols);
transpose(x, xT);
dot(xT, x, xTx);
inverse(xTx, xTx_inv);
dot(xT, y, xTy);
dot(xTx_inv, xTy, rfit->beta);
// For summary statistics.
dot(x, rfit->beta, y_hat);
matrix_subtract(y, y_hat, eps);
transpose(eps, epsT);
dot(epsT, eps, epsTeps);
rfit->s2 = at(0, 0, epsTeps) / (n - degrees_of_freedom);
// Sum of squares for r2 calculation.
for(int i=0; i < y->rows; i++){
ssr += pow(at(i, 0, y_hat) - y_bar, 2.0);
tss += pow(at(i, 0, y) - y_bar, 2.0);
}
rfit->r2 = (ssr / tss);
// Adjusted R2
n_m_1 = rfit->n - 1;
n_m_p_1 = rfit->n - (rfit->dof - 1) - 1;
rfit->adjusted_r2 = 1.0 - (1.0 - rfit->r2) * (n_m_1 / n_m_p_1);
// Beta covariance and t-stats
rfit->beta_cov = new_matrix(xTx_inv->rows, xTx_inv->cols);
matrix_scalar_multiply(xTx_inv, rfit->s2, rfit->beta_cov);
rfit->beta_tstats = new_matrix(rfit->beta->rows, rfit->beta->cols);
for(int i=0; i < rfit->beta->rows; i++){
std_err = sqrt(at(i, i, rfit->beta_cov));
*ptr_at(i, 0, rfit->beta_tstats) = at(i, 0, rfit->beta) / std_err;
}
free_matrix(xT);
free_matrix(xTy);
free_matrix(xTx);
free_matrix(xTx_inv);
free_matrix(eps);
free_matrix(epsT);
free_matrix(epsTeps);
free_matrix(y_hat);
return 1;
}
/************************
* Public API Functions *
************************/
double
at(int i, int j, matrix *m){
if(m == NULL){
return 0.0;
} else if(m->data == NULL){
return 0.0;
} else if(i >= m->rows || j >= m->cols){
return 0.0;
}
return m->data[index(i, j, m->cols)];
}
double *
ptr_at(int i, int j, matrix *m){
if(m == NULL){
return NULL;
} else if(m->data == NULL){
return NULL;
} else if(i >= m->rows || j >= m->cols){
return NULL;
}
return &(m->data[index(i, j, m->cols)]);
}
int
init_matrix(double init_val, matrix *m){
if(m == NULL){
return -1;
} else if(m->data == NULL){
return -2;
}
return m_init_matrix(init_val, m);
}
matrix *
new_matrix(int i, int j){
matrix *m = (matrix *) malloc(sizeof(matrix));
if(m == NULL){
return NULL;
}
m->rows = i;
m->cols = j;
m->data = (double *) malloc(sizeof(double) * m->rows * m->cols);
if(m->data == NULL){
free(m);
return NULL;
}
m_init_matrix(0.0, m);
return m;
}
matrix *
copy_matrix(matrix *m){
if(m == NULL){
return NULL;
}
if(m->data == NULL){
return NULL;
}
matrix *output = new_matrix(m->rows, m->cols);
for(int i=0; i < m->rows * m->cols; i++){
output->data[i] = m->data[i];
}
return output;
}
matrix *
make_matrix(int i, int j, ...){
va_list arg_list;
va_start(arg_list, j);
matrix *m = (matrix *) malloc(sizeof(matrix));
if(m == NULL){
return NULL;
}
m->data = (double *) malloc(sizeof(double) * i * j);
if(m->data == NULL){
free(m);
return NULL;
}
m->rows = i;
m->cols = j;
for(int pos=0; pos < i*j; pos++){
m->data[pos] = va_arg(arg_list, double);
}
va_end(arg_list);
return m;
}
int
free_data(matrix *m){
if(m == NULL){
return -1;
} else if(m->data == NULL){
return -2;
}
return m_free_data(m);
}
int
free_matrix(matrix *m){
int data_status = free_data(m);
if(data_status == 0){
return m_free_matrix(m);
}
return data_status;
}
regression_fit *
new_regression_fit(void){
regression_fit *rfit = (regression_fit *) malloc(sizeof(regression_fit));
if(rfit == NULL){
return NULL;
}
return rfit;
}
int
free_regression_fit(regression_fit *rfit){
if( rfit->beta == NULL
|| rfit->beta_cov == NULL
|| rfit->beta_tstats == NULL){
return -1;
}
// TODO -- further check return statuses of free_matrix.
free_matrix(rfit->beta);
free_matrix(rfit->beta_cov);
free_matrix(rfit->beta_tstats);
free(rfit);
return 1;
}
int
print_matrix(matrix *m){
if(m == NULL){
return -1;
} else if(m->data == NULL){
return -2;
}
return m_print_matrix(m);
}
int
print_regression_fit(regression_fit *rfit){
if(rfit == NULL){
return -1;
}
return m_print_regression_fit(rfit);
}
int
transpose(matrix *m, matrix *m_t){
if(m == NULL || m_t == NULL){
return -1;
} else if(m->data == NULL || m_t->data == NULL){
return -2;
}
return m_transpose(m, m_t);
}
int
dot(matrix *a, matrix *b, matrix *c){
if(a == NULL || b == NULL || c == NULL){
return -1;
} else if(a->data == NULL || b->data == NULL || c->data == NULL){
return -2;
} else if(a->cols != b->rows){
return -3;
} else if(a->rows != c->rows || b->cols != c->cols){
return -4;
}
return m_dot(a, b, c);
}
int
matrix_add(matrix *a, matrix *b, matrix *c){
if(a == NULL || b == NULL || c == NULL){
return -1;
} else if(a->data == NULL || b->data == NULL || c->data == NULL){
return -2;
} else if(a->rows != b->rows || b->rows != c->rows){
return -3;
} else if(a->cols != b->cols || b->cols != c->cols){
return -4;
}
return m_matrix_add(a, b, c);
}
int
matrix_subtract(matrix *a, matrix *b, matrix *c){
if(a == NULL || b == NULL || c == NULL){
return -1;
} else if(a->data == NULL || b->data == NULL || c->data == NULL){
return -2;
} else if(a->rows != b->rows || b->rows != c->rows){
return -3;
} else if(a->cols != b->cols || b->cols != c->cols){
return -4;
}
return m_matrix_subtract(a, b, c);
}
int
matrix_scalar_multiply(matrix *a, double b, matrix *c){
if(a == NULL || c == NULL){
return -1;
} else if(a->data == NULL || c->data == NULL){
return -2;
} else if(a->rows != c->rows || a->cols != c->cols){
return -3;
}
return m_matrix_scalar_multiply(a, b, c);
}
int
inverse(matrix *a, matrix *a_inv){
if(a == NULL || a_inv == NULL){
return -1;
} else if(a->data == NULL || a_inv->data == NULL){
return -2;
} else if(a->rows != a_inv->rows){
return -3;
} else if(a->cols != a_inv->cols){
return -4;
}
return m_inverse(a, a_inv);
}
int
lstsq(matrix *y, matrix *x, regression_fit *rfit){
if(y == NULL || x == NULL || rfit == NULL){
return -1;
}
if(y->data == NULL || x->data == NULL){
return -2;
}
// TODO
// add shape conformity and missing data checks.
return m_lstsq(y, x, rfit);
}
double
matrix_mean(matrix *a, int col){
if(a == NULL || a->data == NULL){
return 0.0;
} else if(a->cols >= col){
return 0.0;
}
return m_matrix_mean(a, col);
}