Learning k-Modal Distributions via Testing

: A k -modal probability distribution over the discrete domain { 1 ,..., n } is one whose histogram has at most k “peaks” and “valleys.” Such distributions are natural gener-alizations of monotone ( k = 0) and unimodal ( k = 1) probability distributions, which have been intensively studied in probability theory and statistics. In this paper we consider the problem of learning (i. e., performing density estimation of) an unknown k -modal distribution with respect to the L 1 distance. The learning algorithm is given access to independent samples drawn from an unknown k -modal distribution p , and it must output a hypothesis distribution (cid:98) p such that with high probability the total variation distance between p and (cid:98) p is at most ε . Our main goal is to obtain computationally efﬁcient algorithms for this problem that use (close to) an information-theoretically optimal number of samples. We give an efﬁcient algorithm for this problem that runs in time poly ( k , log ( n ) , 1 / ε ) . For k ≤ ˜ O ( log n ) , the number of samples used by our algorithm is very close (within an ˜ O ( log ( 1 / ε )) factor) to being information-theoretically optimal. Prior to this work computationally efﬁcient algorithms were known only for the cases k = 0 , 1 (Birgé 1987, 1997). A novel feature of our approach is that our learning algorithm crucially uses a new algorithm for property testing of probability distributions as a key subroutine. The learning algorithm uses the property tester to efﬁciently decompose the k -modal distribution into k (near-)monotone distributions, which are easier to learn.


Introduction
This paper considers a natural unsupervised learning problem involving k-modal distributions over the discrete domain [n] ={1, . . . , n}. A distribution is k-modal if the plot of its probability density function (pdf) has at most k "peaks" and "valleys" (see Section 2.1 for a precise definition). Such distributions arise both in theoretical (see, e. g., [7,19,6]) and applied (see, e. g., [20,1,13]) research; they naturally generalize the simpler classes of monotone (k = 0) and unimodal (k = 1) distributions that have been intensively studied in probability theory and statistics (see the discussion of related work below).
Our main aim in this paper is to give an efficient algorithm for learning an unknown k-modal distribution p to total variation distance ε, given access only to independent samples drawn from p. As described below there is an information-theoretic lower bound of Ω(k log(n/k)/ε 3 ) samples for this learning problem, so an important goal for us is to obtain an algorithm whose sample complexity is as close as possible to this lower bound. An equally important goal is for our algorithm to be computationally efficient, i. e., to run in time polynomial in the size of its input sample. Our main contribution in this paper is a computationally efficient algorithm that has nearly optimal sample complexity for small (but super-constant) values of k.

Background and relation to previous work
There is a rich body of work in the statistics and probability literatures on estimating distributions under various kinds of "shape" or "order" restrictions. In particular, many researchers have studied the risk of different estimators for monotone (k = 0) and unimodal (k = 1) distributions; see for example the works of [23,26,17,3,4,5], among many others. These and related papers from the probability/statistics literature mostly deal with information-theoretic upper and lower bounds on the sample complexity of learning monotone and unimodal distributions. In contrast, a central goal of the current work is to obtain computationally efficient learning algorithms for larger values of k.
It should be noted that some of the works cited above do give efficient algorithms for the cases k = 0 and k = 1; in particular we mention the results of Birgé [4,5], which give computationally efficient O(log(n)/ε 3 )-sample algorithms for learning unknown monotone or unimodal distributions over [n] respectively. (Birgé [3] also showed that this sample complexity is asymptotically optimal, as we discuss below; we describe the algorithm of [4] in more detail in Section 2.2, and indeed use it as an ingredient of our approach throughout this paper.) However, for these relatively simple k = 0, 1 classes of distributions the main challenge is in developing sample-efficient estimators, and the algorithmic aspects are typically rather straightforward (as is the case in [4]). In contrast, much more challenging and interesting algorithmic issues arise for the general values of k which we consider here. THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570

Our results
Our main result is a highly efficient algorithm for learning an unknown k-modal distribution over [n]: Theorem 1.1. Let p be any unknown k-modal distribution over [n]. There is an algorithm that uses 1 k log(n/k) samples from p, runs for poly(k, log n, 1/ε, log(1/δ )) bit operations, and with probability 1 − δ outputs a (succinct description of a) hypothesis distribution h over [n] such that the total variation distance between p and h is at most ε.
As alluded to earlier, Birgé [3] gave a sample complexity lower bound for learning monotone distributions. The lower bound in [3] is stated for continuous distributions but the arguments are easily adapted to the discrete case; [3] shows that (for ε ≥ 1/n Ω(1) ) 2 any algorithm for learning an unknown monotone distribution over [n] to total variation distance ε must use Ω(log(n)/ε 3 ) samples. By a simple construction which concatenates k copies of the monotone lower bound construction over intervals of length n/k, using the monotone lower bound it is possible to show: Proposition 1.2. Any algorithm for learning an unknown k-modal distribution over [n] to variation distance ε (for ε ≥ 1/n Ω(1) ) must use Ω(k log(n/k)/ε 3 ) samples.
Thus our learning algorithm is nearly optimal in its sample complexity; more precisely, for k ≤ O(log n) (and ε as bounded above), our sample complexity in Theorem 1.1 is asymptotically optimal up to a factor ofÕ(log(1/ε)). Since each draw from a distribution over [n] is a log(n)-bit string, Proposition 1.2 implies that the running time of our algorithm is optimal up to polynomial factors. As far as we are aware, prior to this work no learning algorithm for k-modal distributions was known that simultaneously had poly(k, log n) sample complexity and even running time q(n) for a fixed polynomial q(n) (where the exponent does not depend on k).

