Hardy inequalities and Assouad dimensions

We establish both sufficient and necessary conditions for weighted Hardy inequalities in metric spaces in terms of Assouad (co)dimensions. Our sufficient conditions in the case where the complement is thin are new even in Euclidean spaces, while in the case of a thick complement we give new formulations for previously known sufficient conditions which reveal a natural duality between these two cases. Our necessary conditions are rather straight-forward generalizations from the unweighted case, but together with some examples they indicate the essential sharpness of our results. In addition, we consider the mixed case where the complement may contain both thick and thin parts.


Introduction
Let X be a metric measure space. We say that an open set Ω ⊂ X admits a (p, β)-Hardy inequality, if there exists a constant C > 0 such that the inequality holds for all u ∈ Lip 0 (Ω) and for all upper gradients g u of u. Here d Ω (x) = dist(x, Ω c ) is the distance from x ∈ Ω to the complement Ω c = X \ Ω, and in the case X = R n we have g u = |∇u|.
There is a well-known dichotomy concerning domains admitting a Hardy inequality: either the complement of the domain is is large (or "thick") or sufficiently "thin". For instance, if an open set Ω ⊂ R n admits a (p, β)-Hardy inequality, then there exists δ > 0 such that for each ball B ⊂ R n either dim H (2B ∩ Ω c ) > n − p + β + δ or dim A (B ∩ Ω c ) < n−p+β −δ; see [21] (the case β = 0) and [23]. Here dim H denotes the Hausdorff dimension and dim A is the (upper) Assouad dimension (see Section 2 for definitions).
Reflecting this dichotomy, sufficient conditions for the validity of a (p, β)-Hardy inequality can be given in both of the above cases. For thick complements, a canonical sufficient condition for unweighted (β = 0) p-Hardy inequalities in Ω is the uniform p-fatness of Ω c , or equivalently a uniform Hausdorff content density condition for Ω c , see [29,35,20]. In R n , uniform p-fatness of Ω c implies in particular that dim H (2B ∩ Ω c ) > n − p for all balls centered at Ω c . On the other hand, in the case of thin complements the smallness of the (upper) Assouad dimension of the complement (dim A (Ω c ) < n − p) is known to be sufficient for the p-Hardy inequality; see [21,23] and note that in this case these results are based on the works of Aikawa [1,3]. See also [7,19,20,26,27] for sufficient conditions for weighted Hardy inequalities and to metric space versions of such results.
The main purpose of this paper is to sharpen the previously known sufficient conditions for the validity of Hardy inequalities in the case where the complement is assumed to be thin. More precisely, we prove the following theorem in the setting of a doubling metric space X supporting certain Poincaré inequalities (cf. Section 2). Here the thinness is formulated in terms of the so-called lower Assouad codimension of Ω c (a metric space version of the (upper) Assouad dimension, see Section 2). Theorem 1.1. Let 1 ≤ p < ∞ and β < p−1, and assume that X is an unbounded doubling metric space. If β ≤ 0, we further assume that X supports a p-Poincaré inequality, and if β > 0 we assume that X supports a (p − β)-Poincaré inequality. If Ω ⊂ X is an open set satisfying co dim A (Ω c ) > p − β, then Ω admits a (p, β)-Hardy inequality.
In the unweighted case β = 0, which is the most important and most interesting, Theorem 1.1 shows that a p-Hardy inequality holds in Ω under the assumptions that X supports a p-Poincaré inequality and co dim A (Ω c ) > p > 1; in a Q-regular space the latter condition is equivalent to dim A (Ω c ) < Q − p. In particular, this gives a complete answer to a question of Koskela and Zhong [21,Remark 2.8]. See also Corollary 6.5 for an improvement concerning the boundary values of test-functions in Theorem 1.1.
In R n , the case β = 0 of Theorem 1.1 coincides with the above-mentioned results from [21,23], but our approach gives a completely new proof is this case. For β = 0 the result is new even in Eulidean spaces. Our proof of Theorem 1.1 follows the general scheme of Wannebo [35]: We first prove (p, β)-Hardy inequalities for β < 0, with a suitable control for the constants in the inequalities for β close to 0, and then elementary -but slightly technical -integration tricks yield the inequalities for β ≥ 0.
Another goal of this work is to bring together much of the recent research on Hardy inequalities (see e.g. [19,20,21,23,24,25,26,27,28]) in a unified manner in the setting of metric spaces. For instance, it was shown in [26] that an open set Ω ⊂ X admits a (p, β)-Hardy inequality if the complement Ω c satisfies a uniform density condition in terms of a Hausdorff content of codimension q < p − β. In the present paper, we establish a new characterization for the upper Assouad codimension by means of Hausdorff cocontent density, see Corollary 5.2. (In Ahlfors regular spaces, such a characterization was observed in [17,Remark 3.2].) Consequently, we obtain the following sufficient condition for Hardy inequalities in terms of the upper Assouad codimension, which provides a natural counterpart for Theorem 1.1 and shows that there exists a nice "duality" between the sufficient conditions for the cases of thick and thin complements. Theorem 1.2. Let 1 ≤ p < ∞ and β < p − 1, and assume that X is a doubling metric space supporting a p-Poincaré inequality if β ≤ 0, and a (p − β)-Poincaré inequality if β > 0. Let Ω ⊂ X be an open set satisfying co dim A (Ω c ) < p − β, and, in case Ω is unbounded, we require in addition that Ω c is unbounded as well. Then Ω admits a (p, β)-Hardy inequality.
The sharpness of Theorems 1.1 and 1.2 will be discussed in detail in Section 8, but let us mention here some of the relevant facts. First of all, the bound p − β for the codimensions is very natural and sharp. Indeed, we show in Theorem 6.1 that if Ω admits a (p, β)-Hardy inequality, then either co dim H (Ω c ) < p − β or co dim A (Ω c ) > p − β, and it is clear that for sufficiently regular Ω c we have co dim H (Ω c ) = co dim A (Ω c ). Nevertheless, it is not necessary for a (p, β)-Hardy inequality that either co dim A (Ω c ) < p − β or co dim A (Ω c ) > p − β, since suitable local combinations of the assumptions in Theorems 1.1 and 1.2 yield sufficient conditions for Hardy inequalities as well (cf. Section 7), and in such cases typically only the bound co dim H (Ω c ) < p − β is satisfied. There is also a corresponding local dimension dichotomy for Hardy inequalities, again with co dim H and co dim A , see Theorem 6.2.
The requirement p − β > 1 is also sharp in both of the theorems. For instance, the unit ball B = B(0, 1) ⊂ R n gives a simple counterexample for the result of Theorem 1.2 in the case 0 < p − β ≤ 1, since co dim A (R n \ B) = 0, but B admits (p, β)-Hardy inequalities only when p − β > 1. However, under additional conditions on Ω these Hardy inequalities can also be proven in the case p − β ≤ 1, see Remark 4.1 and the discussion in Section 8. The unboundedness assumptions of X and Ω c in Theorems 1.1 and 1.2, respectively, can not be relaxed either, as the examples at the end of Section 8 show. The only assumption whose role is not completely understood at the moment is the (p − β)-Poincaré inequality in the cases β > 0 of the theorems; a p-Poincaré inequality is certainly necessary in any of the cases. See Remark 4.1 for a related discussion.
Part of the motivation for the present work stems from the connection between Hardy inequalities and the so-called quasiadditivity property of the variational capacity. Some aspects of such a connection have been visible e.g. in [2,3,21,23], but only recently it was shown in [27] that quasiadditivity of the p-capacity with respect to Ω and the validity of a p-Hardy inequality in Ω are essentially equivalent conditions (under some mild assumptions on the space X or the open set Ω). An assumption equivalent to the dimension bound co dim A (E) > p (or rather dim A (E) < n − p) was used already by Aikawa [1] in connection to the quasiadditivity of the Riesz capacity R 1,p with respect to Whitney decompositions of the complement R n \ E. In order to obtain a corresponding result for the variational p-capacity (in metric spaces), the unweighted case β = 0 of Theorem 1.1 was deduced in [27,Prop. 3] under an additional accessibility condition (note that the lower Assouad codimension is called the Aikawa codimension in [27] and in [28], see Section 2 for a discussion). Now Theorem 1.1 makes such an additional condition unnecessary, and thus we have the following corollary to Theorem 1.1 and [27, Thm 1], yielding a complete analogy with the results of Aikawa [1,3]. Corollary 1.4. Let 1 < p < ∞, and assume that X is an unbounded doubling metric space supporting a p-Poincaré inequality. If Ω ⊂ X is an open set satisfying co dim A (Ω c ) > p, then the variational p-capacity cap p (·, Ω) is quasiadditive with respect to Whitney covers W c (Ω) for suitably small parameters c > 0.
We refer to [27] for all the relevant definitions. Let us also point out that the proof of the corresponding Hardy inequalities in [27] is more straight-forward than the proof of Theorem 1.1 here, and so the proof from [27] may actually be preferred in the cases where the accessibility condition is known to hold.
The organization of the rest of the paper is as follows. In Section 2 we recall the necessary background material concerning metric spaces and the various notions of dimension. Section 3 contains a proof of the case β < 0 of Theorem 1.1, and the case 0 ≤ β < p − 1 is then established in the following Section 4. The relation between Hausdorff (co)content density and the upper Assouad codimension is studied in Section 5 with the help of a measure distribution procedure. This section also contains the proof of Theorem 1.2. The necessary conditions for Hardy inequalities are the topic of Section 6. Finally, in Section 7 we discuss the case where the complement contains both thick and thin parts, and in Section 8 we give examples which indicate the sharpness of our assumptions.
For the notation we remark that C and c will denote positive constants whose values are not necessarily the same at each occurrence. If there exist constants c 1 , c 2 > 0 such that c 1 F ≤ G ≤ c 2 F , we sometimes write F ≃ G and say that F and G are comparable.

