How Many Random Seeds ?



Reproducibility in Machine Learning and Deep Reinforcement Learning in particular has become a serious issue in the recent years. Reproducing an RL paper can turn out to be much more complicated than you thought, see this blog post about lessons learned from reproducing a deep RL paper. Indeed, codebases are not always released and scientific papers often omit parts of the implementation tricks. Recently, Henderson et al. conducted a thorough investigation of various parameters causing this reproducibility crisis [Henderson et al., 2017]. They used trendy deep RL algorithms such as DDPG, ACKTR, TRPO and PPO with OpenAI Gym popular benchmarks such as Half-Cheetah, Hopper and Swimmer to study the effects of the codebase, the size of the networks, the activation function, the reward scaling or the random seeds. Among other results, they showed that different implementations of the same algorithm with the same set of hyperparameters led to drastically different results.

Perhaps the most surprising thing is this: running the same algorithm 10 times with the same hyper-parameters using 10 different random seeds and averaging performance over two splits of 5 seeds can lead to learning curves seemingly coming from different statistical distributions. Then, they present this table:

Figure 1: Number of trials reported during evaluation in various works, from [Henderson et al., 2017].

This table shows that all the deep RL papers reviewed by Henderson et al. use less than 5 seeds. Even worse, some papers actually report the average of the best performing runs! As demonstrated in Henderson et al., these methodologies can lead to claim that two algorithms performances are different when they are not. A solution to this problem is to use more random seeds, to average more different trials to obtain a more robust measure of your algorithm performance. OK, but how many more? Should I use 10, should I use 100 as in [Mania et al, 2018]? The answer is, of course, it depends.

If you read this blog, you must be in the following situation: you want to compare the performance of two algorithms to determine which one performs best in a given environment. Unfortunately, two runs of the same algorithm often yield different measures of performance. This might be due to various factors such as the seed of the random generators (called random seed or seed thereafter), the initial conditions of the agent, the stochasticity of the environment, etc.

Part of the statistical procedures described in this article are available on Github here. The article is available on ArXiv here.

Definition of the statistical problem

The performance of an algorithm can be modeled as a random variable and running this algorithm in an environment results in a realization . Repeating the procedure times, you obtain a statistical sample . A random variable is usually characterized by its mean and its standard deviation, noted . Of course, you do not know what are the values of and . The only thing you can do is to compute their estimations and :

where is called the empirical mean, and is called the empirical standard deviation. The larger the sample size , the more confidence you can be in the estimations.

Here, two algorithms with respective performances and are compared. If and follow normal distributions, the random variable describing their difference also follows a normal distribution with parameters and . In this case, the estimator of the mean of is and the estimator of is . The effect size can be defined as the difference between the mean performances of both algorithms: .

Testing for a difference between the performances of two algorithms ( and ) is mathematically equivalent to testing a difference between their difference and 0. The second point of view is considered from now on. We draw a sample from by subtracting two samples and obtained from and .

Example 1

To illustrate the concepts developed in this article, let us take two algorithms ( and ) and compare them on the Half-Cheetah environment from the OpenAI Gym framework. The actual algorithms used are not so important here, and will be revealed later. First, we run a preliminary study with random seeds for each and plot the results in Figure 2. This figure shows the average learning curves, with the confidence interval. Each point of a learning curve is the average cumulated reward over evaluation episodes. The measure of performance of an algorithm is the average performance over the last points (i.e. last evaluation episodes). From the figure, it seems that performs better than . Moreover, the confidence intervals do not overlap much at the end. Of course, we need to run statistical tests before drawing any conclusion.

Figure 2: Algo1 versus Algo2 on Half-Cheetah. Mean and confidence intervals for 5 seeds

</div>

Comparing performances with a difference test

In a difference test, statisticians define the null hypothesis and the alternate hypothesis . assumes no difference whereas assumes one:

  • :
  • :

These hypothesis refers to the two-tail case. When you have an a-priori on which algorithm performs best, (let us say ), you can use the one-tail version:

  • :
  • :

