Thanks that worked well.
Now If I may asked about student t copula please :
Here is my is simulation in R
library(copula)
# Set the copula parameter
rho <- 0.70
nu <- 3.84 # Degrees of freedom for the Student's t copula
# Create the Student's t copula object
u <- rCopula(500,tCopula(dim=2,rho,df=nu))
plot(u[,1],u[,2],pch='.',col='blue')
cor(u,method='spearman')
# Plot the simulated data
plot(x, y, pch = 16, xlab = "x", ylab = "y", main = "Simulated Data")
# Compute and print the sample correlation
correlation <- cor(x, y)
print(paste("Sample Correlation:", correlation))
# Plot the simulated data
plot(x, y, pch = 16, xlab = "x", ylab = "y", main = "Simulated Data")
data <- list(
N = length(x),
x = (u[, 1]),
y = u[, 2]
)
This is the Stan code for student t copula:
functions {
real t_copula(vector u, vector v, real rho, real nu) {
int N = num_elements(u);
vector[N] t1 = inv_Phi(u); // Inverse CDF of the first marginal (standard normal)
vector[N] t2 = inv_Phi(v); // Inverse CDF of the second marginal (standard normal)
real log_det_J = sum(log1p(square(t1)) + log1p(square(t2))); // Calculate the log determinant of the Jacobian matrix
real log_pdf = lgamma((nu + 2.0) / 2.0) - lgamma(nu / 2.0) - (N / 2.0) * log(nu * pi())
- 0.5 * (nu + 2.0) * log1p(rho * rho / (nu - 2.0)) - log_det_J;
return log_pdf;
}
}
data {
int<lower=0 N; // Number of observations
vector[N] x; // First variable
vector[N] y; // Second variable
}
parameters {
real<lower=-1, upper=1 rho; // Copula parameter (correlation)
real<lower=2 nu; // Degrees of freedom parameter
}
model {
// Priors
rho ~ uniform(-1, 1);
nu ~ gamma(2, 0.5); // Adjust the shape and scale parameters as needed
// Likelihood
target += t_copula(x, y, rho, nu);
}
Once again I am trying to recover rho, but its way off, any suggestion @spinkney .
Thanks in advance for your suggestion.
Antony.
edit: Admin formatting. Code formatting can be enabled by starting the code block with ```stan or ```r and closing the code block with ```