Metric spaces and concepts of dimension
We assume throughout this paper that X = (X, d, µ) is a complete metric measure space, where µ is a Borel measure supported on X, with 0 < µ(B) < ∞ whenever B = B(x, r) := {y ∈ X : d(x, y) ≤ r} is a (closed) ball in X. In addition, we assume that µ is doubling, that is, there is a constant C > 0 such that whenever x ∈ X and r > 0, we have µ(B(x, 2r)) ≤ C µ(B(x, r)). The completeness of X is actually not needed in all of our results, but for simplicity we still keep this as a standing assumption. We also make the tacit assumption that each ball B ⊂ X has a fixed center x B and radius rad(B) (but these need not be unique), and thus notation such as λB = B(x B , λ rad(B)) is well-defined for all λ > 0. The diameter of a set E ⊂ X is denoted diam(E), the distance from a point x to E is dist(x, E), and χ E denotes the characteristic function of E.
We also say that the measure µ is Q-regular, if there is a constant C ≥ 1 such that for all x ∈ X and every 0 < r < diam(X).
Given a measurable function f : X → [−∞, ∞], a Borel measurable non-negative function g on X is an upper gradient of f if whenever γ is a compact rectifiable curve in X, Here x and y are the two endpoints of γ, and the above condition should be interpreted as claiming that γ g ds = ∞ whenever at least one of |f (x)|, |f (y)| is infinite. See e.g. [4,15] for introduction on analysis on metric spaces based on the notion of upper gradients. In addition to the doubling property, we will also assume throughout the paper that the space X supports a (1, p)-Poincaré inequality (or simply p-Poincaré inequality) for 1 ≤ p < ∞, that is, there exist constants C > 0 and λ ≥ 1 such that whenever B = B(x, r) ⊂ X and g is an upper gradient of a measurable function f , we have To be more precise, we keep a p-Poincaré inequality as a standing assumption, but as was already seen in Theorems 1.1 and 1.2, we occasionally require even stronger Poincaré inequalities. Moreover, we will rely in some of our formulations on the fundamental result of Keith and Zhong [18] on the self-improvement of Poincaré inequalities: If 1 < p < ∞ and a complete doubling metric space X supports a p-Poincaré inequality, then there exist 1 ≤ p 0 < p such that X supports also a p 0 -Poincaré inequality, and hence actually p ′ -Poincaré inequalities for all p ′ ≥ p 0 .
One consequence of Poincaré inequalities for the geometry of X is that a space supporting a p-Poincaré inequality is quasiconvex. This means that there exists C ≥ 1 such that each pair of points x, y ∈ X can be joined using a rectifiable curve γ x,y of length ℓ(γ x,y ) ≤ Cd(x, y); see e.g. [4] for details. From quasiconvexity we obtain the useful fact that if Ω ⊂ X is an open set, then d Ω ( Let Ω ⊂ X. A function u : Ω → R is said to be (L-)Lipschitz, if for all x, y ∈ Ω.
We denote the set of all Lipschitz functions u : Ω → R by Lip(Ω). In addition, Lip 0 (Ω) (resp. Lip b (Ω)) denotes the set of Lipschitz functions with compact (resp. bounded) support in Ω. Recall that the support of a function u : Ω → R is the closure of the set where u is non-zero. The upper and lower pointwise Lipschitz constants of a function u : Ω → R at x ∈ Ω are respectively. It is not hard to see that both of these are upper gradients of a (locally) Lipschitz function u : Ω → R (cf. [4, Proposition 1.14]). In R n , on the other hand, |∇u| is a (minimal weak) upper gradient of u ∈ Lip(R n ), consult e.g. [4] for the result and the terminology.
Let us now recall the various notions of dimension that will important for us throughout the paper. Let E ⊂ X. The (upper) Assouad dimension of E, denoted dim A (E) (or simply dim A (E)), is the infimum of exponents s ≥ 0 for which there is a constant C ≥ 1 such that for all x ∈ E and every 0 < r < R < diam(E), the set E ∩ B(x, R) can be covered by at most C(r/R) −s balls of radius r. We remark that this upper Assouad dimension is the "usual" Assouad dimension found in the literature. See Luukkainen [30] for the basic properties and a historical account on the (upper) Assouad dimension. near the set E.
Conversely to the above definition, in [17] the lower Assouad dimension of E, dim A (E), was defined to be the supremum of exponents t ≥ 0 for which there is a constant c > 0 so that if 0 < r < R < diam(E), then for every x ∈ E at least c(r/R) −t balls of radius r are needed to cover E ∩ B(x, R). Closely related concepts have been considered e.g. by Larman [22] and Farser [10], but an important difference in our definition is that we consider all radii 0 < r < diam(E), not just small radii; in the context of Hardy inequalities this turns out to be essential.
For comparison, recall that the upper Minkowski dimension of a compact E ⊂ X, denoted dim M (E), is the infimum of λ ≥ 0 such that the whole set E can be covered by at most Cr −λ balls of radius 0 < r < diam(E), and the lower Minkowski dimension, dim M (E), is the supremum of λ ≥ 0 for which at least cr −λ balls of radius 0 < r < diam(E) are needed to cover E. It follows immediately that dim Since a doubling metric space is separable, there exists for all r > 0 a maximal r-packing of E ⊂ X, that is, a countable collection B of pairwise disjoint balls B(x i , r), with When working in a (non-regular) metric space X, it is often convenient to describe the sizes of sets in terms of codimensions rather than dimensions. For instance, the Hausdorff codimension of E ⊂ X (with respect to µ) is the number We define next the Assouad codimensions following [17]: for all E ⊂ X (cf. [17]).
Remark 2.1. It was shown in [28, Thm. 5.1] that the lower Assouad codimension can also be characterized as the supremum of all q ≥ 0 for which there exists a constant C ≥ 1 such that for every x ∈ E and all 0 < r < diam(E). (Here we interpret the integral to be +∞ if q > 0 and E has positive measure.) A concept of dimension defined via integrals as in (1) was used by Aikawa in [1] for subsets of R n (see also [3]). Thus, in [27,28], where the interest originates from such integral estimates, the lower Assouad codimension was called the Aikawa codimension. We will see later, especially in Section 6, that this kind of integral estimates arise very naturally in connection to Hardy inequalities.
The next lemma records the fact that the Aikawa condition (1) enjoys self-improvement. This property is a direct consequence of the famous self-improvement result for reverse Hölder inequalities (in R n due to Gehring [11]), and we will need this in Section 6 when proving our necessary conditions for Hardy inequalities. For q > 1, the result of Lemma 2.2 is contained in the proof of Lemma 2.4 in [21], and in the proof of Proposition 4.3 in [16] the same fact is used in R n .

Lemma 2.2.
Assume that E ⊂ X satisfies the Aikawa condition (1) for every x ∈ E and all 0 < r < diam(E) with an exponent q > 0. Then there exist δ > 0, depending only on the given data, such that condition (1) holds for every x ∈ E and all 0 < r < diam(E) with the exponent q + δ.
Fix any 0 < s < q, e.g. s = q/2, and let B 0 = B(x, R) with x ∈ E and 0 < R < diam(E). In addition, let B be a ball such that B ⊂ B 0 . If 4B ∩ E = ∅, then we find a ball B ′ centered at E and with a radius comparable to rad(B), and so we have by (1) and doubling that in the last inequality we used the fact that dist(y, E) −s ≥ Cr −s for all y ∈ 2B. In particular we obtain the reverse Hölder inequality On the other hand, if 4B ∩ E = ∅, then dist(y, E) ≃ dist(x, E) for all y ∈ 2B, and thus (2) holds in this case as well.
Since the function f (y) = dist(y, E) −s ∈ L 1 (B 0 ) now satisfies the assumption of the Gehring Lemma [4, Thm. 3.22] for all balls B ⊂ B 0 , the proof in [4] shows that there is δ > 0 such that we have for the ball where we also used Hölder's inequality and the original estimate (1). Moreover, here the constant C > 0 is independent of the ball B 1 . The claim follows, since for balls B 1 with diam(E)/2 ≤ rad(B 1 ) < diam(E) we can consider a cover using smaller balls.

A weighted Hardy inequality for β < 0
As a first step towards Theorem 1.1, we establish in this section the result in the case β < 0. The particular form of the constant in the (p, β)-Hardy inequalities below plays an important role in the proof of the general case of Theorem 1.1. Notice that here the test functions are not required to vanish in Ω c , and recall that we have the standing assumption that X is a (complete) doubling metric space supporting a p-Poincaré inequality.
Moreover, there exists β < 0 such that forβ < β < 0 the constant in the (p, β)-Hardy inequality can be chosen to be A standard telescoping trick using the p-Poincaré inequality then yields for every Comparison of the sum on the right-hand side of (3) with the convergent geometric series ∞ j=k 2 (k−j)δ , for any δ > 0, shows that there exists a constant C 1 (δ) > 0, independent of u and B, and an index j(B) ≥ k such that We obtain from (4) for each B ∈ B k a ball B j(B) with radius 2 j(B) satisfying Let us now start to estimate the left-hand side of the (p, β)-Hardy inequality. Since where C 2 = 2 p−β C ′ and C ′ > 0 is independent of β. The first sum in the last line of (6) can be estimated with the help of the (p, p)-Poincaré inequality (which is a well-known consequence of the p-Poincaré inequality, see e.g. [13,15]) and the controlled overlap of the balls 3λB for B ∈ B k (with a fixed k ∈ Z, by doubling). We also rewrite the integral, change the order of summation, and use the simple estimate d Ω (x) ≤ 3 · 2 i for x ∈ A i , as follows: Above the constants C > 0 and M = M (λ) ≥ 3 are independent of β; note also how the assumption β < 0 was needed.
In the last sum of (6) we first use (5) and then change the order of summation: Since the balls B, for B ∈ B k , are pairwise disjoint, the assumption co dim Thus we obtain from (8), using also the bounded overlap of 3λB, that where the last inequality follows just like in (7). A combination of (6), (7), and (9) thus yields the (p, β)-Hardy inequality where the constant C > 0 is independent of β, but depends on δ (cf. (3)), q 1 , q 2 , p, and the data associated to X. We conclude the proof with a closer examination of the constant in (10). First of all, if −1 < β < 0, then 2 −(M +2)β ≤ 2 M +2 , and when β is close enough to 0, then 1 − 2 β ≃ −β. In addition, the constant C in (10) depends on δ, q 1 and q 2 , and hence indirectly on β as well, since δ = (q 2 − p + β)/p and p − β < q 1 < q 2 < co dim A (Ω c ). Nevertheless, if e.g. p − co dim A (Ω c ) /2 < β < 0, then this constant can obviously be chosen to depend only on p and co dim A (Ω c ) (and the data associated to X). It follows that there exists β < 0 such that for all β < β < 0 we have where the constant C * > 0 is independent of the particular β.

4.
The case 0 ≤ β < p − 1 of Theorem 1.1 We now turn to the proof of the weighted (p, β)-Hardy inequality in the case 0 ≤ β < p − 1 under the assumption co dim A (Ω c ) > p − β. The proof is based on Proposition 3.1, and it combines ideas from [35,21,24]. In fact, in the Euclidean case the result can be readily deduced from the (p, β)-Hardy inequalities of Proposition 3.1 with a careful use of [24, Lemma 2.1].
We now choose 0 < β 0 < β 0 to be so small that . This is possible since p − β > 1 and the factor C * C(p, β) does not depend on β 0 . After the insertion of (13) into (12), we observe that under the above choice of β 0 , the second term emerging on the right-hand side is less than half of the left-hand side, and thus we obtain The (p, β)-Hardy inequality for u now follows from (14) by dividing with the first factor on the right-hand side (which we may assume to be non-zero), and then taking both sides to power p/(p − β).
Notice that the requirement p − β > 1 is essential in the above proof. This is not merely a technical assumption, since Theorem 1.1 need not hold if p − β ≤ 1; see Section 8. Here it is good to recall that when Ω c (or actually ∂Ω) satisfies additional accessibility conditions from within Ω, then such direct proofs exist. Moreover, under these accessibility conditions the results of Theorems Remark 4.2. An interesting special case of Theorem 1.1 is that where X is unbounded and the distance function d Ω (x) is replaced by the distance to a fixed point x 0 ∈ X, i.e., we have the inequality It follows from Theorem 1.1 that this inequality holds for all u ∈ Lip 0 (X \ {x 0 }) when co dim A ({x 0 }) > p − β > 1, and in fact, by the below Corollary 6.5, u need not vanish at x 0 , so the inequality actually holds for all u ∈ Lip b (X). On the other hand, Theorem 1.2 implies that for co dim A ({x 0 }) < p − β inequality (15) is valid for all u ∈ Lip 0 (X \ {x 0 }).
Let us mention here that the lower and upper Assouad codimensions of a point are closely related to the exponent sets of the point x 0 , defined in [5]. Namely, co dim A ({x 0 }) = sup Q(x 0 ) and co dim A ({x 0 }) = inf Q(x 0 ) (see [5] for the definitions of the Q-sets).
In the Heisenberg group H n , which is one particular example of a metric space satisfying our general assumptions, an inequality of the type (15) was recently obtained by Yang [34] using a completely different approach. Since co dim A ({0}) = Q := 2n + 2 for 0 ∈ H n , inequality (3.6) in [34] corresponds exactly to inequality (15), for x 0 = 0, under the condition 1 < p < co dim A ({x 0 }) ; notice that Theorem 1.1 in [34] only records the unweighted case β = 0. However, the requirement p − β > 1 is not needed in [34], and the inequality is established even with the sharp constant p/(Q − p + β) p . With our techniques there is no hope of obtaining any sharpness for the constants.
Recall also that in Euclidean spaces the corresponding well-known inequality, i.e.
with the optimal constant C = p/|n − p + β| p , follows easily by using the classical 1dimensional weighted Hardy inequalities (cf. [14]) on rays starting from the origin. For p − β < n this inequality holds for all u ∈ Lip b (R n ), and for p − β > n for all u ∈ Lip 0 (R n \ {0}). See also [32] for related inequalities where the distance is taken to a k-dimensional subspace of R n , 1 ≤ k < n.

Upper Assouad codimension and thickness
In this section we establish a connection between the upper Assouad codimension and Hausdorff content density conditions, which might also be of independent interest, and as a consequence obtain a proof for Theorem 1.2. The following lemma is a modification of [25, Lemma 4.1], where a corresponding statement was given in terms of Minkowski contents in Euclidean spaces.
Lemma 5.1. Let E ⊂ X be a closed set and assume that co dim A (E) < q. Then there exists a constant C > 0 such that (16) H µ,q R E ∩ B(w, R) ≥ C R −q µ(B(w, R)) for every w ∈ E and all 0 < R < diam(E).
Proof. Let co dim A (E) < q ′ < q and fix 0 < δ < 1/2 to be chosen a bit later. Let also w ∈ E and 0 < R < diam(E), and denote B 0 = B(w, R) and r k = δ k R.
We begin with a maximal packing

and thus doubling and the fact q
In particular, there exists a constant c 0 > 0, independent of w and R, such that We now choose δ > 0 to be so small that δ q−q ′ < c 0 , whence c 0 δ q ′ > δ q , and so We complete the first step of the proof by defining a measure distribution for the balls B 1 by In the next step, we create a similar measure distribution inside the balls B 1 . As above, we find for each i 1 ∈ I 0 pairwise disjoint balls where the inequality follows from (17) and (18). Notice that since B i 1 ∩ B j 1 = ∅ whenever i 1 = j 1 , and clearly B i 1 i 2 ⊂ B i 1 for every i 2 ∈ I i 1 , we have that all the balls B i 1 i 2 are pairwise disjoint. Continuing the construction in the same way, we find in the k:th step a collection of pairwise disjoint closed balls ..i k−1 and all of radius r k = δ k R, such that Thus q ′ > co dim A (E) and the choice of δ imply We now distribute the measure for the balls B i 1 i 2 ...i k−1 i k as follows: where we used (19) and the recursive assumption that This concludes the general step of the construction.
Next, we define so that E ⊂ E ∩ B 0 is a non-empty compact set (here we need the assumptions that X is complete and E is closed). Using the Carathéodory construction (cf. e.g. [31, pp. 54-55]) for the set function ν, we obtain a Borel regular measureν which is supported on E and satisfiesν(B i 1 i 2 ...i k ) = ν(B i 1 i 2 ...i k ) for all of the balls in the construction (see also [9, pp. 13-14]). If x ∈ E and 0 < r < R, we choose k ∈ N such that Rδ k = r k ≤ r < Rδ k−1 . Then there exists a constant C 1 > 0 (depending on the doubling constant and δ) such that B(x, r) intersects at most C 1 of the balls B i 1 i 2 ...i k from the k:th step of the construction; let these be B ′ 1 , . . . , B ′ N . Since these balls are pairwise disjoint and cover E ∩ B(x, r), we have by (20) that Finally, let {B(z i , r i )} i be a cover of E ∩ B 0 with balls of radii 0 < r i < R. Using (21), we conclude that and so taking the infimum over all such covers yields Consequently, we obtain a characterization for the upper Assouad codimension (of closed sets) in terms of Hausdorff content density: Corollary 5.2. Let E ⊂ X be a closed set. Then co dim A (E) is the infimum of all q ≥ 0 for which there exists C ≥ 0 such that (16) holds for every w ∈ E and all 0 < R < diam(E).
Proof. Let q ≥ 0 be such that (16) holds for every w ∈ E and 0 < R < diam(E), and let {B i } be a maximal packing of E ∩ B(w, R/2) with balls of radius 0 < r < R. Then {2B i } is a cover of E ∩ B(w, R/2), and so the doubling condition and (16) imply µ(B(w, R)).

Thus co dim A (E) gives a lower bound for exponents satisfying (16).
On the other hand, Lemma 5.1 shows that there can not be a larger lower bound for these q, and thus co dim A (E) is the infimum, as was required.
Proof of Theorem 1.2. We assumed that co dim A (Ω c ) < p − β and p − β > 1, and thus we can choose q > 1 so that co dim A (Ω c ) < q < p − β. Lemma 5.1 then implies that (22) H µ,q R Ω c ∩ B(w, R) ≥ C R −q µ(B(w, R)) for every w ∈ Ω c and all 0 < R < diam(Ω c ), with a constant C > 0 independent of w and R. By [26,Thm. 4.1], this condition is sufficient for Ω to admit a (p, β)-Hardy inequality, as desired. The following remarks are however in order here: The condition in [26,Thm. 4.1] actually requires that ∂Ω). Recall from Section 2 that the validity of a Poincaré inequality implies that X is quasiconvex, and thus dist(x, ∂Ω) ≃ dist(x, Ω c ), and so the different distance function causes no problems here. Moreover, inspecting the proofs in [26] one sees that for β ≤ 0 the assumption (23) can be replaced in Lemma 3.1(a) of [26] by the condition (22). One subtlety here is the case when Ω is unbounded, since then (22) needs to hold for arbitrarily large radii, and thus we need the additional assumption that in this case Ω c is unbounded as well. Once Lemma 3.1(a) of [26] is established, the (p, β)inequalities for β > 0 follow just like in the proof of [26,Thm. 4.1]; the idea is the same as in the proof of Theorem 1.1 of the present paper.
Let us also remark that the proofs in [26] require the validity of a p 0 -Poincaré inequality for 1 ≤ p 0 < p, which is guaranteed by the self-improvement result of Keith and Zhong [18].
Remark 5.3. Actually both the inner boundary density of (23) and the complement density (22), with an exponent 1 ≤ q < p, are equivalent to the uniform p-fatness of Ω c , see [19]. The deep fact that (also) uniform fatness is a self-improving condition (see [29,7]) is essential in the necessity part of this claim.