At first, a statistical test always assumes the null hypothesis. Once a sample is collected from , you can estimate the probability (called -value) of observing data as extreme, under the null hypothesis assumption. By extreme, one means far from the null hypothesis ( far from ). The -value answers the following question: how probable is it to observe this sample or a more extreme one, given that there is no true difference in the performances of both algorithms? Mathematically, we can write it this way for the one-tail case:

and this way for the two-tail case:

When this probability becomes really low, it means that it is highly improbable that two algorithms with no performance difference produced the collected sample . A difference is called significant at significance level when the -value is lower than in the one-tail case, and lower than in the two tail case to account for the two sided test. Usually is set to or lower. In this case, the low probability to observe the collected sample under hypothesis results in its rejection. Note that a significance level still results in chance out of to claim a false positive, to claim that there is a true difference when there is not.

Another way to see this, is to consider confidence intervals. Two kinds of confidence intervals can be computed:

  • : The confidence interval for the mean of the difference given a sample characterized by and .
  • : The confidence interval for any realization of under (assuming ).

Having that does not include is mathematically equivalent to a -value below . In both cases, it means there is less than chance that under . When does not include , we are also confident that , without assuming . Proving one of these things leads to conclude that the difference is significant at level .

Two types of errors can be made in statistics:

  • The type-I error rejects when it is true, also called false positive. This corresponds to claiming the superiority of an algorithm over another when there is no true difference. Note that we call both the significance level and the probability of type-I error because they both refer to the same concept. Choosing a significance level of enforces a probability of type-I error , under the assumptions of the statistical test.
  • The type-II error fails to reject when it is false, also called false negative. This corresponds to missing the opportunity to publish an article when there was actually something to be found.

Important:

  • :
  • :
  • In the two-tail case, the null hypothesis is . The alternative hypothesis is
  • -value .
  • A difference is said statistically significant when a statistical test passed. One can reject the null hypothesis when 1) -value ; 2) does not contain ; 3) does not contain .
  • statistically significant does not refer to the absolute truth. Two types of error can occur. Type-I error rejects when it is true. Type-II error fails to reject when it is false.

Select the appropriate statistical test

You must decide which statistical tests to use in order to assess whether the performance difference is significant or not. As recommended in [Henderson et al., 2017], the two-sample t-test and the bootstrap confidence interval test can be used for this purpose. Henderson et al. also advised for the Kolmogorov-Smirnov test, which tests if two samples comes from the same distribution. This test should not be used to compare RL algorithms because it is unable to prove any order relation.

T-test and Welch’s t-test

We want to test the hypothesis that two populations have equal means (null hypothesis ). A 2-sample t-test can be used when the variances of both populations (both algorithms) are assumed equal. However, this assumption rarely holds when comparing two different algorithms (e.g. DDPG vs TRPO). In this case, an adaptation of the 2-sample t-test for unequal variances called Welch’s -test should be used. -tests make a few assumptions:

  • The scale of data measurements must be continuous and ordinal (can be ranked). This is the case in RL.
  • Data is obtained by collecting a representative sample from the population. This seem reasonable in RL.
  • Measurements are independent from one another. This seems reasonable in RL.
  • Data is normally-distributed, or at least bell-shaped. The normal law being a mathematical concept involving infinity, nothing is ever perfectly normally distributed. Moreover, measurements of algorithm performances might follow multi-modal distributions.

Under these assumptions, one can compute the -statistic and the degree of freedom for the Welch’s -test as estimated by the Welch–Satterthwaite equation, such as:

with ; the empirical standard deviations of the two samples, and the sample size (same for both algorithms). The -statistics are assumed to follow a -distribution, which is bell-shaped and whose width depends on the degree of freedom. The higher this degree, the thinner the distribution.

Figure 3 helps making sense of these concepts. It represents the distribution of the -statistics corresponding to , under (left distribution) and under (right distribution). assumes , the distribution is therefore centered on 0. assumes a (positive) difference , the distribution is therefore shifted by the -value corresponding to , . Note that we consider the one-tail case here, and test for a positive difference.

