-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorDistr.R
248 lines (175 loc) · 7.03 KB
/
PriorDistr.R
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Load necessary package
#install.packages("ggplot2")
library(ggplot2)
########### Simulating Beta distribution as prior ###############
# Define the parameters:
alpha <- 2
beta <- 2
# Create a sequence of x values from 0 to 1
x <- seq(0, 1, length.out = 100)
# Compute the density of the Beta distribution for each x value
y <- dbeta(x, shape1 = alpha, shape2 = beta)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Beta distribution with grey-blue color
ggplot(data, aes(x = x, y = y)) +
geom_line(color = "#6699CC") +
geom_area(fill = "lightblue", alpha=0.5) +
labs(title = "Beta Distribution (2, 2)",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating Exponential distribution as prior ###############
# Define the rate parameter for the exponential distribution
rate <- 1.54
# Create a sequence of x values from 0 to 5 (to cover a range of the distribution)
x <- seq(0, 5, length.out = 100)
# Compute the density of the exponential distribution for each x value
y <- dexp(x, rate = rate)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the exponential distribution
ggplot(data, aes(x = x, y = y)) +
geom_line(color = "#6699CC") +
geom_area(fill = "lightblue", alpha = 0.5) +
labs(title = "Exponential Distribution (rate = 1.54)",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating Gamma distribution as prior ###############
# Define the parameters for the Gamma distribution
shape <- 2
rate <- 0.5
# Create a sequence of x values
x <- seq(0, 10, length.out = 100)
# Compute the density of the Gamma distribution for each x value
y <- dgamma(x, shape = shape, rate = rate)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Gamma distribution with light blue fill (half transparent)
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Gamma Distribution (shape = 2, rate = 0.5)",
x = "x",
y = "Density") +
theme_minimal()
## now let's make some comparisons:
# Define the parameters for the Gamma distributions
shape1 <- 2
rate1 <- 0.5
shape2 <- 2
rate2 <- 1
# Create a sequence of x values
x <- seq(0, 10, length.out = 100)
# Compute the density of the Gamma distributions for each x value
y1 <- dgamma(x, shape = shape1, rate = rate1)
y2 <- dgamma(x, shape = shape2, rate = rate2)
# Create a data frame for plotting
data1 <- data.frame(x = x, y = y1, distribution = "Gamma(2, 0.5)")
data2 <- data.frame(x = x, y = y2, distribution = "Gamma(2, 1)")
data <- rbind(data1, data2)
# Plot the Gamma distributions
ggplot(data, aes(x = x, y = y, fill = distribution, color = distribution)) +
geom_area(data = data1, fill = "lightblue", alpha = 0.5) +
geom_line(data = data1, color = "#6699CC") +
geom_area(data = data2, fill = "lightcoral", alpha = 0.5) +
geom_line(data = data2, color = "red") +
labs(title = "Gamma Distributions",
x = "x",
y = "Density") +
theme_minimal() +
scale_fill_manual(values = c("Gamma(2, 0.5)" = "lightblue", "Gamma(2, 1)" = "lightcoral")) +
scale_color_manual(values = c("Gamma(2, 0.5)" = "#6699CC", "Gamma(2, 1)" = "red"))
########### Simulating Half-normal distribution as prior ###############
# Define the parameters for the Half-Normal distribution
sigma <- 2.5
# Create a sequence of x values from 0 to a reasonable upper limit
x <- seq(0, 10, length.out = 100)
# Compute the density of the Half-Normal distribution for each x value
# Half-Normal is a truncated Normal distribution with mean = 0 and sd = sigma
y <- (2 / (sigma * sqrt(2 * pi))) * exp(-x^2 / (2 * sigma^2))
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Half-Normal distribution
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Half-Normal Distribution (sigma = 2.5)",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating for log-normal distribution as prior ###############
# Define the parameters for the Log-Normal distribution
mu <- -1.1931
sigma <- 1
# Create a sequence of x values
x <- seq(0, 5, length.out = 100)
# Compute the density of the Log-Normal distribution for each x value
y <- dlnorm(x, meanlog = mu, sdlog = sigma)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Log-Normal distribution
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Log-Normal Distribution (mean = 0.5, fat tail)",
subtitle = "params: mu= -1.1931, sigma=1",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating for normal distribution as prior ###############
# Define the parameters for the Normal distribution
mean <- 0
sd <- 2.5
# Create a sequence of x values
x <- seq(-10, 10, length.out = 100)
# Compute the density of the Normal distribution for each x value
y <- dnorm(x, mean = mean, sd = sd)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Normal distribution
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Normal Distribution (mean = 0, sd = 2.5)",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating for t-student distribution as prior ###############
# Define the parameters for the Student's t-distribution
df <- 3 # Degrees of freedom
mean <- 0 # Location parameter (mean)
scale <- 2 # Scale parameter
# Create a sequence of x values
x <- seq(-10, 10, length.out = 100)
# Compute the density of the Student's t-distribution for each x value
y <- dt((x - mean) / scale, df = df) / scale
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Student's t-distribution
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Student's t-Distribution (df = 3, mean = 0, scale = 2)",
x = "x",
y = "Density") +
theme_minimal()
########### Simulating for cauchy distribution as prior ###############
# Define the parameters for the Cauchy distribution
location <- 0 # Location parameter (median)
scale <- 2.5 # Scale parameter
# Create a sequence of x values
x <- seq(-10, 10, length.out = 1000)
# Compute the density of the Cauchy distribution for each x value
y <- dcauchy(x, location = location, scale = scale)
# Create a data frame for plotting
data <- data.frame(x = x, y = y)
# Plot the Cauchy distribution
ggplot(data, aes(x = x, y = y)) +
geom_area(fill = "lightblue", alpha = 0.5) +
geom_line(color = "#6699CC") +
labs(title = "Cauchy Distribution (location = 0, scale = 2.5)",
x = "x",
y = "Density") +
theme_minimal()