-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
97 changed files
with
48,626 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
assignments2023/additional_files/assignment6/linear_model.stan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
46 changes: 46 additions & 0 deletions
46
assignments2023/additional_files/assignment7/chickens_separate.stan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.