A -distribution is defined by its probability density function (left curve in Figure 3, which is parameterized by . The cumulative distribution function is the function evaluating the area under from to . This allows to write:

Figure 3: Representation of H0 and Ha under the t-test assumptions. Areas under the distributions represented in red, dark blue and light blue correspond to the probability of type-I error alpha, type-II error beta and the statistical power 1-beta respectively.

In Figure 3, represents the critical -value to satisfy the significance level in the one-tail case. When , -value . When , the -value is lower than and the test rejects . On the other hand, when is lower than , the -value is superior to and the test fails to reject . As can be seen in the figure, setting the threshold at might also cause an error of type-II. The rate of this error () is represented by the dark blue area: under the hypothesis of a true difference (under , right distribution), we fail to reject when is inferior to . can therefore be computed mathematically using the :

Using the translation properties of integrals, we can rewrite as:

The procedure to run a Welch’s -test given two samples is:

  • Computing the degree of freedom and the -statistic based on , , and .
  • Looking up the value for the degree of freedom in a t-table or by evaluating the inverse of the function in .
  • Compare the -statistic to . The difference is said statistically significant ( rejected) at level when .

Note that does not mean there is no difference between the performances of both algorithms. It only means there is not enough evidence to prove its existence with confidence (it might be a type-II error). Noise might hinder the ability of the test to detect the difference. In this case, increasing the sample size could help uncover the difference.

Selecting the significance level of the -test enforces the probability of type-I error to . However, Figure 3 shows that decreasing this probability boils down to increasing , which in turn increases the probability of type-II error . One can decrease while keeping constant by increasing the sample size . This way, the estimation of gets more accurate, which translates in thinner distributions in the figure, resulting in a smaller . The next section gives standard guidelines to select so as to meet requirements for both and .

Bootstrapped confidence intervals

Bootstrapped confidence interval is a method that does not make any assumption on the distributions of performance differences. It estimates the confidence intervals by re-sampling among the samples actually collected and by computing the mean of each generated sample.

Given the true mean and standard deviation of a normal distribution, a simple formula gives the confidence interval. But here, we consider an unknown distribution (the distribution of performances for a given algorithm). As we saw above, the empirical mean is an unbiased estimate of its true mean, but how do we compute a confidence interval? One solution is to use the bootstrap principle.

Let us say we have a sample of measures (performance measures in our case), where is the sample size. The empirical bootstrap sample is obtained by sampling with replacement inside the original sample. This bootstrap sample is noted and has the same number of measurements . The bootstrap principle then says that, for any statistics computed on the original sample and computed on the bootstrap sample, variations in are well approximated by variations in . More explanations and justifications can be found in this document from MIT. You can therefore approximate variations of the empirical mean (let’s say its range), by variations of the bootstrapped samples.

The computation would look like this:

  • Generate bootstrap samples of size from the original sample of and samples from from the original sample of .
  • Compute the empirical mean for each sample: and
  • Compute the differences
  • Compute the bootstrapped confidence interval at . This is basically the range between the and percentiles of the vector (e.g. for , the range between the and the percentiles).

The number of bootstrap samples should be chosen large (e.g. ). If the confidence interval bounds does not contain , it means that you are confident at % that the difference is either positive (both bounds positive) or negative (both bounds negative). You just found a statistically significant difference between the performances of your two algorithms. You can find a nice implementation of this here.

Example 1 (continued) Here, the type-I error requirement is set to . Running the Welch’s -test and the bootstrap confidence interval test with two samples of seeds each leads to a -value of and a bootstrap confidence interval such that . Since the -value is below the significance level and the confidence interval does not include , both test passed. This means both tests found a significant difference between the performances of and with a confidence. There should have been only chance to conclude a significant difference if it did not exist. In fact, we did encounter a type-I error. I know that for sure because:

Algo 1 and Algo 2 are the exact same algorithm

They are both the canonical implementation of DDPG [Lillicrap et al., 2015]. The codebase can be found on this repository. This means that was the true hypothesis, there is no possible difference in the true means of the two algorithms. Our first conclusion was wrong, we committed a type-I error, rejecting when it was true. In our case, we selected the two tests so as to set the type-I error probability to . However, statistical tests often make assumptions, which results in wrong estimations of the probability of the type-I error. We will see in the last section that the false positive rate was strongly under-evaluated.

Important:

  • -tests assume -distributions of the -values. Under some assumptions, they can compute analytically the -value and the confidence interval at level .
  • The Welch’s -test does not assume both algorithms have equal variances but the -test does.
  • The bootstrapped confidence interval test does not make assumptions on the performance distribution and estimates empirically the confidence interval at level .
  • Selecting a test with a significance level enforces a type-I error when the assumptions of the test are verified.

The theory: power analysis for the choice of the sample size

We saw that was enforced by the choice of the significance level in the test implementation. The second type of error must now be estimated. is the probability to fail to reject when is true. When the effect size and the probability of type-I error are kept constant, is a function of the sample size . Choosing so as to meet requirements on is called statistical power analysis. It answers the question: what sample size do I need to have chance to detect an effect size , using a test with significance level ? The next paragraphs present guidelines to choose in the context of a Welch’s -test.

As we saw above, can be analytically computed as:

where is the cumulative distribution function of a -distribution centered on , is the critical value for significance level and is the -value corresponding to an effect size . In the end, depends on , , (, ) the empirical standard deviations computed on two samples and the sample size .

Example 2 To illustrate, we compare two DDPG variants: one with action perturbations () [Lillicrap et al., 2015], the other with parameter perturbations () [Plappert et al., 2017]. Both algorithms are evaluated in the Half-Cheetah environment from the OpenAI Gym framework.

Step 1 - Running a pilot study

To compute , we need estimates of the standard deviations of the two algorithms (). In this step, the algorithms are run in the environment to gather two samples and of size . From there, we can compute the empirical means and standard deviations .

Example 2 (continued) Here we run both algorithms with . We find empirical means and empirical standard deviations for (blue) and (red) respectively. From Figure 4, it seems there is a slight difference in the mean performances . Running preliminary statistical tests at level lead to a -value of for the Welch’s -test, and a bootstrapped confidence interval of for the value of . The Welch’s -test does not reject (-value ) but the bootstrap test does (). One should compute to estimate the chance that the Welch’s -test missed an underlying performance difference (type-II error).

Figure 4: DDPG with action perturbation versus DDPG with parameter perturbation tested in Half-Cheetah. Mean and 95% confidence interval computed over 5 seeds are reported. The figure shows a small difference in the empirical mean performances.

Step 2 - Choosing the sample size

Given a statistical test (Welch’s -test), a significance level (e.g. ) and empirical estimations of the standard deviations of and (), one can compute as a function of the sample size and the effect size one wants to be able to detect.

Example 2 (continued) For in and in , we compute and using the formulas given in Section \ref{sec:ttest}, as well as for each . Finally, we compute the corresponding probability of type-II error using Equation~\ref{eq:beta}. Figure 5 shows the evolution of as a function of for the different . Considering the semi-dashed black line for , we find for : there is chance of making a type-II error when trying to detect an effect . To meet the requirement , should be increased to ().

Figure 5: Evolution of the probability of type-II error as a function of the sample size N for various effect sizes epsilon, when (s1, s2)= (1341, 990) and alpha=0.05. The requirement 0.2 is represented by the horizontal dashed black line.

In our example, we find that was enough to be able to detect an effect size with a Welch’s -test, using significance level and using empirical estimations . However, let us keep in mind that these computations use various approximations () and make assumptions about the shape of the -values distribution.

Step 3 - Running the statistical tests

Both algorithms should be run so as to obtain a sample of size . The statistical tests can be applied.

Example 2 (continued) Here, we take and run both the Welch’s -test and the bootstrap test. We now find empirical means and empirical standard deviations for and respectively. Both tests rejected , with a -value of for the Welch’s -test and a confidence interval for the difference for the bootstrap test. Both tests passed. In Figure 7, plots for and can be compared. With a larger number of seeds, the difference that was not found significant with is now more clearly visible. With a larger number of seeds, the estimate is more robust, more evidence is available to support the claim that outperforms , which translates to tighter confidence intervals represented in the figures. \end{myex}

Figure 7: Performance of DDPG with action perturbation (Algo1) and parameter perturbation (Algo2) with N=5 seeds (left) and N=10 seeds (right). The 95% confidence intervals on the right are smaller, because more evidence is available (N larger). The underlying difference appears when N grows.

Important: Given a sample size , a minimum effect size to detect and a requirement on type-I error the probability of type-II error can be computed. This computation relies on the assumptions of the -test. The sample size should be chosen so as to meet the requirements on .

In practice: influence of deviations from assumptions

Under their respective assumptions, the -test and bootstrap test enforce the probability of type-I error to the selected significance level . These assumptions should be carefully checked, if one wants to report the probability of errors accurately. First, we propose to compute an empirical evaluation of the type-I error based on experimental data, and show that: 1) the bootstrap test is sensitive to small sample sizes; 2) the -test might slightly under-evaluate the type-I error for non-normal data. Second, we show that inaccuracies in the estimation of the empirical standard deviations and due to low sample size might lead to large errors in the computation of , which in turn leads to under-estimate the sample size required for the experiment.

