mlogit.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "glm.hpp"
14 #include "utils.hpp"
15 
16 namespace target {
17 
18  class MLogit {
19  private:
20  arma::mat _z1; // alternative-specific with constant coef.
21  arma::mat _z2; // alternative-specific with varying c19oef.
22  arma::mat _x; // individual specific covariates
23  arma::uvec _id_idx; // Index of start of each cluster (choice
24  // observations for each individual is a 'cluster')
25  arma::uvec _alt; // Vector of alternatives
26  arma::uvec _choice; // alternative-specific with constant coef.
27  // Vector of unique alternatives (base-ref: 0)
28  arma::uvec alt_idx;
29  arma::vec _weights;
30 
31  // Parameters
32  arma::vec theta_z1;
33  arma::mat theta_z2;
34  arma::mat theta_x;
35  arma::vec theta;
36  arma::uvec idx_z1; // Index of the parameters belonging to 'z1'
37  arma::uvec idx_z2; // Index of the parameters belonging to 'z2'
38  arma::uvec idx_x; // Index of the parameters belonging to 'x'
39 
40 
41  public:
42  unsigned n; // Number of observations in long format
43  // (individuals and choices)
44  unsigned ncl; // Number of individuals
45  unsigned J; // Number of alternatives
46  unsigned basealt = 0; // Base alternative
47  arma::vec lp;
48  arma::mat zx;
49  arma::vec logpr;
50  unsigned p_z1; // Number of columns in z1
51  unsigned p_z2; // Number of columns in z2
52  unsigned p_x; // Number of columns in x
53 
54  MLogit() {} // Empty constructor
55  MLogit(const arma::uvec &choice,
56  const arma::uvec &alt,
57  const arma::uvec &id_idx,
58  const arma::mat &z1,
59  const arma::mat &z2,
60  const arma::mat &x,
61  unsigned nalt = 0,
62  arma::vec weights = arma::vec());
63 
64  arma::mat hessian(bool update = false);
65  arma::mat score(bool update = false, bool indiv = true);
66  double loglik();
67  void updateZX();
68  void updateRef(unsigned basealt);
69  void updatePar(arma::vec theta);
70  arma::vec getPar() { return theta; }
71  void updateProb();
72  void updateProb(arma::vec theta) {
73  updatePar(theta);
74  updateProb();
75  }
76 
77  const arma::vec* Weights() { return &_weights; }
78  double Weights(unsigned i) { return _weights(i); }
79 
80  const arma::uvec* Choice() { return &_choice; }
81  unsigned Choice(unsigned i) { return _choice(i); }
82 
83  const arma::uvec* Alt() { return &_alt; }
84  unsigned Alt(unsigned i) { return _alt(i); }
85 
86  const arma::mat* X() { return &_x; }
87  arma::rowvec X(unsigned row) { return _x.row(row); }
88 
89  const arma::mat* Z1() { return &_z1; }
90  arma::rowvec Z1(unsigned row) { return _z1.row(row); }
91 
92  const arma::mat* Z2() { return &_z2; }
93  arma::rowvec Z2(unsigned row) { return _z2.row(row); }
94 
95  const arma::uvec* cluster() { return &_id_idx; }
96  unsigned cluster(unsigned index) { return _id_idx(index); }
97 
98  void updateData(const arma::uvec &choice,
99  const arma::uvec &alt,
100  const arma::uvec &id_idx,
101  const arma::mat &z1,
102  const arma::mat &z2,
103  const arma::mat &x,
104  const arma::vec &weights) {
105  _choice = choice;
106  _z1 = z1;
107  _z2 = z2;
108  _x = x;
109  _id_idx = id_idx;
110  _alt = alt;
111  _weights = weights;
112  p_z1 = z1.n_cols;
113  p_z2 = z2.n_cols;
114  p_x = x.n_cols;
115  }
116  }; // class MLogit
117 
118 } // namespace target
Utility functions for Generalized Linear Models.
Various utility functions and constants.