8
$\begingroup$

Let $d$ a positive integer How to generate a sample of $n$ random variables with a multivariate uniform distribution on the cube $[a,b]^d$ in R?

I don't know what to do. I know that I need a covariance matrix for the random vectors that I will generate...

$\endgroup$
2
  • 6
    $\begingroup$ Why do you think you need a covariance matrix? $\endgroup$ Oct 17, 2017 at 15:10
  • 2
    $\begingroup$ If your real task is about discovering the covariance matrix, by using trials, then you may be interested in a direct analytical solution: the variance-covariance matrix for a uniform variable, in Euclidean space, on the cube $[a,b]^d$ is $$cov(x_i,x_j) = \frac{1}{12}(a-b)^2 \cdot \mathbf{I}$$ $\endgroup$ Oct 18, 2017 at 16:56

3 Answers 3

20
$\begingroup$

It depends a little bit on the terminology, but usually multivariate uniform refers to a distribution where every point in $[a,b]^d$ is equally likely. Hence, the dimensions are independent, and you can draw uniformly between $[a,b]$ d times individually to get a sample from the multivariate uniform.

If you don't want the dimensions to be independent, it might be worth looking into Copulas

$\endgroup$
2
$\begingroup$

Following up on Sam's answer:

samps <- replicate(n, runif(d,a,b)) # draw samples
cov(t(samps))  # get the sample covariance matrix
$\endgroup$
0
$\begingroup$

Following up on the second part Sam's answer:

library(copula)
set.seed(2019)
d <- 2
n <- 1000

indep.cop <- indepCopula(d)
sample    <- rCopula(n, indep.cop)
chisq.test(sample)

#        Pearson's Chi-squared test

# data:  sample
# X-squared = 362.15, df = 1998, p-value = 1

# Warning message:
# In chisq.test(sample) : Chi-squared approximation may be incorrect

Because the p-value is 1, it’s not unreasonable to assume that the dimensions are independent.

$\endgroup$
1
  • $\begingroup$ @RichardHardy, yes. Fixed $\endgroup$
    – Nick
    Oct 2, 2020 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.