Empirical estimation of the type-I error

Remember, type-I errors occur when the null hypothesis () is rejected in favor of the alternative hypothesis , being correct. Given the sample size , the probability of type-I error can be estimated as follows:

  • Run twice this number of trials () for a given algorithm. This ensures that is true because all measurements come from the same distribution.
  • Get average performance over two randomly drawn splits of size . Consider both splits as samples coming from two different algorithms.
  • Test for the difference of both fictive algorithms and record the outcome.
  • Repeat this procedure times (e.g. )
  • Compute the proportion of time was rejected. This is the empirical evaluation of .

Example 3 We use from Example 2. From available measures of performance, the above procedure is run for in . Figure 8 presents the results. For small values of , empirical estimations of the false positive rate are much larger than the supposedly enforced value .

Figure 8: Empirical estimations of the false positive rate on experimental data (Example 3) when N varies, using the Welch's t-test (blue) and the bootstrap confidence interval test (orange).

In our experiment, the bootstrap confidence interval test should not be used with small sample sizes (). Even in this case, the probability of type-I error () is under-evaluated by the test (). The Welch’s -test controls for this effect, because the test is much harder to pass when is small (due to the increase of ). However, the true (empirical) false positive rate might still be slightly under-evaluated. In this case, we might want to set the significance level to to make sure the true positive rate stays below . In the bootstrap test, the error is due to the inability of small samples to correctly represent the underlying distribution, which impairs the enforcement of the false positive rate to the significance level . Concerning the Welch’s -test, this might be due to the non-normality of our data (whose histogram seems to reveal a bimodal distribution). In Example 1, we used and encountered a type-I error. We can see on the Figure 8 that the probability of this to happen was around for the bootstrap test and above for the Welch’s -test.

