Skip to content

Commit 66aa564

Browse files
committed
Try StableRNG
1 parent 2db4ffb commit 66aa564

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.11.7"
44
manifest_format = "2.0"
5-
project_hash = "271c58c55ced391f9e88f814e2e4b8b4b7d5724d"
5+
project_hash = "2b3993e6e60ba9c1c456523b8f2caaae65279013"
66

77
[[deps.ADTypes]]
88
git-tree-sha1 = "27cecae79e5cc9935255f90c53bb831cc3c870d7"

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
4141
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
4242
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
4343
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
44+
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
4445
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
4546
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
4647
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"

tutorials/bayesian-linear-regression/index.qmd

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ using StatsBase
4242
using LinearAlgebra
4343
4444
# For ensuring reproducibility.
45-
using Random
45+
using StableRNGs: StableRNG
4646
```
4747

4848
```{julia}
@@ -75,7 +75,7 @@ The next step is to get our data ready for testing. We'll split the `mtcars` dat
7575
select!(data, Not(:Model))
7676
7777
# Split our dataset 70%/30% into training/test sets.
78-
trainset, testset = map(DataFrame, splitobs(Xoshiro(472), data; at=0.7, shuffle=true))
78+
trainset, testset = map(DataFrame, splitobs(StableRNG(468), data; at=0.7, shuffle=true))
7979
8080
# Turing requires data in matrix form.
8181
target = :MPG
@@ -142,7 +142,7 @@ With our model specified, we can call the sampler. We will use the No U-Turn Sam
142142

143143
```{julia}
144144
model = linear_regression(train, train_target)
145-
chain = sample(Xoshiro(468), model, NUTS(), 20_000)
145+
chain = sample(StableRNG(468), model, NUTS(), 20_000)
146146
```
147147

148148
We can also check the densities and traces of the parameters visually using the `plot` functionality.
@@ -242,9 +242,11 @@ let
242242
ols_test_loss = msd(test_prediction_ols, testset[!, target])
243243
@assert bayes_train_loss < bayes_test_loss "Bayesian training loss ($bayes_train_loss) >= Bayesian test loss ($bayes_test_loss)"
244244
@assert ols_train_loss < ols_test_loss "OLS training loss ($ols_train_loss) >= OLS test loss ($ols_test_loss)"
245-
@assert isapprox(bayes_train_loss, ols_train_loss; rtol=0.01) "Difference between Bayesian training loss ($bayes_train_loss) and OLS training loss ($ols_train_loss) unexpectedly large!"
246-
@assert isapprox(bayes_test_loss, ols_test_loss; rtol=0.05) "Difference between Bayesian test loss ($bayes_test_loss) and OLS test loss ($ols_test_loss) unexpectedly large!"
245+
@assert bayes_train_loss > ols_train_loss "Bayesian training loss ($bayes_train_loss) <= OLS training loss ($bayes_train_loss)"
246+
@assert bayes_test_loss < ols_test_loss "Bayesian test loss ($bayes_test_loss) >= OLS test loss ($ols_test_loss)"
247247
end
248248
```
249249

250-
As we can see above, OLS and our Bayesian model fit our training and test data set about the same.
250+
We can see from this that both linear regression techniques perform fairly similarly.
251+
The Bayesian linear regression approach performs worse on the training set, but better on the test set.
252+
This indicates that the Bayesian approach is more able to generalise to unseen data, i.e., it is not overfitting the training data as much.

0 commit comments

Comments
 (0)