Necessary conditions
In this section we extend the previously known necessary conditions for Hardy inequalities to cover also weighted Hardy inequalities in metric measure spaces. In [21] and [28], such conditions were obtained in the unweighted case β = 0 in metric spaces, and in [23] for weighted inequalities in the Euclidean setting.
Recall that our sufficient conditions for Hardy inequalities were given in terms of co dim A (Ω c ) and co dim A (Ω c ). However, in the above necessary conditions it is not possible to replace co dim H by the (larger) co dim A in either of the theorems, cf. the discussion in Section 8. Also the assumption β = p is essential in both of the theorems, as the result need not hold for the (p, p)-Hardy inequality, see [23]. On the other hand, for β > p the claims reduce to trivialities, since always co dim A (E) ≥ 0.
One important ingredient in the proofs of these necessary conditions is the following self-improvement result for Hardy inequalities. Proposition 6.3. Let 1 < p < ∞ and β 0 ∈ R, and assume that Ω ⊂ X admits a (p, β 0 )-Hardy inequality. Then there exists ε > 0, depending only on the given data, such that Ω admits (q, β)-Hardy inequalities whenever p − ε < q < p + ε and β 0 − ε < β < β 0 + ε. Moreover, the constant C > 0 in all these Hardy inequalities can be chosen to be independent of q and β.
The proof of Proposition 6.3 is almost identical to the Euclidean case [24, Thm. 1.1], so we omit the details. In fact, the proof in [24] is based on the proof of the unweighted case from [21], where the result was established in the setting of metric spaces satisfying the doubling and Poincaré conditions.
More specifically, we need for the proofs of Theorems 6.1 and 6.2 only the fact that a (p, β 0 )-Hardy inequality implies (q, β 0 )-Hardy inequalities for some 1 < q < p. This transition from p to q < p is by far the deepest of the self-improvements contained in Proposition 6.3 and, in addition, this is actually the only place in the proofs of Theorems 6.1 and 6.2 where Poincaré inequalities are needed. Notice that in [24] the use of Poincaré inequalities is not explicit, but they are needed in the proof of Theorem 3.1 in [24] for the pointwise estimate u (x)) 1/p 0 + (M d Ω (x)/2 g p 0 u (y)) 1/p 0 , which holds for a.e. x, y ∈ Ω (cf. also [21, Lemma 2.1]). Here g u is an upper gradient of u ∈ Lip 0 (Ω) and 1 ≤ p 0 < p is again such that X supports a p 0 -Poincaré inequality; in R n it is of course possible to take p 0 = 1. In this connection we also note that instead of the set G λ in the proof of Theorem 3.1 in [24], we should now use (as in [21]) the set G λ = {x ∈ Ω : M d Ω (x)/2 g p 0 u ≤ λ p 0 }. Another fact that we need in the proofs of Theorems 6.1 and 6.2 is that if a part of the complement of Ω is small enough, then the test functions for the Hardy inequalities need not vanish in that particular part of Ω c . Lemma 6.4. Let 1 ≤ p < ∞ and β ≥ 0, and assume that Ω ⊂ X admits a (p, β)-Hardy inequality with a constant C 0 > 0. Assume further that U ⊂ X is an open set such that Then a (p, β)-Hardy inequality holds for all u ∈ Lip 0 (Ω ∪ U ) with a constant C 1 = C 1 (C 0 , p) > 0.
Proof. Let u ∈ Lip 0 (Ω ∪ U ) with an upper gradient g u . By the definition of H µ,p−β r , there then exists, for a fixed j ∈ N, balls B We define cut-off functions ψ j (x) = min i {1, r i −1 d(x, 2B j i )} and set u j = ψ j u. Then is an upper gradient of ψ j , u j ∈ Lip 0 (Ω), and g u j = g ψ j |u| + g u is an upper gradient of u j (cf. [4,Thm. 2.15]). We may in addition assume that u j ≤ u j+1 for each j ∈ N.
Since β ≥ 0, we have d Ω (y) β ≤ r β i for all y ∈ B j i , and thus the (p, β)-Hardy inequality for the functions u j and the choice of the balls B j i imply that where C = C(C 0 , p) > 0. The claim now follows by monotone convergence, since u j (x) → u(x) for a.e. x ∈ Ω.
Let us record here the following consequence of the previous lemma, which gives an improvement to Theorem 1.1: Corollary 6.5. Let 1 ≤ p < ∞ and β < p − 1, and assume that X and Ω are as in Theorem 1.1, in particular that co dim A (Ω c ) > p − β. Then a (p, β)-Hardy inequality holds for all u ∈ Lip b (Ω).
Proof. For β < 0 the claim follows directly from Proposition 3.1. For β ≥ 0 we have by Theorem 1.1 that Ω admits a (p, β)-Hardy inequality. We now choose U = ( and Ω c ⊂ U , it follows in particular that H µ,p−β diam(U ) (U ∩ Ω c ) = 0. Thus Lemma 6.4 implies that actually the (p, β)-Hardy inequality holds for all Lip 0 (Ω ∪ U ) = Lip b (X), and the claim follows. Lemma 6.4 and the self-improvement of the Aikawa condition from Lemma 2.2 now yield the following result, which is basically a "weighted" version of [21,Lemma 2.4]. For Euclidean spaces the same result can be found in [23, Lemma 5.2], but note that there the proof is different and especially avoids the use of Gehring's Lemma, thus making the proof therein more self-contained. The approach of [23] could be used in the present setting of metric spaces as well, but we chose instead to follow the outline of the proofs from [21] for the sake of brevity and also to emphasize the role of the the self-improvement result of Lemma 2.2. Lemma 6.6. Let 1 ≤ p < ∞, 0 ≤ β < p, and assume that Ω ⊂ X admits a (p, β)-Hardy inequality. Assume further that B 0 = B(x 0 , R) ⊂ X is an open ball such that H µ,p−β R (2B 0 ∩ Ω c ) = 0. Then there exists δ > 0, depending only on the given data, such that co dim A (B 0 ∩ Ω c ) ≥ p − β + δ.
We have now established enough tools to prove Theorem 6.2. The proof follows the lines of the proofs of [21, Corollary 2.7] and [28, Thm. 6.2], but we present the main ideas here for the convenience of the reader.
Proof of Theorem 6.2. Let B 0 = B(x 0 , R) ⊂ X. If β > p, it is clear that the lower bound for the lower Aikawa codimension holds, and so we only need to consider the case β < p. First of all, we may assume that β ≥ 0. Indeed, if this is not the case, we have, by [24,Thm. 2.2], that Ω admits a (p − β, 0)-Hardy inequality with a constant depending only on the data, and now we may consider this instead of the original (p, β)-Hardy inequality. Notice that even though [24,Thm. 2.2] is written in Euclidean spaces, the proof applies almost verbatim in metric spaces, and moreover this shift from p to p−β causes no troubles with respect to the Poincaré inequalities, since the assumed p-Poincaré inequality implies (p − β)-Poincaré inequalities for all β < 0.
By the self-improvement of Hardy inequalities from Proposition 6.3, we find ε 1 > 0 and C 1 > 0 such that Ω admits (q, β)-Hardy inequalities for all p − ε 1 < q ≤ p, and moreover the constants in all these inequalities can be taken to be C 1 . In addition, we require that Let then 0 < ε < ε 1 /2 to be specified later. If co dim H (2B 0 ∩ Ω c ) < p − β − ε, the claim holds, and thus we may assume that co dim As Ω admits a (p−2ε, β)-Hardy inequality and p−2ε > p−ε 1 ≥ β, we may use Lemma 6.6 to conclude that there exists δ > 0, independent of the particular choice of ε < ε 1 /2 (cf. Remark 6.7), such that We now choose ε < min{ε 1 /2, δ/3}, and the claim follows.
The global dimension dichotomy in Theorem 6.1 follows along the same lines as above: If Ω admits a (p, β)-Hardy inequality for 0 ≤ β < p, and if in addition co dim H (Ω c ) ≥ p−β−ε, we obtain from Lemma 6.6 that B d Ω (x) −p+2ε+β−δ dx ≤ Cµ(B)r −p+2ε+β−δ for any ball B = B(w, r) with w ∈ Ω c and r > 0, where C and δ are independent of B and the particular ε. Choosing ε as in the proof of Theorem 6.2 yields the claim of Theorem 6.1.

7.
Combining thick and thin parts of the complement Theorem 1.1 gives a sufficient condition for Hardy inequalities in the case where the complement of Ω is thin. Conversely, Theorem 1.2 gives such a condition in the case where the complement is thick (everywhere and at all scales). Nevertheless, requiring the whole complement to be either thick or thin rules out all cases where the complement contains both large and small pieces; an easy (and well-understood) example is the punctured ball B(0, 1) \ {0} ⊂ R n . In the next proposition we show how it is possible to combine the results of Theorems 1.1 and 1.2 for this kind of domains. A slightly different approach to Hardy inequalities in such domains, for β = 0, was given in [27,Section 5], and in the Euclidean case, earlier results for weighted inequalities can be found in [23]; both of these require additional accessibility properties for Ω. On the other hand, the results from [23] also cover the case β ≥ p − 1, where such extra conditions are known to be indispensable (cf. Section 8).
Since this result (in this generality) is new even in Euclidean spaces, let us formulate this special case as a corollary: Corollary 7.2. Let 1 ≤ p < ∞ and β < p − 1, and assume that Ω 0 ⊂ R n is an open set satisfying dim A (Ω c ) > n − p + β. If F ⊂ Ω 0 is a closed set with dim A (F ) < n − p + β, a (p, β)-Hardy inequality holds for all u ∈ Lip 0 (Ω 0 ).
Proof of Proposition 7.1. Again, it suffices to prove the claim for β < 0, with a suitable control for the constant, since then the claim for β ≥ 0 follows just as in the proof of Theorem 1.1 from Section 4. For β < 0, the idea is to modify the proof of Proposition 3.1.
In the present case we can no longer "chain to infinity" as in (3), but we can instead use for sufficiently large balls centered at F the fact that a (p, β)-Hardy inequality holds in Ω 0 .
Let W(Ω 0 ) be a Whitney-type cover of Ω 0 with balls B(x, cd Ω (x)), x ∈ Ω, where 0 < c < 1/2 is such that the balls 3λB, B ∈ W(Ω), have a uniformly bounded overlap (see e.g. [6]); here λ is the dilatation constant from the Poincaré ineqaulity. Let Without loss of generality, let us first consider E := B 1 ∩ F , where B 1 = B(x, R). Choose k 0 ∈ Z so that 3 · 2 k 0 < R ≤ 3 · 2 k 0 +1 , and let B k , N k , and A k for k ≤ k 0 be just as in the proof of Proposition 3.1 for this set E. Then 3B ⊂ 3B 1 for all B ∈ B k 0 . Now let u ∈ Lip 0 (Ω). We divide each B k , k ≤ k 0 , into two subsets as follows: We set for B ∈ B k that B ∈ B (1) k if |u 3B 1 | ≤ 1 2 |u 3B |, and otherwise B ∈ B (2) k . For convenience, we also set B k 0 +1 = B (1) This is analogous to estimate (3), and, indeed, the balls B ∈ B (1) k can be treated just like in the proof of Proposition 3.1, yielding as in (8) and (9) that On the other hand, for the balls B ∈ B (2) k , k ≤ k 0 , we have by Hölder's inequality that , and since d Ω 0 (y) ≃ R for all y ∈ 3B 1 , we obtain (26) |u Now pick q such that co dim A (E) > q > p − β, whence by definition Estimate (26) and the bounded overlap of the balls 3B, for B ∈ B (2) k with a fixed k, then imply since q + β − p > 0. Following the proof of Proposition 3.1, we can now combine (25) and (27), and we obtain as in (6) that Note that here A k 0 +1 = 3B 1 \ N k 0 , and that the first integral in the second line can be estimated just like in (7). Similar estimates hold of course for all balls B 1 , B 2 , . . . , with constants independent of i. Moreover, if x ∈ Ω \ i 3B i , then d Ω (x) ≥ Cd Ω 0 (x): If d Ω (x) = d Ω 0 (x) the claim is trivial, so we may assume that d Ω (x) = d(x, w) for some w ∈ F . Pick B i ∋ w. Since x / ∈ 3B i and rad(B i ) ≥cd(w, Ω 0 ) with 0 <c < 1, it follows that and the claim follows. In particular d Ω (x) β−p ≤ Cd Ω 0 (x) β−p for these x, and thus estimate (28) for each B i , the (p, β)-Hardy inequality for Ω 0 , and the bounded overlap of the balls 3λB i (and thus of 3B i ) yield we also used the facts that d Ω 0 (x) β ≤ d Ω (x) β for all x ∈ Ω (since β < 0) and that µ(F ) = 0.
The above constant C > 0 naturally depends on β, but for −1 < β < 0 close enough to 0, the dependence can be reduced again to the form C = |β| −1 C * , where C * > 0 is independent of β; the details are exactly the same as in the proof Proposition 3.1. Hence (p, β)-Hardy inequalities, for 0 ≤ β < p − 1, now follow along the same lines as in the proof of the corresponding case of Theorem 1.1 (cf. Section 4).
Finally, regarding the boundary values, we see that in the above proof of the case β < 0 it is not necessary for u to vanish in F , and thus this case indeed holds for all u ∈ Lip 0 (Ω 0 ). On the other hand, in the case β ≥ 0 we can apply Lemma 6.4 with U = Ω 0 (since p − β < co dim A (F ) ≤ co dim H (Ω 0 ∩ Ω c )), and it follows again that a (p, β)-Hardy inequality holds for all u ∈ Lip 0 (Ω ∪ Ω 0 ) = Lip 0 (Ω 0 ). This concludes the proof.

Sharpness of the results
We close the paper with an examination of the sharpness of our results. In particular, we consider the necessity of the assumptions in our main theorems.
It was already mentioned in the introduction that the bound p − β is very natural for the dimensions in all of the sufficient and necessary conditions, and can not be improved. Moreover, the bound p − β for the lower Assouad codimension appears both in the sufficient and necessary conditions (and also in the local formulations) in the cases where the complement is (locally) thin, and so it is obvious that co dim A is the optimal concept of dimension in this setting. However, when (a part of) the complement is thick, the sufficient conditions are given in terms of the upper Assouad codimension, while in the necessary conditions the Hausdorff codimension is used, and so these conditions do not quite meet. This raises the question whether it could be possible to improve the bounds in either sufficient or necessary conditions by using a different concept of dimension. Since the possibility to combine thick and thin parts in the sufficient conditions (Proposition 7.1) immediately rules out such improvements in the global results, the sharpness of the conditions (in terms of dimensions) is established if we show that (i) co dim H (2B 0 ∩ Ω c ) can not be replaced by co dim A (2B 0 ∩ Ω c ) in Theorem 6.2, and that on the other hand (ii) the local bound co dim H (B ∩ Ω c ) < p − β, for all balls B(w, r) with w ∈ Ω c , does not suffice for the (p, β)-Hardy inequality in Ω.
The following construction yields a counterexample for both (i) and (ii).
Next, we replace each point w j with a ball B j = B(w j , 2 −2j ), that is, we consider the domain Ω = B(0, 2) \ j≥2 B j ∪ {0} . Then also Ω admits p-Hardy inequalities whenever 1 < p < n; this can be seen as follows: Write Ω ′ = B(0, 2) \ j≥2 2B j ∪ {0} and Ω ′′ = j≥2 2B j \ B j , so that Ω = Ω ′ ∪ Ω ′′ . For all x ∈ Ω ′ ⊂ Ω 1 we have d Ω 1 (x) ≥ d Ω (x) ≥ 1 2 d Ω 1 (x), and thus, using the p-Hardy inequality in Ω 1 , we have for all u ∈ Lip 0 (Ω) ⊂ Lip 0 (Ω 1 ) that On the other hand, for all x ∈ Ω ′′ the complement of Ω near x satisfies the density condition H n (Ω c ∩ B(x, 2d Ω (x))) ≥ Cd Ω (x) n , and so it follows that even a stronger pointwise 1-Hardy inequality holds for these points (cf. [12,19]): If u ∈ Lip 0 (Ω) and x ∈ Ω ′′ , then |u(x)| ≤ Cd Ω (x)M 2d Ω (x) |∇u|(x), where M 2d Ω (x) is the restricted Hardy-Littlewood maximal operator. The L p -boundedness of M 2d Ω (x) then yields and so the p-Hardy inequality for Ω, for every 1 < p < n, follows by combining (29) and (30). Nevertheless, while it is clear that co dim H (Ω c ∩ B(w, r)) = co dim A (Ω c ∩ B(w, r)) = 0 whenever w ∈ Ω c , and thus in particular co dim H (Ω c ∩ B(w, r)) < p in accordance with Theorem 6.2, this example shows that co dim H cannot be replaced with co dim A in the theorem: Indeed, we see that for all balls B centered at the origin, e.g. for B = B(0, 1/2), we have co dim A (Ω c ∩B) = n (i.e. dim A (Ω c ∩B) = 0), since for all j ≥ 3 the set Ω c ∩2 j B j ⊂ B can be covered by the ball B j , and here the ratio of the radii of the balls has no positive lower bound. Hence neither of the estimates co dim A (Ω c ∩2B) < p and co dim A (Ω c ∩B) > p holds here when 1 < p < n, even though Ω admits a p-Hardy inequality. We conclude that in general co dim H (Ω c ∩ 2B) < p − β is the best counterpart for co dim A (Ω c ∩ B) > p − β in Theorem 6.2, and clearly the same conclusion holds for Theorem 6.1 as well.
Moreover, since co dim H (Ω c ∩ B(w, r)) = 0 < n whenever w ∈ Ω c but still the n-Hardy inequality fails in Ω, this example also shows that the uniformity provided by the upper Assouad codimension in the sufficient conditions of Theorem 1.2 and Proposition 7.1 is essential.
Let us next take a look at the the requirement p − β > 1 in Theorems 1.1 and 1.2. It was already mentioned in the Introduction that for Theorem 1.2, the unit ball B = B(0, 1) ⊂ R n shows the necessity of this condition, since co dim A (R n \ B) = 0, but B admits (p, β)-Hardy inequalities only when p − β > 1. In fact, it is now understood that in the case p − β ≤ 1 it is the thickness of the boundary (rather than the complement) that plays a role in Hardy inequalities. For instance, the planar domain Ω bounded by the usual von Koch -showflake curve of dimension λ = log 4/ log 3 admits a (p, β)-Hardy inequality if (and only if) β < p − 2 + λ, i.e. exactly when p − β > co dim A (∂Ω) (cf. [20]). However, the requirement co dim A (∂Ω) < p − β alone is not sufficient for a (p, β)-Hardy inequality if co dim A (∂Ω) < 1, as is shown by [20,Examples 7.4 and 7.4], but certain accessibility conditions are required in addition; cf. [20,26] and see also Remark 4.1.
In the case of Theorem 1.1, the unbounded domain Ω s indicated at the end of [23, Example 6.3] serves as an example where 1 = co dim A (R n \ Ω s ), but the domain does not admit any (p, β)-Hardy inequalities when p − β ≤ 1. Nevertheless, all known counterexamples here are such that co dim A (Ω c ) ≤ 1 (and thus in the examples in R n we have dim A (Ω c ) ≥ n − 1), and so it could be asked if the requirement p − β > 1 could actually be removed (or weakened) if co dim A (Ω c ) > 1. Under additional accessibility conditions Hardy inequalities can be obtained in the range p − β ≤ 1 in the case of thin boundaries as well; see [23] for the Euclidean case.
Shanmugalingam for fruitful discussions, and the whole department for their hospitality. Thanks are also due to Antti Käenmäki for discussions related to Section 5.