Influence of the empirical standard deviations

The Welch’s -test computes -statistics and the degree of freedom based on the sample size and the empirical estimations of standard deviations and . When is low, estimations and under-estimate the true standard deviation in average. Under-estimating leads to smaller and lower , which in turn leads to lower estimations of . Finally, finding lower leads to the selection of smaller sample size to meet requirements. We found this had a significant effect on the computation of . Figure 9 shows the false negative rate when trying to detect effects of size between two normal distributions and . The only difference between both figures is that the left one uses the true values of to compute , whereas the right figure uses (inaccurate) empirical evaluations to compute . We can see that the estimation of standard deviations influences the computation of , and the subsequent choice of an appropriate sample size to meet requirements on . See our paper for further details.

Figure 9: Evolution of the probability of type-II error as a function of the sample size N and the effect size epsilon, when (s1, s2)= (1-error, 1-error) and alpha=0.05. Left: error=0, this is the ideal case. Right: error=0.40, a large error that can be made when evaluating s over n=5 samples. The compared distributions are normal, one is centered on 3, the other on 3+\epsilon.

Important:

  • One should not blindly believe in statistical tests results. These tests are based on assumptions that are not always reasonable.
  • must be empirically estimated, as the statistical tests might underestimate it, because of wrong assumptions about the underlying distributions or because of the small sample size.
  • The bootstrap test evaluation of type-I error is strongly dependent on the sample size. A bootstrap test should not be used with less than samples.
  • The inaccuracies in the estimation of the standard deviations of the algorithms (), due to small sample sizes in the preliminary study, lead to under-estimate the sample size required to meet requirements in type-II errors.

