4
$\begingroup$

I have created a predictive model that outputs a predictive density. I used 1000 rolling windows to estimate the model and predict one step ahead in each window. I collected the 1000 predictions and compared them to the actual realizations. I used several diagnostic tests, among them Kolmogorov-Smirnov. I saved the $p$-value of the test.

I did the same for multiple time series. Then I looked at all of the $p$-values from the different series. I found that they are 0.440, 0.579, 0.848, 0.476, 0.753, 0.955, 0.919, 0.498, 0.997. At first I was quite happy that they are much larger than 0.010, 0.050 or 0.100 (to use the standard cut-off values). But then a colleague of mine pointed out that the $p$-values should be distributed as $\text{Uniform}[0,1]$ under the null of correct predictive distribution, and so I should perhaps not be so happy.

On the one hand, the colleague must be right; the $p$-values should ideally be uniformly distributed. On the other hand, I have found that my model predicts "better" than the true model normally would; the discrepancy between the predicted density and the realized density is less than one would normally expect between the true density and the realized density. This could be an indication of overfitting if I were evaluating my model in-sample, but the model has been evaluated out of sample. What does this tell me? Should I be concerned with a diagnostic test's $p$-values being too high?

You could say this is just a small set of $p$-values (just 8 of them) so anything could happen, and you might be right. However, suppose I have a larger set of $p$-values that are closer to 1 than uniformly distributed; is that a problem? What does that tell me?


Update: I have adapted the code by @jbowman and played with different values of $k$ and $n$ to arrive at the following histograms of $p$-values from the Kolmogorov-Smirnov test. The relationship between the shape of the histogram and $k$ and $n$ seems nontrivial.

  • For small $n$ (columns on the left), increasing $k$ (going down the rows) appears to push the $p$-values towards $0$.
  • For large $n$ (columns on the right), increasing $k$ (going down the rows) appears to push the $p$-values towards $1$.
  • For cases in between such as $n=30$, I do not see a clear trend.

(The number of simulation runs is $1000$ for each histogram.)

enter image description here

You can experiment for yourself by running the code below:

m=1e3 # number of simulation runs
n=1e3 # size of training sample (length of rolling window)
k=1e3 # size of test sample / number of rolling windows within one simulation run

ks_value <- rep(NA, m)
p_value  <- rep(NA, k)

# Runs 45  seconds for n=1e3, m=1e3
# Runs 5.5 minutes for n=1e4, m=1e3
print(paste0(Sys.time()," start"))
for (j in 1:m) {
   if(j%%100==0) print(paste0(Sys.time()," simulation run no. ",j," of ",m))
   set.seed(j); x <- rnorm(n+k)
   for (i in 1:k) {
      xbar       <- mean(x[i:(n+i-1)])   
      s_d        <- sd  (x[i:(n+i-1)])
      p_value[i] <- pnorm(x[n+i], xbar, s_d)
   }
   ks_value[j]   <- ks.test(p_value, punif)$p.value
}; print(paste0(Sys.time()," end"))

hist(ks_value)

It is still a puzzle for me why this is happening. I can see that the $p$-values of observations are autocorrelated across windows (within a simulation run) because of the overlap of the training windows that causes the estimated mean and estimated standard deviation to be autocorrelated. However, I do not see why this should skew their unconditional distribution towards $1$. Actually, deliberately setting the estimated mean and estimated standard deviation to be fixed across windows (within a simulation run) yields KS test's $p$-values that look fairly uniform when collected across the simulation runs. This appears to imply that the problem is caused by data leakage, i.e. test observations becoming training observations as the window rolls.
But why/how exactly does that cause nonuniformity?

$\endgroup$
11
  • $\begingroup$ How are you using the model, just for prediction? $\endgroup$
    – Dave
    Commented Jun 14, 2021 at 14:19
  • $\begingroup$ @Dave, yes, purely for prediction. $\endgroup$ Commented Jun 14, 2021 at 14:19
  • 1
    $\begingroup$ Those p-values aren't independent, though, are they? If I understand correctly, they come from overlapping windows. I think under such circumstances, they shouldn't be uniformly distributed. If you happen to have an especially good fit, they could all be closer to $1.0$. You might check carefully to ensure there hasn't been any 'data leakage' between the training set and the validation set, though. $\endgroup$ Commented Aug 10, 2021 at 14:35
  • $\begingroup$ Hmmm, I should have read @jbowman's answer before commenting... I can delete these, if you want. $\endgroup$ Commented Aug 10, 2021 at 14:36
  • $\begingroup$ @gung-ReinstateMonica, I appreciate your input; do not delete. I understand that these $p$-values are autocorrelated (and that is confirmed by plotting their ACF), but I do not see why they should be biased. Autocorrelation and bias seem tangential to me. Do you have any explanation? $\endgroup$ Commented Aug 10, 2021 at 14:42

1 Answer 1

5
+50
$\begingroup$

That "rolling windows" in your second sentence raises the question of whether the 1000 predictive densities are actually independent of each other... with obvious implications for the validity of the diagnostic tests. Let's perform an experiment, which of course will not match your problem exactly.

