Beispiel #1
0
 void StateModel::simulate_initial_state(VectorView eta)const{
   if(eta.size() != state_dimension()){
     std::ostringstream err;
     err << "output vector 'eta' has length " << eta.size()
         << " in StateModel::simulate_initial_state.  Expected length "
         << state_dimension();
     report_error(err.str());
   }
   eta = rmvn(initial_state_mean(), initial_state_variance());
 }
 void DRSM::set_xnames(const std::vector<std::string> &xnames) {
   if (xnames.size() != state_dimension()) {
     std::ostringstream err;
     err << "Error in DRSM::set_xnames." << endl
         << "The size of xnames is " << xnames.size() << endl
         << "But ncol(X) is " << state_dimension() << endl;
     report_error(err.str());
   }
   xnames_ = xnames;
 }
Beispiel #3
0
 //======================================================================
 void ArStateModel::set_initial_state_mean(const Vector &mu) {
   if (mu.size() != state_dimension()) {
     std::ostringstream err;
     err << "Attempt to set mu to the wrong size in "
         "ArStateModel::set_initial_state_mean." << std::endl
         << " Required size: " << state_dimension() << std::endl
         << "Supplied argument: " << mu.size() << std::endl;
     report_error(err.str());
   }
   initial_state_mean_ = mu;
 }
// TODO(stevescott):  test
void ASSR::simulate_initial_state(VectorView state0)const {
    // First, simulate the initial state of the client state vector.
    VectorView client_state(state0, 0, state0.size()-2);
    StateSpaceModelBase::simulate_initial_state(client_state);

    // Next simulate the initial value of the first latent weekly
    // observation.
    double mu = StateSpaceModelBase::observation_matrix(0).dot(client_state);
    state0[state_dimension() - 2] = rnorm(mu, regression_->sigma());

    // Finally, the initial state of the cumulator variable is zero.
    state0[state_dimension() - 1] = 0;
}
 //======================================================================
 Vector ArStateModel::initial_state_mean()const{
   if (initial_state_mean_.size() != state_dimension()) {
     report_error("mu_.size() != state_dimension() in "
                  "ArStateModel::initial_state_mean()");
   }
   return initial_state_mean_;
 }
 TrigStateModel::TrigStateModel(double period, const Vector &frequencies)
     : IndependentMvnModel(2 * frequencies.size()),
       period_(period),
       frequencies_(frequencies),
       state_transition_matrix_(new IdentityMatrix(state_dimension())),
       state_variance_matrix_(new DiagonalMatrixBlock(state_dimension())),
       variance_is_current_(false)
 {
   if (frequencies_.empty()) {
     report_error("At least one frequency needed to initialize TrigStateModel.");
   }
   for (int i = 0; i < frequencies_.size(); ++i) {
     frequencies_[i] = 2 * Constants::pi * frequencies_[i] / period_;
   }
   set_mu(Vector(state_dimension(), 0));
 }
 //======================================================================
 void ArStateModel::set_initial_state_variance(const SpdMatrix &Sigma){
   if(Sigma.nrow() != state_dimension()){
     report_error("attempt to set Sigma to the wrong size in "
                  "ArStateModel::set_initial_state_mean");
   }
   initial_state_variance_ = Sigma;
 }
 //======================================================================
 void ArStateModel::set_initial_state_mean(const Vector &mu){
   if(mu.size() != state_dimension()){
     report_error("attempt to set mu to the wrong size in "
                  "ArStateModel::set_initial_state_mean");
   }
   initial_state_mean_ = mu;
 }
 void TrigStateModel::set_initial_state_variance(
     const SpdMatrix &variance) {
   if (nrow(variance) != state_dimension()) {
     report_error("initial_state_variance is the wrong size "
                  "in TrigStateModel.");
   }
   initial_state_variance_ = variance;
 }
 SparseVector TrigStateModel::observation_matrix(int t) const {
   Vector trig(state_dimension());
   for (int i = 0; i < frequencies_.size(); ++i) {
     trig[2 * i] = cos(frequencies_[i] * t);
     trig[2 * i + 1] = sin(frequencies_[i] * t);
   }
   return SparseVector(trig);
 }