Conclusion

In this post, I detailed the statistical problem of comparing the performance of two RL algorithms. I defined type-I and type-II errors and proposed ad-hoc statistical tests to test for performance difference. Finally, I detailed how to pick the right number of random seeds (your sample size) so as to reach the requirements in terms of type-I and II errors and illustrated the process with a practical example.

The most important part is what came after. We challenged the hypotheses made by the Welch’s -test and the bootstrap test and found several problems. First, we showed significant difference between empirical estimations of the false positive rate in our experiment and the theoretical values supposedly enforced by both tests. As a result, the bootstrap test should not be used with less than samples and tighter significance level should be used to enforce a reasonable false positive rate (). Second, we show that the estimation of the sample size required to meet requirements in type-II error were strongly dependent on the accuracy of (). To compensate the under-estimation of , should be chosen systematically larger than what the power analysis prescribes.

Final recommendations

  • Use the Welch’s -test over the bootstrap confidence interval test.
  • Set the significance level of a test to lower values () so as to make sure the probability of type-I error (empirical ) keeps below .
  • Correct for multiple comparisons in order to avoid the linear growth of false positive with the number of experiments.
  • Use at least samples in the pilot study to compute robust estimates of the standard deviations of both algorithms.
  • Use larger sample size than the one prescribed by the power analysis. This helps compensating for potential inaccuracies in the estimations of the standard deviations of the algorithms and reduces the probability of type-II errors.

Note that I am not a statistician. If you spot any approximation or mistake in the text above, please feel free to report corrections or clarifications.

References

  • Henderson, P., Islam, R., Bachman, P., Pineau, J., Precup, D., & Meger, D. (2017). Deep Reinforcement Learning that Matters. link

  • Mnih, V.; Badia, A. P.; Mirza, M.; Graves, A.; Lillicrap, T.; Harley, T.; Silver, D.; and Kavukcuoglu, K. 2016. Asynchronous methods for deep reinforcement learning. In International Conference on Machine Learning, 1928–1937. link

  • Schulman, J.; Wolski, F.; Dhariwal, P.; Radford, A.; and Klimov, O. 2017. Proximal policy optimization algorithms. link

  • Duan, Y.; Chen, X.; Houthooft, R.; Schulman, J.; and Abbeel, P. 2016. Benchmarking deep reinforcement learning for continuous control. In Proceedings of the 33rd International Conference on Machine Learning (ICML). link

  • Gu, S.; Lillicrap, T.; Ghahramani, Z.; Turner, R. E.; Schölkopf, B.; and Levine, S. 2017. Interpolated policy gradient: Merging on-policy and off-policy gradient estimation for deep reinforcement learning. link

  • Lillicrap, T. P.; Hunt, J. J.; Pritzel, A.; Heess, N.; Erez, T.; Tassa, Y.; Silver, D.; andWierstra, D. 2015. Continuous control with deep reinforcement learning. link

  • Schulman, J.; Levine, S.; Abbeel, P.; Jordan, M.; and Moritz, P. 2015a. Trust region policy optimization. In Proceedings of the 32nd International Conference on Machine Learning (ICML). link

  • Wu, Y.; Mansimov, E.; Liao, S.; Grosse, R.; and Ba, J. 2017. Scalable trust-region method for deep reinforcement learning using kronecker-factored approximation. link

  • Plappert, M., Houthooft, R., Dhariwal, P., Sidor, S., Chen, R. Y., Chen, X., … & Andrychowicz, M. (2017). Parameter space noise for exploration. link

Code

The code is available on Github here.

Paper

The paper can be found on ArXiv here.

Contact

Email: cedric.colas@inria.fr


Subscribe to our Twitter.

-->