# Can data be created using Monte Carlo Simulation

I am aware that Monte-Carlo Simulation is used for making accurate assumptions by introducing randomness. But can it be used to synthesize or create a dataset? If yes, can someone share an example?

• A dataset of what? Nov 18, 2022 at 10:06

YES

I’ll give an example in R.

set.seed(2022)

# Set sample size
#
N <- 100

# Declare some regression feature
#
x <- seq(1, 100, 1)

# Define the outcome variable, using a
# random normal error term (this is
# the Monte Carlo part)
#
y <- 1 - x + rnorm(N)


Now your have a synthetic data set and can run a regression, for instance.

An example of where this might be useful is if you develop a new hypothesis test for a regression coefficient and want to see how it performs when the null hypothesis is true. Thus, you loop through $$1000$$ regression simulations like this and hypothesis test each time, collecting the p-value of each hypothesis test.

set.seed(2022)
N <- 100
R <- 1000
ps <- rep(NA, R)
x <- seq(1, N, 1)
for (i in 1:R){

y <- 1 + rnorm(N) # zero slope coefficient
L <- lm(y ~ x)
ps[i] <- summary(L)\$coef[2, 4] # extract p-value of t-testing slope

}

plot(
ps,
ecdf(ps)(ps),
xlab = "p-value",
ylab = "quantile"
)
abline(0, 1)


If you were the inventor of the t-test used here, you would see that your test gives uniform p-values under the null hypothesis of zero slope (exactly the desired behavior). The library TensorFlow Probability is designed for this purpose. In fact, the first example currently at the web site involves the creation of synthetic data which is then used for a regression example:

import tensorflow as tf
import tensorflow_probability as tfp

# Pretend to load synthetic data set.
features = tfp.distributions.Normal(loc=0., scale=1.).sample(int(100e3))
labels = tfp.distributions.Bernoulli(logits=1.618 * features).sample()

# Specify model.
model = tfp.glm.Bernoulli()

# Fit model given data.
coeffs, linear_response, is_converged, num_iter = tfp.glm.fit(
model_matrix=features[:, tf.newaxis],
response=tf.cast(labels, dtype=tf.float32),
model=model)
# ==> coeffs is approximately [1.618] (We're golden!)