Our approach
As mentioned in Section 1.1 Birgé, gave a highly efficient algorithm for learning a monotone distribution in [4]. Since a k-modal distribution is simply a concatenation of k + 1 monotone distributions (first non-increasing, then non-decreasing, then non-increasing, etc.), it is natural to try to use Birgé's algorithm as a component of an algorithm for learning k-modal distributions, and indeed this is what we do.
The most naive way to use Birgé's algorithm would be to guess all possible n k locations of the k "modes" of p. While such an approach can be shown to have good sample complexity, the resulting Ω(n k ) running time is grossly inefficient. A "moderately naive" approach, which we analyze in Section 3.1, is to partition [n] into roughly k/ε intervals each of weight roughly ε/k, and run Birgé's algorithm separately on each such interval. Since the target distribution is k-modal, at most k of the intervals can be non-monotone; Birgé's algorithm can be used to obtain an ε-accurate hypothesis on each monotone interval, and even if it fails badly on the (at most) k non-monotone intervals, the resulting total contribution towards the overall error from those failures is at most O(ε). This approach is much more efficient than the totally naive approach, giving running time polynomial in k, log n, and 1/ε, but its sample complexity turns out to be polynomially worse than the O(k log(n)/ε 3 ) that we are shooting for. (Roughly speaking, this is because the approach involves running Birgé's O(log(n)/ε 3 )-sample algorithm Ω(k/ε) times, so it uses at least k log(n)/ε 4 samples.) Our main learning result is achieved by augmenting the "moderately naive" algorithm sketched above with a new property testing algorithm. Unlike a learning algorithm, a property testing algorithm for probability distributions need not output a high-accuracy hypothesis; instead, it has the more modest goal of successfully (with high probability) distinguishing between probability distributions that have a given property of interest, versus distributions that are far (in total variation distance) from every distribution that has the property. See [16,25,14] for broad overviews of property testing.
We give a property testing algorithm for the following problem: given samples from a distribution p over [n] which is promised to be k-modal, output "yes" (with high probability) if p is monotone and "no" (with high probability) if p is ε-far in total variation distance from every monotone distribution. Crucially, our testing algorithm uses O(k/ε 2 ) samples independent of n for this problem. Roughly speaking, by using this algorithm O(k/ε) times we are able to identify k + 1 intervals that (i) collectively contain almost all of p's mass, and (ii) are each (close to) monotone and thus can be handled using Birgé's algorithm. Thus the overall sample complexity of our approach is (roughly) O(k 2 /ε 3 ) (for the O(k/ε) runs of the tester) plus O(k log(n)/ε 3 ) (for the k runs of Birgé's algorithm), which gives Theorem 1.1 and is very close to optimal for k not too large.

Discussion
Our learning algorithm highlights a novel way that property testing algorithms can be useful for learning. Much research has been done on understanding the relation between property testing algorithms and learning algorithms, see, e. g., [16,18] and the lengthy survey [24]. As Goldreich has noted [15], an often-invoked motivation for property testing is that (inexpensive) testing algorithms can be used as a "preliminary diagnostic" to determine whether it is appropriate to run a (more expensive) learning algorithm. In contrast, in this work we are using property testing rather differently, as an inexpensive way of decomposing a "complex" object (a k-modal distribution) which we do not a priori know how to learn, into a collection of "simpler" objects (monotone or near-monotone distributions) which can be learned using existing techniques. We are not aware of prior learning algorithms that successfully use property testers in this way; we believe that this high-level approach to designing learning algorithms, by using property testers to decompose "complex" objects into simpler objects that can be efficiently learned, may find future applications elsewhere.
We consider discrete probability distributions over [n], which are functions p : , we write p S to denote the conditional distribution over S that is induced by p. We use the notation P for the cumulative distribution function (cdf) corresponding to p, i. e., P : in this case, we say that the point a is a left max point of p. Analogously, a min-interval of p is an interval is either a max-interval or a min-interval (it cannot be both) we say that I is an extreme-interval of p, and a is called a left extreme point of p. Note that any distribution uniquely defines a collection of extreme-intervals (hence, left extreme points). We say that p is k-modal if it has at most k extreme-intervals. We write D n (resp. M k n ) to denote the set of all distributions (resp. k-modal distributions) over [n].
Let p, q be distributions over [n] with corresponding cdfs P, Q. The total variation distance between p and q is d TV (p, q) := max The Kolmogorov distance between p and q is defined as We will also need a more general distance measure that captures the above two metrics as special cases. Fix a family of subsets A over [n]. We define the A-distance between p and q by , the powerset of [n], then the A-distance is identified with the total variation distance, while when A = {[1, j], j ∈ [n]} it is identified with the Kolmogorov distance.) Also recall that the VC-dimension of A is the maximum size of a subset X ⊆ [n] that is shattered by A (a set X is shattered by A if for every Y ⊆ X some A ∈ A satisfies A ∩ X = Y ). Learning k-modal Distributions. Given independent samples from an unknown k-modal distribution p ∈ M k n and ε > 0, the goal is to output a hypothesis distribution h such that with probability 1 − δ we have d TV (p, h) ≤ ε. We say that such an algorithm A learns p to accuracy ε and confidence δ . The parameters of interest are the number of samples and the running time required by the algorithm.

