void lmMean(const mat& x,const mat& z, const rowvec& vec_beta, mat& mean){
	int P= mean.n_cols;
	mat xt=x, zt=z;
	xt.each_row() %=vec_beta.subvec(0,P-1);
	zt.each_row() %=vec_beta.subvec(P,2*P-1);
	mean= xt + zt;
	mean.each_row() += vec_beta.subvec(P*2,P*3-1);
}
Ejemplo n.º 2
0
void normalize_simplex(rowvec &x) {
    double sum = 0;
    for (auto &xi : x) {
        assert(xi >= 0);
        sum += xi;
    }

    if (sum < numeric_limits<double>::epsilon()) {
        x.fill(1.0 / x.size());
    } else {
        x /= sum;
    }
}
Ejemplo n.º 3
0
rowvec rand_dirichlet(const rowvec &alpha) {
    rowvec ret(alpha.size());
    for (unsigned int i = 0; i < ret.size(); ++i) {
        gamma_distribution<> g(alpha[i], 1);
        ret[i] = g(GENERATOR);
    }
    normalize_simplex(ret);
    return ret;
}
void lm2(const mat& wy, const cube& wX, const mat& Omega, rowvec& numer, mat& denom){
	int P=wy.n_cols, n = wy.n_rows, k = wX.n_rows;
	denom.zeros(), numer.zeros();
	mat twX(P,k);
	for(int t=0; t<n; t++){
		twX = trans(wX.slice(t));
		denom=denom+wX.slice(t)*Omega*twX; 
		numer=numer+wy.row(t)*Omega*twX;
	}
}
Ejemplo n.º 5
0
  mat outer_prod(const rowvec& v1, const vec& v2) {
    assert(v1.n_cols == v2.n_rows);
    uint size = v1.n_cols;
    mat res(size, size);


    for(uint c = 0 ; c < size; c++) {
      for(uint r = 0; r < size; r++) {
	res.at(r,c) = v1.at(c) * v2.at(r);
      }      
    }
    return res;
  }
Ejemplo n.º 6
0
void assignWinners(mat bids, rowvec prices, umat & assignments) {
	uword
		winnerIdx = 0,
		nItems = prices.size();
	double winningBid;

	for(int item = 0; item < nItems; item++) {
		vec winner = getMaxItemBid(item, bids);
		winnerIdx = winner(0);
		winningBid = winner(1);
		if(winningBid < 0.0)
			continue;
		prices(item) += winningBid;
		assignments.col(item).fill(0);
		assignments(winnerIdx, item) = 1;
	}
}
Ejemplo n.º 7
0
int rand_discrete(const rowvec &p) {
    discrete_distribution<> d(p.begin(), p.end());
    return d(GENERATOR);
}
mat compute_hessian_theta1_delta_weighted(uint i, mat station_data, uint wdclat1_col, uint wdclon1_col,
    double pointslat1_i, double pointslon1_i, double beta1, double sigma0, colvec xdeltain, 
    uvec st_point_list_uvec, rowvec deltain_row, urowvec mat_st_state_row, 
    NumericVector xv0_vec, uint focal_station_index, uint xtheta1_size,
    double point_density_i, rowvec points_den_covariates) {
  
          rowvec station_data_dis_vIdx = conv_to< rowvec >::from(latlondistance(station_data.col(wdclat1_col), 
            station_data.col(wdclon1_col), pointslat1_i, pointslon1_i));                    
          
          rowvec util = exp(beta1*station_data_dis_vIdx + deltain_row)% (mat_st_state_row==0);
          double den_util = sum(util);
          uint no_t_st = util.size();          
          //rowvec lambda_st_t(no_t_st,fill::zeros);
          mat hessian_beta1_delta_t(1,no_t_st,fill::zeros);
          mat hessian_theta1_delta_t(xtheta1_size,no_t_st,fill::zeros);
          rowvec grad_delta(no_t_st,fill::zeros);

          uvec no_focal_indexes(no_t_st,fill::zeros);
          
          //fill  no_focal_indexes with index sequence
          //find more efficient way to do this
          for(uint m=0; m<no_focal_indexes.size(); ++m) {
            no_focal_indexes(m)=m;
          }
          no_focal_indexes.shed_row(focal_station_index);

          for(int m=0; m<xv0_vec.size(); ++m) {
              double out = exp(-xv0_vec(m)*sigma0);
              double denutil_t = den_util+out;        
              
              rowvec util_prob_t =  util/denutil_t;

              rowvec disP = util_prob_t%station_data_dis_vIdx;
              double disP_sum = sum( disP);
              // rowvec disP_sum_vec(no_t_st);
              // disP_sum_vec.fill(disP_sum);

              vec val1(no_t_st,fill::zeros);
              val1 = station_data_dis_vIdx 
              val1 += station_data_dis_vIdx(focal_station_index) - 2*disP_sum_vec;
              val1 = val1 % util_prob_t;
              val1 *= -util_prob_t(focal_station_index);
              //remove focal_station_index from val1 as it is incorrect.
              val1.shed_row(focal_station_index);

              hessian_beta1_delta_t(0,no_focal_indexes) += val1;
              hessian_beta1_delta_t(0,focal_station_index) += util_prob_t(focal_station_index) * (1-2*util_prob_t(focal_station_index))\
                (station_data_dis_vIdx(focal_station_index)-disP_sum);

              grad_delta -= util_prob_t(focal_station_index)*util_prob_t;
              grad_delta(focal_station_index) += util_prob_t(focal_station_index);
          }
          grad_delta *= (1/xv0_vec.size());
          hessian_beta1_delta_t *= (1/xv0_vec.size())* point_density_i;
          
          mat hessian_thetaden_delta_t = points_den_covariates.t() * grad_delta;
          assert(hessian_thetaden_delta_t.n_rows==points_den_covariates.size());
          assert(hessian_thetaden_delta_t.n_cols==grad_delta.size());

          hessian_theta1_delta_t.row(0)=hessian_beta1_delta_t;
          hessian_theta1_delta_t.rows(span(2,xtheta1_size-1))=hessian_thetaden_delta_t;

          return((hessian_theta1_delta_t));  
}