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](https://math.stackexchange.com/questions/1784875/about-the-derivative-of-a-function-defined-on-rational-numbers), [here](http://oeis.org/wiki/Arithmetic_derivative) (concept of arithmetic derivative) and also [here](https://en.wikipedia.org/wiki/Minkowski%27s_question-mark_function) (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](https://en.wikipedia.org/wiki/Normal_number) 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](https://stats.stackexchange.com/questions/439528/parameter-estimation-when-the-likelihood-function-does-not-exist)):
$$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](https://stats.stackexchange.com/questions/439528/parameter-estimation-when-the-likelihood-function-does-not-exist) and [here](https://www.datasciencecentral.com/profiles/blogs/a-strange-family-of-statistical-distributions)):

[![enter image description here](https://i.stack.imgur.com/HT0ZF.png)](https://i.stack.imgur.com/HT0ZF.png)

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

[![enter image description here](https://i.stack.imgur.com/itiHK.png)](https://i.stack.imgur.com/itiHK.png)

**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](https://www.datasciencecentral.com/profiles/blogs/chaos-attractors-in-machine-learning-systems). 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](https://www.datasciencecentral.com/profiles/blogs/math-fun-infinite-nested-radicals-of-random-variables): 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.

[![enter image description here](https://i.stack.imgur.com/PMOnB.png)](https://i.stack.imgur.com/PMOnB.png)

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](https://www.datasciencecentral.com/profiles/blogs/math-fun-infinite-nested-radicals-of-random-variables)). 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]$.

[![enter image description here](https://i.stack.imgur.com/L9TM1.png)](https://i.stack.imgur.com/L9TM1.png)

**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](https://mathoverflow.net/questions/347799/derivatives-in-unusual-support-domains).