Beispiel #11
0
  //======================================================================
  SpdMatrix ArStateModel::initial_state_variance()const{
    if (initial_state_variance_.nrow() != state_dimension()) {
      report_error("Sigma_.nrow() != state_dimension() in "
                   "ArStateModel::initial_state_mean()");
    }
    SpdMatrix & Sigma(const_cast<SpdMatrix &>(initial_state_variance_));

    if (stationary_initial_distribution_) {
      Vector gamma = autocovariance(state_dimension());
      Sigma.diag() = gamma[0];
      for(int i = 1; i < state_dimension(); ++i){
        Sigma.superdiag(i) = gamma[i];
      }
      Sigma.reflect();
    }
    return initial_state_variance_;
  }
// TODO(stevescott):  check this
SparseVector ASSR::observation_matrix(int t)const {
    Ptr<FineNowcastingData> fine_data(this->fine_data(t));
    int p = state_dimension();
    SparseVector ans(p);
    ans[p-1] = 1;
    ans[p-2] = fine_data->fraction_in_initial_period();
    return ans;
}
 SparseVector RWHSM::observation_matrix(int t)const{
   Date now = time_zero_ + t;
   SparseVector ans(state_dimension());
   if(holiday_->active(now)){
     Date holiday_date(holiday_->nearest(now));
     int position = now - holiday_->earliest_influence(holiday_date);
     ans[position] = 1.0;
   }
   return ans;
 }
Vec ASSR::simulate_state_error(int t)const {
    int state_dim = state_dimension();
    Vec ans(state_dim, 0);
    VectorView client_state_error(ans, 0, state_dim - 2);
    client_state_error = StateSpaceModelBase::simulate_state_error(t);

    // TODO(stevescott):  check this
    ans[state_dim - 2] =
        StateSpaceModelBase::observation_matrix(t).dot(client_state_error)
        + rnorm(0, regression_->sigma());
    ans.back() = 0;

    return ans;
}
    /**
     * \copydoc ProcessModelInterface::predict_state
     */
    virtual State predict_state(double delta_time,
                                const State& state,
                                const Noise& noise,
                                const Input& input)
    {
        State prediction = State(state_dimension(), 1);

        predict_state<sizeof...(Models)>(
            models_,
            delta_time,
            state,
            noise,
            input,
            prediction);

        return prediction;
    }
// | V0   Z^T*V0   0 |
// | V0*Z Z^T*V0*Z 0 |
// | 0    0        0 |
Spd ASSR::initial_state_variance()const {
    Spd V0 = StateSpaceModelBase::initial_state_variance();
    SparseVector Z0(StateSpaceModelBase::observation_matrix(0));
    Vec covariance = V0 * Z0;
    double y_variance = Z0.dot(covariance) + regression_->sigsq();

    int state_dim = state_dimension();
    Spd ans(state_dim, 0.0);
    SubMatrix upper_left(ans, 0, state_dim - 3, 0, state_dim - 3);
    upper_left = V0;
    ans.col(state_dim - 2);
    VectorView covariance_column(ans.col(state_dim - 2), 0, state_dim - 2 );
    VectorView covariance_row(ans.row(state_dim - 2), 0, state_dim - 2 );
    covariance_column = covariance;
    covariance_row = covariance;
    ans(state_dim - 2, state_dim -2) = y_variance;
    return ans;
}
// Update the sufficient statistics for the regression component of
// the model.
void ASSR::observe_data_given_state(int t) {
    const ConstVectorView alpha(state(t));
    int state_dim = state_dimension();
    const ConstVectorView client_state_error(alpha, 0, state_dim - 2);
    double y = alpha[state_dim - 2];
    // y is the imputed fine-scale observation from the Kalman filter.

    Ptr<RegressionData> dp(regression_->dat()[t]);
    // The state_mean is computed using the observation_matrix from
    // the client model, available from StateSpaceModelBase.
    double state_mean =
        StateSpaceModelBase::observation_matrix(t).dot(client_state_error);

    // We want y with time series effects subtracted off.  We get this
    // by computing state_mean (which contains the full prediction of
    // y given all state, including the regressors), computing the
    // residual from state_mean, and adding in the regression effect
    // back in.
    double residual = y - state_mean;
    double predicted = regression_->predict(dp->x());
    regression_->suf()->add_mixture_data(residual + predicted, dp->x(), 1.0);
}
 void TrigStateModel::set_initial_state_mean(const Vector &mean) {
   if (mean.size() != state_dimension()) {
     report_error("Initial state mean is the wrong size for TrigStateModel.");
   }
   initial_state_mean_ = mean;
 }
