-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBayesian_Approach_sample_code.qmd
114 lines (77 loc) · 2.17 KB
/
Bayesian_Approach_sample_code.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: using R and python for estimate mean of normal distribution with prior normal
format: gfm
warning: false
message: false
---
## Using R and stan
```{r}
#| echo: false
if(!require(reticulate)){
options(download.file.method = "libcurl", download.file.extra = "-k")
chooseCRANmirror(ind = 1, graphics = FALSE)
install.packages("reticulate")
library(reticulate)
}
```
```{r}
# download require packages
if(!require(rstan)){
options(download.file.method = "libcurl", download.file.extra = "-k")
chooseCRANmirror(ind = 1, graphics = FALSE)
install.packages("rstan")
library(rstan)
}
```
```{r}
Init_time <- Sys.time()
library(rstan) # using rstan library
# in window system for using parallel computaion
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE) # for reduction or avoid reompliation programs
data <- c(2, 4, 6, 8, 1, 3, 5, 7, 9, 10) # my sample data
stan_model <- "
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real mu;
real<lower=0> sigma;
}
model {
y ~ normal(mu, sigma);
mu ~ normal(0, 1);
}
" # define stan model, we can using seperate file for define stan model with 'stan' profix
# Compile the model
sm <- stan_model(model_code = stan_model)
# Perform Bayesian inference
fit <- sampling(sm, data = list(N = length(data), y = data), iter = 1000, chains = 4)
# Print the summary of the inference results
print(summary(fit)$summary)
End_time <- Sys.time()
(R_Elapse_time <- difftime(End_time, Init_time, units = 'sec'))
```
***
***
## Using Python and PyMC
```{python}
import time
start_time = time.process_time() # for get elapsed runnig time in python
data = (2, 4, 6, 8, 1, 3, 5, 7, 9, 10)
import pymc as pm
# Define the model
with pm.Model() as model:
# Define the prior
mu = pm.Normal('mu', mu=0, sigma=1)
# Define the likelihood
likelihood = pm.Normal('likelihood', mu=mu, sigma=1, observed=data)
# Perform Bayesian inference
trace = pm.sample(1000)
# Print the summary of the inference results
print(pm.summary(trace))
end_time = time.process_time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time} seconds")
```