-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgeneralized_beta.R
78 lines (54 loc) · 1.73 KB
/
generalized_beta.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
library(betafunctions)
library(cmdstanr)
# screw around to make a generalized beta distribution in Stan
# make a density function first in R and check with betafunctions
dbetagen <- function(x, l, u, alpha, beta) {
term1 <- (alpha-1)*log(x - l)
term2 <- (beta - 1)*log(u - x)
term3 <- (alpha + beta - 1)*log(u - l)
term1 + term2 - term3 - base::lbeta(alpha,beta)
}
log_beta_function <- function(x, alpha, beta, l, u) {
bfunc <- beta(alpha, beta)
log_bfunc <- log(bfunc) # Logarithm of the beta function
term1 <- (alpha - 1) * log(x - l) # Logarithm of (x - l)^(alpha - 1)
term2 <- (beta - 1) * log(u - x) # Logarithm of (u - x)^(beta - 1)
term3 <- (alpha + beta - 1) * log(u - l) # Logarithm of (u - l)^(alpha + beta - 1)
# Sum the logarithms instead of multiplying the terms
result <- term1 + term2 - term3 - log_bfunc
return(result)
}
dbetagen(x = 0.5, l = 0.25, u = 0.75, alpha = 5, beta = 3)
log(dBeta.4P(x = 0.5, l = 0.25, u = 0.75, alpha = 5, beta = 3))
log_beta_function(x=0.5, alpha=5,beta=3, l=0.25, u=0.75)
# generate data from 4 parameter Beta family, then fit and recover from model
y <- rBeta.4P(500, -1, 1, 0.1,1000)
y2 <- rBeta.4P(500, -1, 1, 1000,0.1)
hist(y2)
this_mod <- "
functions{
//PDF for 4-parameter beta family
real genbeta_lpdf(real y, real alpha, real beta, real gamma, real delta) {
real x = (y - gamma) / delta;
return (alpha - 1) * log(x) + (beta - 1) * log1m(x) - log(delta) - lbeta(alpha, beta);
}
}
}
data {
int N;
real y[N];
real upb;
real lbd;
real scale_prior;
real shape_prior;
}
parameters {
real scale;
real shape;
}
model {
scale ~ exponential(1);
shape ~ exponential(1);
target += genbeta_lpdf(y|scale, shape, lbd, lbd + ubd);
}
"