Beispiel #19
0
 //======================================================================
 void ArStateModel::simulate_state_error(RNG &rng, VectorView eta,
                                         int t) const {
   assert(eta.size() == state_dimension());
   eta = 0;
   eta[0] = rnorm_mt(rng) * sigma();
 }
Vec ASSR::simulate_initial_state()const {
    Vec ans(state_dimension());
    simulate_initial_state(VectorView(ans));
    return ans;
}
  Vector StateSpaceLogitModel::one_step_holdout_prediction_errors(
      RNG &rng,
      BinomialLogitDataImputer &data_imputer,
      const Vector &successes,
      const Vector &trials,
      const Matrix &predictors,
      const Vector &final_state) {
    if (nrow(predictors) != successes.size()
        || trials.size() != successes.size()) {
      report_error("Size mismatch in arguments provided to "
                   "one_step_holdout_prediction_errors.");
    }
    Vector ans(successes.size());
    int t0 = dat().size();
    ScalarKalmanStorage ks(state_dimension());
    ks.a = *state_transition_matrix(t0 - 1) * final_state;
    ks.P = SpdMatrix(state_variance_matrix(t0 - 1)->dense());

    // This function differs from the Gaussian case because the
    // response is on the binomial scale, and the state model is on
    // the logit scale.  Because of the nonlinearity, we need to
    // incorporate the uncertainty about the forecast in the
    // prediction for the observation.  We do this by imputing the
    // latent logit and its mixture indicator for each observation.
    // The strategy is (for each observation)
    //   1) simulate next state.
    //   2) simulate w_t given state
    //   3) kalman update state given w_t.
    for (int t = 0; t < ans.size(); ++t) {
      bool missing = false;
      Vector state = rmvn(ks.a, ks.P);

      double state_contribution = observation_matrix(t+t0).dot(state);
      double regression_contribution =
          observation_model_->predict(predictors.row(t));
      double mu = state_contribution + regression_contribution;
      double prediction = trials[t] * plogis(mu);
      ans[t] = successes[t] - prediction;

      // ans[t] is a random draw of the one step ahead prediction
      // error at time t0+t given observed data to time t0+t-1.  We
      // now proceed with the steps needed to update the Kalman filter
      // so we can compute ans[t+1].

      double precision_weighted_sum, total_precision;
      std::tie(precision_weighted_sum, total_precision) = data_imputer.impute(
          rng,
          trials[t],
          successes[t],
          mu);
      double latent_observation = precision_weighted_sum / total_precision;
      double latent_variance = 1.0 / total_precision;

      // The latent state was drawn from its predictive distribution
      // given Y[t0 + t -1] and used to impute the latent data for
      // y[t0+t].  That latent data is now used to update the Kalman
      // filter for the next time period.  It is important that we
      // discard the imputed state at this point.
      sparse_scalar_kalman_update(latent_observation - regression_contribution,
                                  ks.a,
                                  ks.P,
                                  ks.K,
                                  ks.F,
                                  ks.v,
                                  missing,
                                  observation_matrix(t + t0),
                                  latent_variance,
                                  *state_transition_matrix(t + t0),
                                  *state_variance_matrix(t + t0));
    }
    return ans;
  }