We will generate independent 1100 standard normal variates, then compute a rolling sample mean and standard deviation based on windows of size 100. This will give us 1000 estimates of the distributions of $x_{i+1}$ for $i = 101, \dots, 1100$. We then calculate the predictive cumulative density function $p_{i+1}$ of each of the $x_{i+1}$, and compare the resulting collection of 1000 of these with a $U(0,1)$ distribution using the Kolmogorov-Smirnov test. (Given our sample size of 100, the use of a Normal distribution for predictive purposes seems reasonable.) We repeat the entire exercise 100 times and plot a histogram of the resulting K-S test p-values.

ks_value <- rep(0, 100)
p_value <- rep(0, 1000)

for (j in 1:100) {
   x <- rnorm(1100)
   for (i in 1:1000) {
      xbar <- mean(x[i:(i+99)])   
      s_d <- sd(x[i:(i+99)])
      p_value[i] <- pnorm(x[i+100], xbar, s_d)
   }
   ks_value[j] <- ks.test(p_value, punif)$p.value
}

with resultant histogram:

enter image description here

which certainly seems to match the pattern of your observed p-values better than a Uniform distribution!

Changing our code allows us to verify that in the independent sample case we get a reasonable collection of p-values from the K-S test:

for (j in 1:100) {
   for (i in 1:1000) {
      x <- rnorm(101)
      xbar <- mean(x[1:100])   
      s_d <- sd(x[1:100])
      p_value[i] <- pnorm(x[101], xbar, s_d)
   }
   ks_value[j] <- ks.test(p_value, punif)$p.value
}

enter image description here

which, given only 100 p-values, does not appear unreasonably far from a Uniform distribution.

One mechanism that would cause this result is as follows. Consider a given $x_{i+1}$ and the associated predictive distribution. If $x_{i+1}$ is "large", its p-value will also be large. As the rolling window moves forward, though, that "large" value will cause the sample mean and standard deviation to, all other things being equal, be slightly larger than they would have been had $x_{i+1}$ been "small". This will reduce the probability of obtaining a large p-value from the comparison between $x_{i+2}$ and its associated predictive distribution, working in a way similar to that of antithetic variables in simulation. Similarly, if $x_{i+1}$ is very near the sample mean (relative to the size of the standard deviation), the sample standard deviation will become smaller for the comparison between $x_{i+2}$ and its associated distribution, increasing the chances for a more extreme p-value. Consequently, the distribution of p-values from the predictive distribution tests would seem to be more uniform than they ought to be given their sample size, although with a large enough number of replications no doubt their non-uniformity would manifest itself.

We can see these effects in the following plots. We generate 10 standard normal variates, with the 11th being fixed at either 2.0 or 0.2. We calculate the p-value of the 11th observation and the 12th observation (given a window width of 10) and repeat 100,000 times. We then plot the histograms of the four sets of p-values.

Our code:

p_values <- matrix(0, nrow=100000, ncol=2)

for (i in 1:100000) {
   x <- c(rnorm(10), 2, rnorm(1))

   p_values[i,1] <- pt((x[11] - mean(x[1:10])) / (sqrt(1 + 1/10) * sd(x[1:10])), df=9)
   p_values[i,2] <- pt((x[12] - mean(x[2:11])) / (sqrt(1 + 1/10) * sd(x[2:11])), df=9)
}

hist(p_values[,1], main="Histogram of p-values for x[11] = 2", xlab = "p-values")
hist(p_values[,2], main="Histogram of p-values for x[12] | x[11] = 2", xlab = "p-values")

First, the case where $x_{11} = 2.0$:

enter image description here
enter image description here

And again, for the case where $x_{11} = 0.2$:

enter image description here enter image description here

It's also important to note that the effects in the second of each of the two pairs of plots persist for the entire length of the window.

$\endgroup$
7
  • $\begingroup$ Thank you! Very interesting. The simulation result is persuasive, but I am still digesting the logic of it as laid out in the last paragraph. E.g. that "large" value will cause the sample mean and standard deviation to <...> be slightly larger <...>. This will reduce the probability of obtaining a large p-value: will the sample mean and the sample standard deviation not be working in opposite directions? Higher mean --> higher p-value, higher sd --> lower p-value. Perhaps the latter dominates? But this is not the main question. I am struggling with the intuition here. $\endgroup$ Commented Aug 8, 2021 at 6:05
  • $\begingroup$ Given the rolling window approach, I would expect the predictive densities to be autocorrelated but not biased. The former means the effective sample size of the KS test may be (much) smaller than the nominal sample size, making the test less powerful. The latter means I do not expect deviations from uniformity towards larger values in the [0,1] interval or, for that matter, towards smaller values in the interval. Your explanation seems to suggest a large p-value will be followed by a number of small p-values and vice versa (negative autocorrelation for multiple lags) but.. $\endgroup$ Commented Aug 8, 2021 at 6:17
  • $\begingroup$ ...how does that translate into bias of p-values towards one end of the interval? Or is the test sample too small so that even without bias but in presence of autocorrelation up to long lags we tend to observe a long sequence (that constitutes almost the entire test sample) of either small values or large values? $\endgroup$ Commented Aug 8, 2021 at 9:47
  • $\begingroup$ I should really limit my verbal descriptions of mathematical effects, I'm not good enough at it to get away with it all that often! I will work out a more precise and, hopefully clear, explanation. $\endgroup$
    – jbowman
    Commented Aug 8, 2021 at 23:57
  • $\begingroup$ Still struggling with the problem. I have run some simulations following your code and varying the hyperparameters (window size and the number of training windows). I posted the findings as an update to my original question. $\endgroup$ Commented Aug 10, 2021 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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