Skip to content

Commit

Permalink
add assignments2023 folder
Browse files Browse the repository at this point in the history
  • Loading branch information
avehtari committed Sep 6, 2024
1 parent 57a2989 commit 821120c
Show file tree
Hide file tree
Showing 97 changed files with 48,626 additions and 0 deletions.
1 change: 1 addition & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project:
post-render:
- quarto render --profile public assignments
- cp -r assignments/assignments _site
- cp -r assignments2023 _site

website:
title: "Bayesian Data Analysis course"
Expand Down
34 changes: 34 additions & 0 deletions assignments2023/additional_files/assignment6/linear_model.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
data {
// number of data points
int<lower=0> N;
// covariate / predictor
vector[N] x;
// observations
vector[N] y;
// number of covariate values to make predictions at
int<lower=0> no_predictions;
// covariate values to make predictions at
vector[no_predictions] x_predictions;
}
parameters {
// intercept
real alpha;
// slope
real beta;
// the standard deviation should be constrained to be positive
real<upper=0> sigma;
}
transformed parameters {
// deterministic transformation of parameters and data
vector[N] mu = alpha + beta * x // linear model
}
model {
// observation model / likelihood
y ~ normal(mu, sigma);
}
generated quantities {
// compute the means for the covariate values at which to make predictions
vector[no_predictions] mu_pred = alpha + beta * x_predictions;
// sample from the predictive distribution, a normal(mu_pred, sigma).
array[no_predictions] real y_pred = normal_rng(to_array_1d(mu), sigma);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
data {
int<lower=0> N_observations;
int<lower=0> N_diets;
array[N_observations] int diet_idx; // Pair observations to their diets.
vector[N_observations] weight;
}

parameters {
// Average weight of chicks with a given diet.
vector[N_diets] mean_diet;

// Standard deviation of weights observed among chicks sharing a diet.
vector<lower=0>[N_diets] sd_diet;
}

model {
// Priors
// These look bad. I need to think about these again.

for (diet in 1:N_diets) {
mean_diet[diet] ~ normal(0, 10);
sd_diet[diet] ~ exponential(.02);
}

// Likelihood
for (obs in 1:N_observations) {
weight[obs] ~ normal(mean_diet[diet_idx[obs]], sd_diet[diet_idx[obs]]);
}

// Best practice would be to write the likelihood without the for loop as:
// weight ~ normal(mean_diet[diet_idx], sd_diet[diet_idx]);
}

generated quantities {
real weight_pred;
real mean_five;
// The below is just there to make the plotting in the template work with the "wrong model".
real sd_diets = sd_diet[4];

// Sample from the (posterior) predictive distribution of the fourth diet.
weight_pred = normal_rng(mean_diet[4], sd_diet[4]);

// Construct samples of the mean of the fifth diet.
// We only have the prior...
mean_five = normal_rng(0, 10);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 821120c

Please sign in to comment.