Basic tools
We recall some useful tools from probability theory.
Fix a family of subsets A over [n] of VC-dimension d. The VC inequality states that for m = Ω(d/ε 2 ), with probability 9/10 the empirical distribution p m will be ε-close to p in A-distance. This sample bound is asymptotically optimal.
Uniform convergence. We will also use the following uniform convergence bound: Our second tool, due to Birgé [4], provides a sample-optimal and computationally efficient algorithm to learn monotone distributions to ε-accuracy in total variation distance. Before we state the relevant theorem, we need a definition. We say that a distribution p is δ -close to being non-increasing (resp. nondecreasing) if there exists a non-increasing (resp. non-decreasing) distribution q such that d TV (p, q) ≤ δ . We are now ready to state Birgé's result: Theorem 1). (semi-agnostic learner) There is an algorithm L ↓ with the following performance guarantee: Given m independent samples from a distribution p over [n] which is opt-close to being non-increasing, L ↓ performsÕ m · log n + m 1/3 · (log n) 5/3 bit operations and outputs a (succinct description of a) hypothesis distribution p over [n] that satisfies The aforementioned algorithm partitions the domain [n] in O(m 1/3 · (log n) 2/3 ) intervals and outputs a hypothesis distribution that is uniform within each of these intervals. By taking m = Ω(log n/ε 3 ), one obtains a hypothesis such that E[d TV ( p, p)] ≤ 2 · opt + ε. We stress that Birgé's algorithm for learning non-increasing distributions [4] is in fact "semi-agnostic," in the sense that it also learns distributions that are close to being non-increasing; this robustness will be crucial for us later (since in our final algorithm we will use Birgé's algorithm on distributions identified by our tester, that are close to monotone but not necessarily perfectly monotone). This semi-agnostic property is not explicitly stated in [4] but it can be shown to follow easily from his results. We show how the semi-agnostic property follows from Birgé's results in Appendix A. Let L ↑ denote the corresponding semi-agnostic algorithm for learning non-decreasing distributions.
Our final tool is a routine to do hypothesis testing, i. e., to select a high-accuracy hypothesis distribution from a collection of hypothesis distributions one of which has high accuracy. The need for such a routine arises in several places; in some cases we know that a distribution is monotone, but do not know whether it is non-increasing or non-decreasing. In this case, we can run both algorithms L ↑ and L ↓ and then choose a good hypothesis using hypothesis testing. Another need for hypothesis testing is to "boost confidence" that a learning algorithm generates a high-accuracy hypothesis. Our initial version of the algorithm for Theorem 1.1 generates an ε-accurate hypothesis with probability at least 9/10; by running it O(log(1/δ )) times using a hypothesis testing routine, it is possible to identify an O(ε)-accurate hypothesis with probability 1 − δ . Routines of the sort that we require have been given in, e. g., [12] and [9]; we use the following theorem from [9]: There is an algorithm Choose-Hypothesis p (h 1 , h 2 , ε , δ ) which is given sample access to p, two hypothesis distributions h 1 , h 2 for p, an accuracy parameter ε , and a confidence parameter δ . It makes m = O(log(1/δ )/ε 2 ) draws from p and returns a hypothesis h ∈ {h 1 , h 2 }. If one of h 1 , h 2 has d TV (h i , p) ≤ ε then with probability 1 − δ the hypothesis h that Choose-Hypothesis returns has d TV (h, p) ≤ 6ε .
For the sake of completeness, we describe and analyze the Choose-Hypothesis algorithm in Appendix B.

Learning k-modal distributions
In this section, we present our main result: a nearly sample-optimal and computationally efficient algorithm to learn an unknown k-modal distribution. In Section 3.1 we present a simple learning algorithm with a suboptimal sample complexity. In Section 3.2 we present our main result which involves a property testing algorithm as a subroutine.

Warm-up: A simple learning algorithm
In this subsection, we give an algorithm that runs in time poly(k, log n, 1/ε, log(1/δ )) and learns an unknown k-modal distribution to accuracy ε and confidence δ . The sample complexity of the algorithm is essentially optimal as a function of k (up to a logarithmic factor), but suboptimal as a function of ε, by a polynomial factor.
In the following pseudocode we give a detailed description of the algorithm Learn-kmodal-simple (a precise description appears as Algorithm 1, below); the algorithm outputs an ε-accurate hypothesis THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 with confidence 9/10 (see Theorem 3.3). We explain how to boost the confidence to 1 − δ after the proof of the theorem.
Algorithm Learn-kmodal-simple works as follows: We start by partitioning the domain [n] into consecutive intervals of mass "approximately ε/k." To do this, we draw Θ(k/ε 3 ) samples from p and greedily partition the domain into disjoint intervals of empirical mass roughly ε/k. (Some care is needed in this step, since there may be "heavy" points in the support of the distribution; however, we gloss over this technical issue for the sake of this intuitive explanation.) Note that we do not have a guarantee that each such interval will have true probability mass Θ(ε/k). In fact, it may well be the case that the additive error δ between the true probability mass of an interval and its empirical mass (roughly ε/k) is δ = ω(ε/k). The error guarantee of the partitioning is more "global" in that the sum of these errors across all such intervals is at most ε. In particular, as a simple corollary of the VC inequality, we can deduce the following statement that will be used several times throughout the paper: Since J is a collection of at most d intervals, it is clear that A is a union of at most d intervals. If A d is the family of all unions of at most d intervals, then the right hand side of (3.1) is at most 2 p − p m A d . Since the VC-dimension of A d is 2d, Theorem 2.1 implies that the quantity (3.1) has expected value at most ε/2. The claim now follows by an application of Theorem 2.2 with η = ε/2.
If this step is successful, we have partitioned the domain into a set of O(k/ε) consecutive intervals of probability mass "roughly ε/k." The next step is to apply Birgé's monotone learning algorithm to each interval.
A caveat comes from the fact that not all such intervals are guaranteed to be monotone (or even close to being monotone). However, since our input distribution is assumed to be k-modal, all but (at most) k of these intervals are monotone. Call a non-monotone interval "bad." Since all intervals have empirical probability mass at most ε/k and there are at most k bad intervals, it follows from Fact 3.1 that these intervals contribute at most O(ε) to the total mass. So even though Birgé's algorithm gives no guarantees for bad intervals, these intervals do not affect the error by more than O(ε).
Let us now focus on the monotone intervals. For each such interval, we do not know if it is monotone increasing or monotone decreasing. To overcome this difficulty, we run both monotone algorithms L ↓ and L ↑ for each interval and then use hypothesis testing to choose the correct candidate distribution. Also, note that since we have O(k/ε) intervals, we need to run each instance of both the monotone learning algorithms and the hypothesis testing algorithm with confidence 1 − O(ε/k), so that we can guarantee that the overall algorithm has confidence 9/10. Note that Theorem 2.3 and Markov's inequality imply that if we draw Ω(log n/ε 3 ) samples from a non-increasing distribution p, the hypothesis p output by L ↓ satisfies d TV ( p, p) ≤ ε with probability 9/10. We can boost the confidence to 1 − δ with an overhead of in the sample complexity: There is an algorithm L ↓ δ with the following performance guarantee: Given bit operations and outputs a (succinct description of a) hypothesis distribution p over [n] that satisfies d TV ( p, p) ≤ ε with probability at least 1 − δ .
Algorithm L ↓ δ runs L ↓ O(log(1/δ )) times and performs a tournament among the candidate hypotheses using Choose-Hypothesis. Let L ↑ δ denote the corresponding algorithm for learning non-decreasing distributions with confidence δ . We postpone further details on these algorithms to Appendix C. samples, performs poly(k, log n, 1/ε) bit operations, and learns a k-modal distribution to accuracy O(ε) with probability 9/10.
Proof. First, it is easy to see that the algorithm has the claimed sample complexity. Indeed, the algorithm draws a total of r + m + m samples in Steps 1, 4 and 5. The running time is also easy to analyze, as it is easy to see that every step can be performed in polynomial time (in fact, nearly linear time) in the sample size.
We need to show that with probability 9/10 (over its random samples), algorithm Learn-kmodalsimple outputs a hypothesis h such that d TV (h, p) ≤ O(ε).
Since r = Θ(d/ε 2 ) samples are drawn in Step 1, Fact 3.1 implies that with probability of failure at most 1/100, for each family J of at most d disjoint intervals from [n], we have For the rest of the analysis of Learn-kmodal-simple we condition on this "good" event.

Construct a set of light intervals
of t ≤ heavy points as follows: Fix δ := ε/(500k).

Draw
be the corresponding conditional hypothesis distributions.
THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 in Steps 2 and 3, every light interval I ∈ I has p(I ) ≤ ε/(5k). Note also that every heavy point b has p(b) ≥ ε/(10k) and the number of heavy points t is at most . Since the light intervals and heavy points form a partition of [n], we can write Therefore, we can bound the variation distance as follows: Since + t ≤ d, by Fact 3.1 and our conditioning, the contribution of the first two terms to the sum is upper bounded by ε.
We proceed to bound the contribution of the third term. Since p is k-modal, at most k of the light intervals I j are not monotone for p. Call these intervals "bad" and denote by B as the set of bad intervals. Even though we have not identified the bad intervals, we know that all such intervals are light. Therefore, their total empirical probability mass (under p m ) is at most k · ε/(5k) = ε/5, i. e., ∑ I∈B p(I) ≤ ε/5. By our conditioning (see equation (3.2)) and the triangle inequality it follows that which implies that the true probability mass of the bad intervals is at most ε/5 + ε = 6ε/5. Hence, the contribution of bad intervals to the third term of the right hand side of (3.3) is at most O(ε). (Note that this statement holds true independent of the samples s we draw in Step 4.) It remains to bound the contribution of monotone intervals to the third term. Let ≤ be the number of monotone light intervals and assume after renaming the indices that they are I := {I j } j=1 . To bound from above the right hand side of (3.3), it suffices to show that with probability at least 19/20 (over the samples drawn in Steps 4-5) it holds To prove (3.4) we partition the set I into three subsets based on their probability mass under p. Note that we do not have a lower bound on the probability mass of intervals in I. Moreover, by our conditioning (see equation (3.2)) and the fact that each interval in I is light, it follows that any I ∈ I has p(I) ≤ p(I) +ε ≤ 2ε. We define the partition of I into the following three sets: We bound the contribution of each subset in turn. It is clear that the contribution of I 1 to (3.4) is at most To bound from above the contribution of I 2 to (3.4), we partition I 2 into groups. For i ∈ [g 2 ], the set ( I 2 ) i consists of those intervals in I 2 that have mass under p in the range 2 −i · (ε/k), 2 −i+1 · (ε/k) . The following statement establishes the variation distance closeness between the conditional hypothesis for an interval in the i-th group ( I 2 ) i and the corresponding conditional distribution.
Proof. Since in Step 4 we draw m samples, and each interval I j ∈ ( I 2 ) i has a standard coupon collector argument [22] tells us that with probability 99/100, for each (i, j) pair, the interval I j will get at least 2 −i · (log(n)/ε 3 ) ·Ω(log(1/δ )) many samples. Let's rewrite this as samples. We condition on this event.
Fix an interval I j ∈ ( I 2 ) i . We first show that with failure probability at most ε/(500k) after Step 4, either p ↓ I j or p ↑ I j will be (2 i/3 · ε)-accurate. Indeed, by Fact 3.2 and taking into account the number of samples that landed in I j , with probability 1 − ε/(500k) over s, where α i =↓ if p I j is non-increasing and α i =↑ otherwise. By a union bound over all (at most many) (i, j) pairs, it follows that with probability at least 49/50, for each interval I j ∈ ( I 2 ) i one of the two candidate hypothesis distributions is (2 i/3 ε)-accurate. We condition on this event. Now consider Step 5. Since this step draws m samples, and each interval I j ∈ ( I 2 ) i has as before a standard coupon collector argument [22] tells us that with probability 99/100, for each (i, j) pair, the interval I j will get at least THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 many samples in this step; we henceforth assume that this is indeed the case for each I j . Thus, Theorem 2.4 applied to each fixed interval I j implies that the algorithm Choose-Hypothesis will output a hypothesis that is 6 · (2 i/3 ε)-close to p I j with probability 1 − ε/(500k). By a union bound, it follows that with probability at least 49/50, the above condition holds for all monotone light intervals under consideration. Therefore, except with failure probability 19/20, the statement of the claim holds.
Given the claim, we exploit the fact that for intervals I j such that p(I j ) is small we can afford larger error on the total variation distance. More precisely, let c i = |( I 2 ) i |, the number of intervals in ( I 2 ) i , and note that ∑ Hence, we can bound the contribution of I 2 to (3.4) by Bounding the contribution of I 3 to (3.4) is very similar. We partition I 3 into The following statement is identical to Claim 3.4 albeit with different parameters: We can now bound from above the contribution of I 3 to (3.4) by

By (3.5) it follows that the above expression is maximized for
where the final equality uses the fact that 2 g 3 ≤ 4k as follows by our definition of g 3 . This proves (3.4) and completes the proof of Theorem 3.3.
In order to obtain an O(ε)-accurate hypothesis with probability 1 − δ , we can simply run Learnkmodal-simple O(log(1/δ )) times and then perform a tournament using Theorem 2.4. This increases the sample complexity by aÕ(log(1/δ )) factor. The running time increases by a factor of O(log 2 (1/δ )). We postpone the details for Appendix C.

Main result: Learning k-modal distributions using testing
Here is some intuition to motivate our k-modal distribution learning algorithm and give a high-level idea of why the dominant term in its sample complexity is O(k log(n/k)/ε 3 ).
Let p denote the target k-modal distribution to be learned. As discussed above, optimal (in terms of time and sample complexity) algorithms are known for learning a monotone distribution over [n], so if the locations of the k modes of p were known then it would be straightforward to learn p very efficiently by running the monotone distribution learner over k + 1 separate intervals. But it is clear that in general we cannot hope to efficiently identify the modes of p exactly (for instance it could be the case that p(a) = p(a + 2) = 1/n while p(a + 1) = 1/n + 1/2 n ). Still, it is natural to try to decompose the k-modal distribution into a collection of (nearly) monotone distributions and learn those. At a high level that is what our algorithm does, using a novel property testing algorithm.
More precisely, we give a distribution testing algorithm with the following performance guarantee: Let q be a k-modal distribution over [n]. Given an accuracy parameter τ, our tester takes poly(k/τ) samples from q and outputs "yes" with high probability if q is monotone and "no" with high probability if q is τ-far from every monotone distribution. (We stress that the assumption that q is k-modal is essential here, since an easy argument given in [2] shows that Ω(n 1/2 ) samples are required to test whether a general distribution over [n] is monotone versus Θ(1)-far from monotone.) With some care, by running the above-described tester O(k/ε) times with accuracy parameter τ, we can decompose the domain [n] into • at most k + 1 "superintervals," which have the property that the conditional distribution of p over each superinterval is almost monotone (τ-close to monotone); • at most k + 1 "negligible intervals," which have the property that each one has probability mass at most O(ε/k) under p (so ignoring all of them incurs at most O(ε) total error); and • at most k + 1 "heavy" points, each of which has mass at least Ω(ε/k) under p.
We can ignore the negligible intervals, and the heavy points are easy to handle; however some care must be taken to learn the "almost monotone" restrictions of p over each superinterval. A naive approach, using a generic log(n)/ε 3 -sample monotone distribution learner that has no performance guarantees if the target distribution is not monotone, leads to an inefficient overall algorithm. Such an approach would require that τ (the closeness parameter used by the tester) be at most 1/(the sample complexity of the monotone distribution learner), i. e., τ < ε 3 / log(n). Since the sample complexity of the tester is poly(k/τ) and the tester is run Ω(k/ε) times, this approach would lead to an overall sample complexity that is unacceptably high. Fortunately, instead of using a generic monotone distribution learner, we can use the semi-agnostic monotone distribution learner of Birgé (Theorem 2.3) that can handle deviations from monotonicity far more efficiently than the above naive approach. Recall that given draws from a distribution q over [n] that is τ-close to monotone, this algorithm uses O(log(n)/ε 3 ) samples and outputs a hypothesis distribution that is (2τ + ε)-close to monotone. By using this algorithm we can take the accuracy parameter τ for our tester to be Θ(ε) and learn the conditional distribution of p over a given superinterval to accuracy THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 O(ε) using O(log(n)/ε 3 ) samples from that superinterval. Since there are k + 1 superintervals overall, a careful analysis shows that O(k log(n)/ε 3 ) samples suffice to handle all the superintervals.
We note that the algorithm also requires an additional additive poly(k/ε) samples (independent of n) besides this dominant term (for example, to run the tester and to estimate accurate weights with which to combine the various sub-hypotheses). The overall sample complexity we achieve is stated in Theorem 3.6 below.
Algorithm Learn-kmodal makes essential use of an algorithm T ↑ for testing whether a k-modal distribution over [n] is non-decreasing. Algorithm T ↑ (ε, δ ) uses O(log(1/δ )) · (k/ε 2 ) samples from a k-modal distribution p over [n], and behaves as follows: • (Completeness) If p is non-decreasing, then T ↑ outputs "yes" with probability at least 1 − δ ; • (Soundness) If p is ε-far from non-decreasing, then T ↑ outputs "yes" with probability at most δ .
Let T ↓ denote the analogous algorithm for testing whether a k-modal distribution over [n] is non-increasing (we will need both algorithms). The description and proof of correctness for T ↑ is postponed to the following subsection (Section 3.4).

Algorithm Learn-kmodal and its analysis
Algorithm Learn-kmodal (Algorithm 2) is described in detail below. The analysis of the algorithm is the topic of Theorem 3.6, proved next.
Proof of Theorem 3.6. Before entering into the proof we record two observations; we state them explicitly here for the sake of the exposition.  are both run on p R , then the probability that both calls return "no" is at most τ.
Proof of Fact 3.8. By Fact 3.7 p R is either non-decreasing or non-increasing. If p R is non-decreasing then T ↑ will output "no" with probability at most τ, and similarly, if p R is non-increasing then T ↓ will output "no" with probability at most τ.
Call I j 1 a negligible interval. If j 1 > 1 then define the first superinterval S 1 to be j 1 −1 i=1 I i , and set a 1 ∈ {↑, ↓} to be a 1 =↑ if T ↑ returned "yes" on p j 1 −1 i=1 I i and to be a 1 =↓ if T ↓ returned "yes" on

Repeat
Step 3 starting with the next interval I j 1 +1 , i. e., find the leftmost atomic interval I j 2 such that both T ↑ and T ↓ return "no" over p j 2 i= j 1 +1 I i . Continue doing this until all intervals through I have been used.
Let S 1 , . . . , S t be the superintervals obtained through the above process and (a 1 , . . . , a t ) ∈ {↑, ↓} t be the corresponding string of bits.
6. Draw m = Θ(k · log(n/k)/ε 3 ) samples s from p. For each superinterval S i , i ∈ [t], run A a i on the conditional distribution p S i of p using the samples in s ∩ S i . Let p S i be the hypothesis thus obtained.

Output the hypothesis
THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 Since r = Θ(1/τ 2 ) samples are drawn in the first step, Fact 3.1 (applied for d = 1) implies that with probability of failure at most 1/100 each interval I ⊆ [n] has | p(I) − p(I)| ≤ 2τ. For the rest of the proof we condition on this good event.
Since every atomic interval I ∈ I has p(I) ≥ ε/(10k) (except potentially the rightmost one), it follows that the number of atomic intervals constructed in Step 2 satisfies ≤ 10 · (k/ε). Moreover, by our conditioning, each atomic interval I i has p(I i ) ≥ 8ε/(100k).
In Steps 4-5 we invoke the testers T ↓ and T ↑ on the conditional distributions of (unions of contiguous) atomic intervals. Note that we need enough samples in every atomic interval, since otherwise the testers provide no guarantees. We claim that with probability at least 99/100 over the sample s of Step 3, each atomic interval gets b = Ω (k/ε 2 ) · log(1/τ ) samples. This follows by a standard coupon collector's argument, which we now provide. As argued above, each atomic interval has probability mass Ω(ε/k) under p. So, we have = O(k/ε) bins (atomic intervals), and we want each bin to contain b balls (samples). It is well-known [22] that after taking Θ( · log + · b · log log ) samples from p, with probability 99/100 each bin will contain the desired number of balls. The claim now follows by our choice of parameters. Conditioning on this event, any execution of the testers T ↑ (ε, τ ) and T ↓ (ε, τ ) in Steps 4 and 5 will have the guaranteed completeness and soundness properties.
In the execution of Steps 4 and 5, there are a total of at most occasions when T ↑ (ε, τ ) and T ↓ (ε, τ ) are both run over some union of contiguous atomic intervals. By Fact 3.8 and a union bound, the probability that (in any of these instances the interval does not contain a left extreme point and yet both calls return "no") is at most (10k/ε)τ ≤ 1/200. So with failure probability at most 1/200 for this step, each time Step 4 identifies a group of consecutive intervals I j , . . . , I j+r such that both T ↑ and T ↓ output "no," there is a left extreme point in j+r i= j I i . Since p is k-modal, it follows that with failure probability at most 1/200 there are at most k + 1 total repetitions of Step 4, and hence the number t of superintervals obtained is at most k + 1.
We moreover claim that with very high probability each of the t superintervals S i is very close to non-increasing or non-decreasing (with its correct orientation given by a i ): Claim 3.9. With failure probability at most 1/100, each i ∈ [t] satisfies the following: if a i =↑ then p S i is ε-close to a non-decreasing distribution and if a i =↓ then p S i is ε-close to a non-increasing distribution.
Proof. There are at most 2 ≤ 20k/ε instances when either T ↓ or T ↑ is run on a union of contiguous intervals. For any fixed execution of T ↓ over an interval I, the probability that T ↓ outputs "yes" while p I is ε-far from every non-increasing distribution over I is at most τ , and similarly for T ↑ . A union bound and the choice of τ conclude the proof of the claim.
Thus we have established that with overall failure probability at most 5/100, after Step 5 the interval [n] has been partitioned into: THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 of t ≤ k + 1 superintervals, with p(S i ) ≥ 8ε/(100k) and p S i being ε-close to either non-increasing or non-decreasing according to the value of bit a i .

A set
We condition on the above good events, and bound from above the expected total variation distance (over the sample s ). In particular, we have the following lemma: Proof of Lemma 3.10. By the discussion preceding the lemma statement, the domain [n] has been partitioned into a set of superintervals, a set of negligible intervals and a set of heavy points. As a consequence, we can write Therefore, we can bound the total variation distance as follows: Recall that each term in the first two sums is bounded from above by 2τ. Hence, the contribution of these terms to the RHS is at most 2τ · (2k + 2) ≤ ε/10. Since each negligible interval I j has p(I j ) ≤ 22ε/(100k), the contribution of the third sum is at most t ·22ε/(100k) ≤ ε/4. It thus remains to bound the contribution of the last sum. We will show that Since we are conditioning on the good events (1)-(3), each superinterval is ε-close to monotone with a known orientation (non-increasing or non-decreasing) given by a i . Hence we may apply Theorem 2.3 for each superinterval.
Recall that in Step 5 we draw a total of m samples. Let m i , i ∈ [t] be the number of samples that land in S i ; observe that m i is a binomially distributed random variable with m i ∼ Bin(m, p(S i )). We apply Theorem 2.3 for each ε-monotone interval, conditioning on the value of m i , and get d TV ( p S i , p S i ) ≤ 2ε + O (log n i /(m i + 1)) 1/3 . Hence, we can bound from above the desired expectation as follows Since ∑ j p(S j ) ≤ 1, to prove the lemma, it suffices to show that the second term is bounded, i. e., that To do this, we will first need the following claim:

Proof. Jensen's inequality implies that
. This can be shown as follows: We first recall that E[X] = m · q. For the expectation of the inverse, we can write: The claim now follows by the monotonicity of the mapping x → x 1/3 . By Claim 3.11, applied to m i ∼ Bin(m, p(S i )), we have that Therefore, our desired quantity can be bounded from above by (p(S j )) 2/3 · log n j k · log(n/k) 1 We now claim that the second term in the RHS above is upper bounded by 2. Indeed, this follows by an application of Hölder's inequality for the vectors p(S j ) 2/3 t j=1 and log n j k · log(n/k) with Hölder conjugates 3/2 and 3. That is, The first inequality is Hölder and the second uses the fact that This last inequality is a consequence of the concavity of the logarithm and the fact that ∑ j n j ≤ n. This completes the proof of the lemma.
By applying Markov's inequality and a union bound, we get that with probability 9/10 the algorithm Learn-kmodal outputs a hypothesis h that has d TV (h, p)=O(ε) as required.
It is clear that the algorithm has the claimed sample complexity. The running time is also easy to analyze, as it is easy to see that every step can be performed in polynomial time in the sample size. This completes the proof of Theorem 3.6.

Testing whether a k-modal distribution is monotone
In this section we describe and analyze the testing algorithm T ↑ (Algorithm 3). Given sample access to a k-modal distribution q over [n] and τ > 0, our tester T ↑ uses O(k/τ 2 ) many samples from q and has the following properties: • If q is non-decreasing, T ↑ outputs "yes" with probability at least 2/3.
• If q is τ-far from non-decreasing, T ↑ outputs "no" with probability at least 2/3.
(Algorithm T ↑ (τ, δ ) is obtained by repeating T ↑ O(log(1/δ )) times and taking the majority vote.) Before we describe the algorithm we need some notation. Let q be a distribution over We also denote .  T (q, a, b, c) is a scaled version of E(q, a, b, c). The idea behind tester T ↑ is simple. It is based on the observation that if q is a non-decreasing distribution, then for any two consecutive intervals [a, b] and [b + 1, c] the average of q over [b + 1, c] must be at least as large as the average of q over [a, b]. Thus any non-decreasing distribution will pass a test that checks "all" pairs of consecutive intervals looking for a violation. Our tester T ↑ checks "all" sums of (at most) k consecutive intervals looking for a violation. Our analysis shows that in fact such a test is complete as well as sound if the distribution q is guaranteed to be k-modal. The key ingredient is a structural result (Lemma 3.13 below), which is proved using a procedure reminiscent of "Myerson ironing" [21] to convert a k-modal distribution to a non-decreasing distribution.

If there exists ∈ [k] and {a
then output "no," otherwise output "yes." The following theorem establishes correctness of the tester.  Now consider any where we used the triangle inequality and (3.7). Note that the rightmost term is the sum of the "additive errors" for the collection Hence, it follows from our conditioning that the last term is bounded from above by τ/20, i. e., We first establish completeness. Suppose that q is non-decreasing. Then the average probability value in any interval [a, b] is a non-decreasing function of a. That is, for all a ≤ b < c ∈ [n] it holds E(q, a, b, c) ≤ 0, hence T (q, a, b, c) ≤ 0. This implies that for any choice of i. e., the tester says "yes" with probability at least 2/3.
To prove soundness, we will crucially need the following structural lemma: Lemma 3.13. Let q be a k-modal distribution over [n] that is τ-far from being non-decreasing. Then there exists ∈ [k] and We first show how the soundness follows from the lemma. Let q be a k-modal distribution over [n] that is τ-far from non-decreasing. Denote s := s ∪ {n} = {s 1 , s 2 , . . . , s r } with r ≤ r + 1 and s j < s j+1 . We want to show that there exist points in s that satisfy (3.6). Namely, that there exists ∈ [k] and To achieve these properties we select: We can rewrite (3.11) as follows: Now note that by properties (i) and (ii) above it follows that Combining with properties (iii) and (iv) we get and similarly q( . (3.14) A combination of (3.12), (3.13), (3.14) yields the desired result (3.10).
It thus remains to prove Lemma 3.13.
Proof of Lemma 3.13. We will prove the contrapositive. Let q be a k-modal distribution over [n] such that for any ≤ k and We will construct a non-decreasing distribution q that is τ-close to q.
The high level idea of the argument is as follows: the construction of q proceeds in (at most) k stages where in each stage, we reduce the number of modes by at least one and incur small error in THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 the total variation distance. In particular, we iteratively construct a sequence of distributions {q (i) } i=0 , q (0) = q and q ( ) = q, for some ≤ k, such that for all i ∈ [ ] we have that q (i) is (k − i)-modal and d TV (q (i−1) , q (i) ) ≤ 2τ i , where the quantities τ i will be defined in the course of the analysis below. By appropriately using (3.15), we will show that Assuming this, it follows from the triangle inequality that as desired, where the last inequality uses (3.16).
Consider the graph (histogram) of the discrete density q. The x-axis represents the n points of the domain and the y-axis the corresponding probabilities. We first informally describe how to obtain q (1) from q. The construction of q (i) from q (i−1) , i ∈ [ ], is essentially identical. Let j 1 be the leftmost (i. e., having minimum x-coordinate) left-extreme point (mode) of q, and assume that it is a local maximum with height (probability mass) q( j 1 ). (A symmetric argument works for the case that it is a local minimum.) The idea of the proof is based on the following simple process (reminiscent of Myerson's ironing process [21]): We start with the horizontal line y = q( j 1 ) and move it downwards until we reach a height h 1 < q( j 1 ) so that the total mass "cut-off" equals the mass "missing" to the right; then we make the distribution "flat" in the corresponding interval (hence, reducing the number of modes by at least one).
We now proceed with the formal argument, assuming as above that the leftmost left-extreme point j 1 of q is a local maximum. We say that the line y = h intersects a point i ∈ By definition this means that q(a(h)) ≥ h and q(a(h) − 1) < h (since q is supported on [n], we adopt the convention that q(0) = 0). Recall that the distribution q is non-decreasing in the interval [1, j 1 ] and that j 1 ≥ a(h). The term "the mass cut-off by the line y = h" means the quantity i. e., the "mass of the interval I(h) above the line." The height h of the line y = h defines the points a(h), b(h) ∈ [n] as described above. We consider values of h such that q is unimodal (increasing then decreasing) over I(h). In particular, let j 1 be the leftmost mode of q to the right of j 1 , i. e., j 1 > j 1 and j 1 is a local minimum. We consider values of h ∈ (q( j 1 ), q( j 1 )). For such values, the interval I(h) is indeed unimodal (as b(h) < j 1 ). For h ∈ (q( j 1 ), q( j 1 )) we define the point c(h) ≥ j 1 as follows: It is the rightmost point of the largest interval containing j 1 whose probability mass does not exceed h. That is, all points in [ j 1 , c(h)] have probability mass at most h and q(c(h) + 1) > h (or c(h) = n). also contain more modes.) The term "the mass missing to the right of the line y = h" means the quantity

Consider the function
. This function is continuous in its domain; moreover, we have that as B (q( j 1 )) = 0. Therefore, by the intermediate value theorem, there exists a value h 1 ∈ (q( j 1 ), q( j 1 )) such that A(h 1 ) = B(h 1 ) .
The distribution q (1) is constructed as follows: We move the mass Let us denote a 1 = a(h 1 ), b 1 = b(h 1 ) and c 1 = c(h 1 ). We claim that q (1) has at least one mode less than q. Indeed, q (1) is non-decreasing in [1, a 1 − 1] and constant in [a 1 , c 1 ]. (By our "flattening" process, all the points in the latter interval have probability mass exactly h 1 .) Recalling that We will now argue that Recall that we have A(h 1 ) = B(h 1 ) = τ 1 , which can be written as From this, we get which gives (3.17). We construct q (2) from q (1) using the same procedure. Recalling that the leftmost mode of q (1) lies in the interval (c 1 , n] an identical argument as above implies that for some a 2 , b 2 , c 2 ∈ [n] satisfying c 1 < a 2 ≤ b 2 < c 2 . Since q (1) is identical to q in (c 1 , n], it follows that We continue this process iteratively for ≤ k stages until we obtain a non-decreasing distribution q ( ) . (Note that we remove at least one mode in each iteration, hence it may be the case that < k.) It follows inductively that for all which is bounded from above by τ/2 by (3.15). This establishes (3.16) completing the proof of Lemma 3.13.
The upper bound on the sample complexity of the algorithm is straightforward, since only Step 1 uses samples.
It remains to analyze the running time. The only non-trivial computation is in Step 2 where we need to decide whether there exist ≤ k "ordered triples" Even though a naive brute-force implementation would need time Ω(r k ) · log n, there is a simple dynamic programming algorithm that runs in poly(r, k) · log n time.
We now provide the details. Consider the objective function . We want to decide whether max ≤k T( ) ≥ τ/4. For ∈ [k] and j ∈ [r ], we use dynamic programming to compute the quantities (This clearly suffices as T( ) = max j∈[r ] T( , j).) The dynamic program is based on the recursive identity where we define

Conclusions and future work
At the level of techniques, this work illustrates the viability of a new general strategy for developing efficient learning algorithms, namely by using "inexpensive" property testers to decompose a complex object (for us these objects are k-modal distributions) into simpler objects (for us these are monotone distributions) that can be more easily learned. It would be interesting to apply this paradigm in other contexts such as learning Boolean functions.
At the level of the specific problem we consider-learning k-modal distributions-our results show that k-modality is a useful type of structure which can be strongly exploited by sample-efficient and computationally efficient learning algorithms. Our results motivate the study of computationally efficient learning algorithms for distributions that satisfy other kinds of "shape restrictions." Possible directions here include multivariate k-modal distributions, log-concave distributions, monotone hazard rate distributions and more.
At a technical level, any improvement in the sample complexity of our property testing algorithm of Section 3.4 would directly improve the "extraneous" additiveÕ(k 2 /ε 3 ) term in the sample complexity of our algorithm. We suspect that it may be possible to improve our testing algorithm (although we note that it is easy to give an Ω( √ k/ε 2 ) lower bound using standard constructions). Our learning algorithm is not proper, i. e., it outputs a hypothesis that is not necessarily k-modal.
Obtaining an efficient proper learning algorithm is an interesting question. Finally, it should be noted that our approach for learning k-modal distributions requires a priori knowledge of the parameter k. We leave the case of unknown k as an intriguing open problem.
Let p be a non-increasing distribution over [n]. (The analysis for the non-decreasing case is identical.) Conceptually, we view algorithm L ↓ as working in three steps: • In the first step, it partitions the set [n] into a carefully chosen set I 1 , . . . , I of consecutive intervals, with = O(m 1/3 · (log n) 2/3 ). Consider the flattened distribution p f over [n] obtained from p by averaging the weight that p assigns to each interval over the entire interval. That is, for j ∈ [ ] and i ∈ I j , p f (i) = ∑ t∈I j p(t)/|I j |. Then a simple argument given in [4] gives that d TV (p f , p) = O (log n/(m + 1)) 1/3 .
• Let p r be the reduced distribution corresponding to p and the partition I 1 , . . . , I . That is, p r is a distribution over [ ] with p r (i) = p(I i ) for i ∈ [ ]. In the second step, the algorithm uses the m samples to learn p r . (Note that p r is not necessarily monotone.) After m samples, one obtains a hypothesis p r such that The first equality follows from Fact A.1 (since p r is distribution over elements) and the second inequality follows from the choice of .
• Finally, the algorithm outputs the flattened hypothesis ( p r ) f over [n] corresponding to p r , i. e., obtained by p r by subdividing the mass of each interval uniformly within the interval. It follows from the above two steps that • The combination of the first and third steps yields that The above arguments are entirely due to Birgé [4]. We now explain how his analysis can be extended to show that his algorithm is in fact a semi-agnostic learner as claimed in Theorem 2.3. To avoid clutter in the expressions below let us fix δ := O (log n/(m + 1)) 1/3 .
The second and third steps in the algorithm description above are used to learn the distribution p f to variation distance δ . Note that these steps do not use the assumption that p is non-increasing. The following claim, which generalizes Step 1 above, says that if p is τ-close to non-increasing, the flattened distribution p f (defined as above) is (2τ + δ )-close to p. Therefore, it follows that, for such a distribution p, algorithm L ↓ succeeds with expected (total variation distance) error (2τ + δ ) + δ . We have: Claim A.2. Let p be a distribution over [n] that is τ-close to non-increasing. Then, the flattened distribution p f (obtained from p by averaging its weight on every interval I j ) satisfies Proof. Let p ↓ be the non-increasing distribution that is τ-close to p. Let τ j denote the L 1 -distance between p and p ↓ in the interval I j . Then, we have that ∑ j=1 τ j ≤ τ .
(A.1) By Birgé's arguments, it follows that the flattened distribution (p ↓ ) f corresponding to p ↓ is δ -close to p ↓ , hence (τ + δ )-close to p. That is, Assuming (A.3) holds, we can conclude by the triangle inequality that Observe that, by assumption, p and p ↓ have L 1 -distance at most τ j in each I j interval. In particular, this implies that, for all j ∈ [ ], it holds Now note that, within each interval I j , p f and (p ↓ ) f are both uniform. Hence, the contribution of I j to the variation distance between p f and (p ↓ ) f is at most |p(I j ) − p ↓ (I j )|.
Therefore, by (A.1) we deduce which completes the proof of the claim.

B Hypothesis testing
Our hypothesis testing routine Choose-Hypothesis p runs a simple "competition" to choose a winner between two candidate hypothesis distributions h 1 and h 2 over [n] that it is given in the input either explicitly, or in some succinct way. We show that if at least one of the two candidate hypotheses is close to the target distribution p, then with high probability over the samples drawn from p the routine selects as winner a candidate that is close to p. This basic approach of running a competition between candidate hypotheses is quite similar to the "Scheffé estimate" proposed by Devroye and Lugosi THEORY OF COMPUTING, Volume 10 (20), 2014, pp. 535-570 (see [10,11] and Chapter 6 of [12]), which in turn built closely on the work of [27], but there are some small differences between our approach and theirs; the [12] approach uses a notion of the "competition" between two hypotheses which is not symmetric under swapping the two competing hypotheses, whereas our competition is symmetric. We now prove Theorem 2.4.
Proof of Theorem 2.4. Let W be the support of p. To set up the competition between h 1 and h 2 , we define the following subset of W: Let then p 1 = h 1 (W 1 ) and q 1 = h 2 (W 1 ). Clearly, p 1 > q 1 and d TV (h 1 , h 2 ) = p 1 − q 1 . The competition between h 1 and h 2 is carried out as follows: It is not hard to check that the outcome of the competition does not depend on the ordering of the pair of distributions provided in the input; that is, on inputs (h 1 , h 2 ) and (h 2 , h 1 ) the competition outputs the same result for a fixed sequence of samples s 1 , . . . , s m drawn from p.
The correctness of Choose-Hypothesis is an immediate consequence of the following lemma.
Hence, the algorithm will go beyond Step 1, and with probability at least 1 − e −mε 2 /2 , it will stop at Step 3, declaring h 1 as the winner of the competition between h 1 and h 2 .
• For part (ii): If p 1 − q 1 ≤ 5ε then the competition declares a draw, hence h 2 is not the winner. Otherwise we have p 1 − q 1 > 5ε and the above arguments imply that the competition between h 1 and h 2 will declare h 2 as the winner with probability at most e −mε 2 /2 .
This concludes the proof of Lemma B.1.
The proof of the theorem is now complete.

C Using the hypothesis tester
In this section, we explain in detail how we use the hypothesis testing algorithm Choose-Hypothesis throughout this paper. In particular, the algorithm Choose-Hypothesis is used in the following places: • In Step 4 of algorithm Learn-kmodal-simple we need an algorithm L ↓ δ (resp. L ↑ δ ) that learns a non-increasing (resp. non-increasing) distribution within total variation distance ε and confidence δ . Note that the corresponding algorithms L ↓ and L ↑ provided by Theorem 2.3 have confidence 9/10. To boost the confidence of L ↓ (resp. L ↑ ) we run the algorithm O(log(1/δ )) times and use Choose-Hypothesis in an appropriate tournament procedure to select among the candidate hypothesis distributions.
• In Step 5 of algorithm Learn-kmodal-simple we need to select among two candidate hypothesis distributions (with the promise that at least one of them is close to the true conditional distribution).
In this case, we run Choose-Hypothesis once to select between the two candidates.
• Also note that both algorithms Learn-kmodal-simple and Learn-kmodal generate an ε-accurate hypothesis with probability 9/10. We would like to boost the probability of success to 1 − δ .
To achieve this we again run the corresponding algorithm O(log(1/δ )) times and use Choose-Hypothesis in an appropriate tournament to select among the candidate hypothesis distributions.
Lemma C.1. Let p be any distribution over a finite set W. Suppose that D ε is a collection of N distributions over W such that there exists q ∈ D ε with d TV (p, q) ≤ ε. Then there is an algorithm that uses O(ε −2 log N log(1/δ )) samples from p and with probability 1 − δ outputs a distribution p ∈ D ε that satisfies d TV (p, p ) ≤ 6ε.
Devroye and Lugosi (Chapter 7 of [12]) prove a similar result by having all pairs of distributions in the cover compete against each other using their notion of a competition, but again there are some small differences: their approach chooses a distribution in the cover which wins the maximum number of competitions, whereas our algorithm chooses a distribution that is never defeated (i. e., won or achieved a draw against all other distributions in the cover). Instead we follow the approach from [9].
Proof. The algorithm performs a tournament by running the competition Choose-Hypothesis p (h i , h j , ε, δ /(2N)) for every pair of distinct distributions h i , h j in the collection D ε . It outputs a distribution q ∈ D ε that was never a loser (i. e., won or achieved a draw in all its competitions). If no such distribution exists in D ε then the algorithm outputs "failure." By definition, there exists some q ∈ D ε such that d TV (p, q) ≤ ε. We first argue that with high probability this distribution q never loses a competition against any other q ∈ D ε (so the algorithm does not output "failure"). Consider any q ∈ D ε . If d TV (p, q ) > 4ε, by Lemma B.1(ii) the probability that q loses to q is at most 2e −mε 2 /2 = O(1/N). On the other hand, if d TV (p, q ) ≤ 4δ , the triangle inequality gives that d TV (q, q ) ≤ 5ε and thus q draws against q . A union bound over all N distributions in D ε shows that with probability 1 − δ /2, the distribution q never loses a competition.
We next argue that with probability at least 1 − δ /2, every distribution q ∈ D ε that never loses has small variation distance from p. Fix a distribution q such that d TV (q , p) > 6ε; Lemma B.1(i) implies that q loses to q with probability 1 − 2e −mε 2 /2 ≥ 1 − δ /(2N). A union bound gives that with probability 1 − δ /2, every distribution q that has d TV (q , p) > 6ε loses some competition.
Thus, with overall probability at least 1 − δ , the tournament does not output "failure" and outputs some distribution q such that d TV (p, q ) is at most 6ε. This proves the lemma.
We now explain how the above lemma is used in our context: Suppose we perform O(log(1/δ )) runs of a learning algorithm that constructs an ε-accurate hypothesis with probability at least 9/10. Then, with failure probability at most δ /2, at least one of the hypotheses generated is ε-close to the true distribution in variation distance. Conditioning on this good event, we have a collection of distributions with cardinality O(log(1/δ )) that satisfies the assumption of the lemma. Hence, using O (1/ε 2 ) · log log(1/δ ) · log(1/δ ) samples we can learn to accuracy 6ε and confidence 1 − δ /2. The overall sample complexity is O(log(1/δ )) times the sample complexity of the learning algorithm run with confidence 9/10, plus this additional O (1/ε 2 ) · log log(1/δ ) · log(1/δ ) term.
In terms of running time,we make the following easily verifiable remarks: When the hypothesis testing algorithm Choose-Hypothesis is run on a pair of distributions that are produced by Birgé's algorithm, its running time is polynomial in the succinct description of these distributions, i. e., in log 2 (n)/ε. Similarly, when Choose-Hypothesis is run on a pair of outputs of Learn-kmodal-simple or Learn-kmodal, its running time is polynomial in the succinct description of these distributions. More