15 arma::mat softmax(arma::mat lp,
bool ref =
true,
bool log =
false) {
16 if (ref) lp.insert_cols(0, arma::zeros(lp.n_rows));
17 arma::colvec lpmax = arma::max(lp, 1);
18 lp.each_col() -= lpmax;
19 arma::colvec denom = sum(exp(lp), 1);
20 lp.each_col() -= arma::log(denom);
26 arma::vec softmax(arma::vec u) {
27 double umax = u.max();
29 double denom = sum(exp(u));
30 return u - log(denom);
36 arma::mat expit(arma::mat x) {
37 for (
unsigned i=0; i < x.n_elem; i++) {
49 arma::cx_mat expit(arma::cx_mat x) {
50 return 1.0/(1+exp(-x));
57 IID logistic_iid(
const arma::vec &y,
64 for (
unsigned i=0; i < x.n_cols; i++)
66 arma::vec v = p%(1-p)%w;
67 arma::mat H = arma::zeros(x.n_cols, x.n_cols);
68 for (
unsigned i=0; i < x.n_rows; i++) {
69 H += v[i]*(trans(x.row(i))*x.row(i));
74 IID linear_iid(
const arma::vec &y,
79 double df = y.n_elem-x.n_cols;
81 double sigma2 = sum(r%s)/df;
83 for (
unsigned i=0; i < x.n_cols; i++)
85 arma::mat H = arma::zeros(x.n_cols, x.n_cols);
86 for (
unsigned i=0; i < x.n_rows; i++) {
87 H += w[i]*trans(x.row(i))*x.row(i);
89 return IID(U, sigma2*H.i());
Utility functions for Generalized Linear Models.