fastglmm
Massively scalable generalized linear mixed models
Loading...
Searching...
No Matches
spectralDecomp.h
Go to the documentation of this file.
1
2#ifndef SPECTRAL_DECOMP_H_
3#define SPECTRAL_DECOMP_H_
4
5// if -D USE_R, use RcppArmadillo library
6#ifdef USE_R
7#include <RcppArmadillo.h>
8#else
9#include <armadillo>
10#endif
11
12#include "misc.h"
13
14using namespace arma;
15
16namespace fastglmmLib {
17
18typedef enum {
21} ZTYPE;
22
28template <typename T>
30
31 public:
33
39 const T &U,
40 const vec &s,
41 const ZTYPE &type = GENERAL) :
42 U(U),
43 s(s),
44 Z(scaleEachRow(U, sqrt(s))),
45 type(type)
46 {}
47
52 const T &Z,
53 const ZTYPE &type = GENERAL ) :
54 Z(Z),
55 type(type){
56
57 vec one(Z.n_rows, fill::ones);
58 reweight(one);
59 }
60
61 // Copy constructor
63 U(other.U),
64 s(other.s),
65 Z(other.Z),
66 Zw(other.Zw),
67 V(other.V),
68 type(other.type){
69 }
70
76 void reweight( const vec &weights, const bool &sort = false){
77
78 switch( type ){
79 case GENERAL:
80 Zw = scaleEachCol(Z, sqrt(weights));
81 eig_sym( s, V, mat(Zw.t() * Zw) );
82 U = scaleEachRow(Zw * V, 1 / sqrt(s));
83 break;
84 case CATEGORICAL:
85 s = (weights.t() * Z).t();
86 U = scaleRowsCols(Z, sqrt(weights), 1 / sqrt(s));
87 break;
88 }
89
90 // mat Z_recon = scaleRowsCols(U, 1 / sqrt(weights), sqrt(s)) * V.t();
91 // Z_recon.print("Z_recon:");
92
93 if( sort ){
94 // reorder by decreasing eigen-value
95 uvec idx = sort_index(s, "descend");
96 s = s(idx);
97 U = U.cols(idx);
98 }
99 }
100
102 T get_U() const {
103 return U;
104 }
105
107 vec get_s() const {
108 return s;
109 }
110
112 mat get_V() const {
113
114 mat V_ret;
115
116 switch( type ){
117 case GENERAL:
118 V_ret = V;
119 break;
120 case CATEGORICAL:
121 V_ret = eye<mat>(U.n_cols, U.n_cols);
122 break;
123 }
124
125 return V_ret;
126 }
127
128 ZTYPE get_type() const {
129 return type;
130 }
131
132 protected:
133 T U;
134 vec s;
135 T Z, Zw;
136 mat V;
138};
139
140
141
142
143}
144
145
146
147
148#endif
ZTYPE type
Definition spectralDecomp.h:137
void reweight(const vec &weights, const bool &sort=false)
Definition spectralDecomp.h:76
spectralDecomp(const T &Z, const ZTYPE &type=GENERAL)
Definition spectralDecomp.h:51
spectralDecomp(const spectralDecomp &other)
Definition spectralDecomp.h:62
T Zw
Definition spectralDecomp.h:135
T Z
Definition spectralDecomp.h:135
mat get_V() const
Definition spectralDecomp.h:112
vec get_s() const
Definition spectralDecomp.h:107
ZTYPE get_type() const
Definition spectralDecomp.h:128
spectralDecomp(const T &U, const vec &s, const ZTYPE &type=GENERAL)
Definition spectralDecomp.h:38
T U
Definition spectralDecomp.h:133
vec s
Definition spectralDecomp.h:134
spectralDecomp()
Definition spectralDecomp.h:32
T get_U() const
Definition spectralDecomp.h:102
mat V
Definition spectralDecomp.h:136
mat scaleEachCol(const mat &X, const vec &w)
Definition misc.h:16
mat scaleEachRow(const mat &X, const vec &w)
Definition misc.h:31
T scaleRowsCols(const T &X, const vec &w1, const vec &w2)
Definition misc.h:48
Definition CleanData.h:17
ZTYPE
Definition spectralDecomp.h:18
@ CATEGORICAL
Definition spectralDecomp.h:20
@ GENERAL
Definition spectralDecomp.h:19