TITLE: Derivative and calculus over sets such as the rational numbers
QUESTION [22 upvotes]: I am interested in the derivative of a function defined on a subset $S$ of $[0, 1]$. The subset in question is dense in $[0, 1]$ but has Lebesgue measure zero. My actual question can be found at the bottom of this post.
There has been a few questions on the subject, but none leading to something interesting as far as I am concerned. See here, here (concept of arithmetic derivative) and also here (Minkowski's question mark function, related to the material discussed here.) 
Generally these discussions lead to some kind of non-sense math. Here it is the opposite. I have a framework that does work as far as applications and computations are concerned, but I have a hard time putting it into some sound mathematical framework. It would have to be some kind of non-standard calculus.
Perhaps the simplest example (though I have plenty of other similar cases) is as follows. Let $Z$ be a random variable defined as follows:
$$Z = \sum_{k=1}^\infty \frac{X_k}{2^k}$$
where the $X_k$'s are identically and independently
distributed with a Bernouilli$(p)$ distribution. Thus $P(X_k = 1) = p$ and $P(X_k = 0) = 1-p$. Here $0 < p < 1$. In short, the $X_k$'s are the binary digits of the random number $Z$.
There are two cases.
Case $p=\frac{1}{2}$
In this case, $Z$ has a uniform distribution on $S$, where $S$ is the set of normal numbers in $[0, 1]$. It is known that $S$ has Lebesgue measure $1$, and that $S$ is dense in $[0, 1]$. Yet it is full of holes (no rational number is is a normal number due to their periodicity, thus the $X_k$'s are not independent for rational numbers.)
This is the simplest case. One might wonder if the density $f_Z$ (the derivative of the distribution $F_Z$) exists. Yet $f_Z(z) = 1$ if $z \in S$ works perfectly well for all purposes. It can easily be extended to $f_Z(z) = 1$ if $z \in [0, 1]$. Let us denote the extended function as $\tilde{f}_Z$. You can compute all the moments using the extended $\tilde{f}_Z$ and get the right answer. If $s$ is a positive real number, then
$$E(Z^s) = \int_0^1 z^s \tilde{f}_Z(z) dz = \frac{1}{s+1}.$$
You could argue that $\tilde{f}_Z$ (and thus $f_Z$) can be obtained by inverting the above functional equation, using some kind of Laplace transform. So we can bypass the concept of derivative entirely, it seems. 
Case $p\neq \frac{1}{2}$
Now we are dealing with a hard nut to crack, and a wildly chaotic system: $Z$'s support domain is a set $S'$ that is a subset of non-normal numbers in $[0, 1]$. This set $S'$ has now Lebesgue measure zero, yet it is dense in $[0, 1]$. For the distribution, it is not a problem: even discrete random variables have a distribution $F_Z$ defined for all positive real numbers: $F_Z(z) = P(Z \leq z)$. 
The issue is with the density $f_Z = dF_Z/dz$. It sounds either it should be zero everywhere or not exist. My guess is that you might be able to define a new, workable concept of density. In the neighborhood of every point $z \in S'$, it looks like $g(z,h) = (F_Z(z+h) - F_Z(z))/h$ oscillates infinitely many times with no limit as $h\rightarrow 0$, yet these oscillations are bounded most of the time, perhaps leading to the fact that averaging $g(z, h)$ around $h = 0$, using smaller and smaller values of $h$, could provide a sound definition for the density $f_Z$.
Again, despite the chaotic nature of the system (see how the the would-be density could potentially look like in the picture below) all the following quantities exist and can be computed exactly and then confirmed by empirical evidence, even though the integrals below may not make sense:
$$E(Z) = \int_{0}^{1} z f_Z(z) dz = p \\
E(Z^2) = \int_{0}^{1} z^2 f_Z(z) dz =\frac{p}{3}(1+2p)\\
E(Z^3) = \int_{0}^{1} z^3 f_Z(z) dz =\frac{p}{7}(1+4p+2p^2)\\
E(Z^4) = \int_{0}^{1} z^4 f_Z(z) dz =\frac{p}{105}(7+46p + 44p^2+8p^3)
$$
Indeed, a general formula for $E(Z^s) = \int_0^1 z^s f_Z(z)dz$ is available for $s \geq 0$, defined by the following functional equation (see here):
$$E(Z^s) = \frac{p}{2^s-1+p}\cdot E((1+Z)^s) .$$
In other words, we would have, under some appropriate calculus theory with a sound definition of integral and derivative:
$$\int_{S'}z^s f_Z(z)dz  = \frac{p}{2^s-1+p}\cdot\int_{S'}(1+z)^s f_Z(z) dz .$$
Here is how the density $f_Z$, if properly defined, could look like for $p=0.75$ (see here and here):

Below is the empirical percentile distribution, for this particular $Z$: 

Other related problems
If instead, we consider the model $Z = X_1 + X_1 X_2 + X_1 X_2 X_3 + \cdots$ with $X$ Bernouilli$(p)$, then $Z$ has a geometric distribution of parameter $1-p$, see section 2.2 in this article. This system is also equivalent to the binary numeration system discussed so far: see section 5 in the same article. It results in $Z$ having a standard, well-known discrete distribution. But if this time $P(X=-0.5) = 0.5 = P(X=0.5)$ then $Z$ is uniform on a subset $S$ of $[-1, 1]$, with $S$ also full of holes.
Here is another interesting model:
$$Z=\sqrt{X_1+\sqrt{X_2+\sqrt{X_3+\cdots}}}$$
The distribution for $X$ is as follows: 
$$P(X=0) = \frac{1}{2}, P(X=1) = \frac{1}{1 + \sqrt{5}}, P(X=2) = \frac{3 - \sqrt{5}}{4} \mbox{ } (\star)$$ 
This corresponds to a different numeration system, and the choice for $X$'s distribution is not arbitrary, see here: in short, it makes the system smoother and possibly easier to solve. To the contrary $P(X=0) = P(X=1)=$ $P(X=2)= \frac{1}{3}$ yields a far more chaotic system, and the case $P(X=0) =$ $P(X=1) = \frac{1}{2}$ is so wild that the support domain of $Z$ has huge gaps, very visible to the naked eye.  
Normal numbers in the nested square root system are very different from normal numbers in the binary numeration system. The successive digits have a very specific auto-correlation structure, and the digits $0, 1, 2$ are not evenly distributed for normal numbers in that system. It is clear that if we assume that the $X_k$'s are i.i.d, then $Z$ is not a normal number in that system. Yet we get a very good approximation for $F_Z$, much smoother (at least visually) than in the binary numeration system investigated earlier, with $p\neq \frac{1}{2}$. In particular, $F_Z$ is very well approximated by a log function, see chart below.

Here the blue line is the empirical distribution for $Z$, the red line is the log approximation. And below is a spectacular chart, featuring the approximation error $\epsilon(z) = F_Z(z) -\log_2(z)$. It's a fractal! (source: see section 2.2 in this article). In short, it is no more differentiable than a Brownian motion, and technically, the derivative $f_Z$ does not exist. Yet all moments of $Z$ can be computed exactly from the functional equation attached to that system ($F_{Z^2}=F_{X+Z}$) and confirmed empirically. Even though the distribution looks smooth to the naked eye, we are dealing here with a very chaotic system in disguise. Again we need non-standard calculus to handle the density, whose support is a dense set of Lebesgue measure zero in $[1, 2]$. 
Since fractals are nowhere differentiable, $f_Z$ does not exist. Yet one could imagine a "density" that would look like $f_Z(z)=\frac{1}{z\log 2}$ for $z\in [1,2]$.

My question
Is there an existing theory to handle this type of density-like-substance? Following some advice, I also posted this question on MathOverflow, here.

REPLY [5 votes]: Measure-theoretic probability provides a formalism in which all quantities in your question are rigorously defined. In my opinion this is the natural framework to use (instead of introducing an ad-hoc approach) since you have already invoked measure theory in your question!
Even better, there is substantial (and beautiful) literature studying the exact same fractal systems you are interested in - read to the bottom of this answer (skimming the pedantic bits you probably already know) to find out more.
TL;DR The answer to your question

Is there an existing theory to handle this type of density-like-substance? 

is a resounding Yes!

Some background
In measure-theoretic probability, one starts with a sample space $(\Omega,\mathcal F)$ which is an arbitrary set equipped with a $\sigma$-algebra. In this case, it is natural to take $\Omega=\{0,1\}^{\mathbb N}$ and use the product $\sigma$-algebra. Equip $(\Omega,\mathcal F)$ with the measure $\mathbb P_p=\textrm{Ber}_p^{\mathbb N}$ which is the product of $p$-Bernoulli measures as you have described.
In this formalism, a random variable is nothing other than a measurable function $f\colon \Omega\to\mathbb R$ where $\mathbb R$ is equipped with the Borel $\sigma$-algebra. In particular, we have a random variable given by
$$
Z(\omega_1,\omega_2,\ldots)=\sum_{n=1}^{\infty}\frac{\omega_n}{2^n}.
$$
It is a measurable function since it is a limit of measurable functions (namely, each partial sum is straightforwardly seen to be measurable).
Since $\mathbb P_p(0\leq Z\leq 1)=1$, the random variable $Z$ is seen to be integrable, so that the mean $\mathbb EZ$ is well-defined. By the same token, $\mathbb E[Z^k]$ is well-defined for all integers $k\geq 0$.
This is not just abstract nonsense: it leads to an operational calculus of the kind you seek. For instance, we have the well-known tail integral for moments of a random variable given by
$$
\mathbb E[Z^k]=\int_0^{\infty}kZ^{k-1}\mathbb P_p(Z>t)\ dt,\qquad k\in\mathbb N,
$$
which is a special case of Tonelli's theorem.
The distribution of the random variable $Z$, let's call it $\mu_p$, is defined to be the pushforward of the measure $\mathbb P_p$ under the mapping $Z\colon\Omega\to\mathbb R$. In symbols, $$\mu_p(A)=\mathbb P_p\bigl(Z^{-1}(A)\bigr),$$for all Borel subsets $A$ of $\mathbb R$. It is a probability measure on $\mathbb R$, with support contained in $[0,1]$. As you have pointed out, the density does not exist in general. In fact, by the Radon-Nikodym theorem it exists if and only if the measure $\mu_p$ is absolutely continuous with respect to Lebesgue measure, in which case the density is precisely the Radon-Nikodym derivative of $\mu_p$ with respect to the Lebesgue measure. The power of measure-theoretic probability is that all techniques and formulas that one is accustomed to working with carry over to the language of probability measures and random variables, ceasing to rely upon the existence of a density. (Of course, complications arise and a little adjustment is required - but this forms the core of the modern probabilist's arsenal of tools.) Thus one has the Lebesgue integral
$$
\mathbb E[Z^k]=\int_0^{\infty}t^{k}\ d\mu_p(t),\qquad k\in\mathbb N,
$$
regardless of whether $\mu_p$ possesses a density.

Modern research
With all this being said, it is still a very interesting question (related to open lines of current research) to understand when the density exists, and what are its fractal properties. I refer you to the survey paper by Peres, Schlag, and Solomyak entitled "Sixty Years of Bernoulli Convolutions", where a closely related generalization of your $p=\tfrac12$ case is considered, namely to the random variable
$$
Z_\lambda(\omega_1,\omega_2,\ldots)=\sum_{n=1}^{\infty}\omega_n\lambda^n,\qquad \lambda\in[0,1].
$$
The distribution $\nu_\lambda$ of the random variable $Z_\lambda$ has fascinating properties when $\lambda\in(\tfrac12,1)$, and the study of these properties leads to beautiful connections with "harmonic analysis, the theory of algebraic numbers, dynamical systems, and Hausdorff dimension
estimation" (direct quote from the abstract).
The fractal nature of these measures is a prominent motif throughout this line of inquiry, and appears at the forefront in the article Self-similar measures and intersections of Cantor sets written by a subset of the authors of the survey paper, which goes beyond the $p=\tfrac12$ case to study the measure $\nu_{\lambda}^{p}:=(Z_\lambda)_*(\mathbb P_p)$, of which the special case $\lambda=\tfrac12$ is the distribution of your random variable $Z$.
The aforementioned survey article is featured prominently in the proceedings of an international conference on fractal geometry, collected in the (unfortunately non-freely available) book Fractal Geometry and Stochastics II, which serves as a jumping off point to the literature on dynamical systems leading to fractal geometries closesly related to your list of "other related problems" as well. Note that many of the individual articles collected in this book (and its bibliography) are freely available at either the authors' webpages or on preprint servers.

Broader picture
To make contact with the answer posted by Milo here, let me point out that the theory of measures may be regarded as a special case of the theory of generalized functions - i.e., every measure is a generalized function with respect to the class of bounded continuous test functions (with the Lebesgue integral supplying the pairing between measure and test function). 
My perspective is that measure theory is (broadly speaking) a more developed field than that of generalized functions, for the same reason that continuous functions are better understood than measures. What I mean is that one can group the objects of study in real analysis by how "well-behaved" the objects are. After linear functions, the best behaved functions are polynomials, after which comes (in order of increasing generality) analytic functions, then smooth functions, then continuous functions, then measurable functions, then measures, and finally generalized functions. (Everything function $f$ that appears before measures in this list can be canonically associated with the measure $f\cdot \textrm{Lebesgue}$; and measures can be regarded as generalized functions as already explained - so everything is on the same footing, just with varying degrees of regularity.)
Fortunately the problems you are interested in (and all the different flavors of dynamical systems, including ergodic theory) live at (or below) the "measure theory" rung on the ladder of abstractions.