Update on 3/1/2020. All the material below and much more has been incorporated into a comprehensive article on this topic. The question below is discussed in that article, entitled "State-of-the-Art Statistical Science to Tackle Famous Number Theory Conjectures", and available here.
Let $W$ be a word (also called block) consisting $k$ binary digits. Let $S$ be a sequence (also called text or book) consisting of $m$ binary digits, with $k\leq m$. Let $N_{W,S}$ be the number of occurrences of $W$ in $S$. For instance, if $S=010001010011$ and $W=00$, then $N_{W,S} = 3$.
Here $k$ is small and $m$ is large: $k=5$ and $m=20,000$ in my experiment.
For a positive integer $x$, a block $W$ of length $k$ and a random sequence $S$ of length $m$, the number of occurrences of the event $\{N_{W,S} = x\}$ is denoted as $P(N_{W,S} = x)$. So $x$ can be viewed as the realization of a discrete random variable $X$. In particular, $$\mbox{E}(X) = \frac{m-k+1}{2^k}.$$
Let $Z = (X-\mbox{E}(X))/\sqrt{\mbox{Var}(X)}.$
Question: what is the limiting distribution of $Z$, as $m\rightarrow\infty$?
Using simulations, I got a pretty decent approximation. Below is its empirical distribution:
It is perfectly smooth and symmetric at the limit, but the excess kurtosis is $0.63$, so it is not a normal distribution. The empirical percentile distribution of $Z$ is in the table below, maybe there is an almost perfect fit with some known distribution with 0 mean and unit variance.
P(Z < x) x
0.01 -2.36
0.02 -2.03
0.03 -1.87
0.04 -1.71
0.05 -1.62
0.06 -1.50
0.07 -1.46
0.08 -1.38
0.09 -1.30
0.10 -1.26
0.11 -1.18
0.12 -1.13
0.13 -1.09
0.14 -1.05
0.15 -1.01
0.16 -0.97
0.17 -0.93
0.18 -0.89
0.19 -0.85
0.20 -0.81
0.21 -0.77
0.22 -0.77
0.23 -0.73
0.24 -0.69
0.25 -0.65
0.26 -0.60
0.27 -0.60
0.28 -0.56
0.29 -0.52
0.30 -0.52
0.31 -0.48
0.32 -0.44
0.33 -0.44
0.34 -0.40
0.35 -0.36
0.36 -0.36
0.37 -0.32
0.38 -0.28
0.39 -0.28
0.40 -0.24
0.41 -0.24
0.42 -0.20
0.43 -0.16
0.44 -0.16
0.45 -0.11
0.46 -0.11
0.47 -0.07
0.48 -0.03
0.49 -0.03
0.50 0.01
0.51 0.01
0.52 0.05
0.53 0.05
0.54 0.09
0.55 0.13
0.56 0.13
0.57 0.17
0.58 0.17
0.59 0.21
0.60 0.25
0.61 0.25
0.62 0.29
0.63 0.33
0.64 0.33
0.65 0.37
0.66 0.37
0.67 0.42
0.68 0.46
0.69 0.46
0.70 0.50
0.71 0.54
0.72 0.54
0.73 0.58
0.74 0.62
0.75 0.66
0.76 0.66
0.77 0.70
0.78 0.74
0.79 0.78
0.80 0.82
0.81 0.82
0.82 0.86
0.83 0.91
0.84 0.95
0.85 0.99
0.86 1.03
0.87 1.11
0.88 1.15
0.89 1.19
0.90 1.23
0.91 1.31
0.92 1.39
0.93 1.44
0.94 1.52
0.95 1.64
0.96 1.72
0.97 1.88
0.98 2.09
0.99 2.46
If instead of one sequence $S$, you consider $n$ random sequences $S_1,\cdots,S_n$ all of same length $m$, and independent from each other, then the variance for the counts $N_{W,S}$, computed across all sequences bundled together, satisfies $$\mbox{Var}(X)\rightarrow\frac{m-k+1}{2^k}\cdot \Big(1-\frac{1}{2^k}\Big) \mbox{ as } n\rightarrow\infty.$$ This result can be used to test if sequences found in actual data sets are both random and independent from each other.
The challenge
The problem is that the successive $m-k+1$ blocks $W$ of length $k$ do overlap in any sequence $S$ of length $m$, resulting in lack of independence between the various counts $N_{W,S}$. If the blocks (and thus their counts) were independent instead, then the counts would follow a multinomial distribution, with each of the $n\cdot (m-k+1)$ probability parameters being $\frac{1}{2^k}$, and $Z$ would be asymptotically normal. Here this is not the case: the excess kurtosis does not converge to zero. There is convergence to smooth, symmetrical distributions as $n$ and $m$ increase, but that limit is never Gaussian. My big question is: what is it then?
That said, for the first two moments (expectation and variance) attached to $N_{W,S}$, we get the same values (at least asymptotically) as those arising from the multinomial model. But not anymore for higher moments.
The following code performs simulations and computes the variances, expectations, kurtosis and all the counts $N_{W,S}$. Note that the variance and kurtosis depend on $S$, but they stabilize as $n$ is increasing. The expectation depends only on $m$ and $k$.
use strict;
my $k;
my $k1;
my $k2;
my $j;
my $rand;
my $m;
my $even;
my $block;
my @digits;
my @ablock;
my @biglist;
my $bigstring;
my $nbigstrings;
my $binary;
my %hash;
my %hlist;
my @blocksum;
my $tweight;
my $sum;
my $sum2;
my $avg;
my $var;
my $kurtosis;
my $num;
my $count;
$rand=500;
$k1=5; # bits of small word
$k2=2**$k1;
$m=7; # bits in big string # m > k1 otherwise var = 0
$nbigstrings=5000; # number of sampled big strings
open(OUT2,">collatzr.txt");
@biglist=();
%hlist=();
for ($bigstring=0; $bigstring<$nbigstrings; $bigstring++) {
@digits=();
@ablock=();
$binary="'";
for ($k=0; $k<$m; $k++) { # compute 200 digits
$rand=(10232193*$rand + 3701101) % 54198451371;
$even=int(2*$rand/54198451371);
@digits[$k]=$even;
$binary=$binary."$even";
}
print OUT2 "\n$binary\n";
for ($k=0; $k<$m-$k1+1; $k++) { ## kmax - 5
$block="";
for ($j=0; $j<$k1; $j++) {
$block+=($digits[$k+$j]* 2**$j);
}
$ablock[$block]++;
}
if ($bigstring%1000 == 0) { print "iter... $bigstring\n"; select()->flush(); }
for ($block=0; $block<$k2; $block++) {
if ($ablock[$block] eq "") { $ablock[$block]=0; }
$count=$ablock[$block];
$hash{$count}++; #{$ablock[$block]}++; # number of occurences of $count (used as weight in AVG, VAR)
$blocksum[$block]+=$count;
$hlist{$block}=$hlist{$block}."\t$count"; # disuse if it uses too much memory
print OUT2 "$block\t$count\n";
}
}
close(OUT2);
#-- summary stats
open(OUT,">coll2.txt");
$tweight=0;
$sum=0;
$sum2=0;
$kurtosis=0;
foreach $count (keys(%hash)) {
$tweight+=$hash{$count};
$sum+=$count*$hash{$count};
$sum2+=$count*$count*$hash{$count};
print "count weight: $count\t$hash{$count}\n";
print OUT "count\tweight\t$count\t$hash{$count}\n";
}
$avg=$sum/$tweight;
$var=($sum2/$tweight)- $avg*$avg;
foreach $count (keys(%hash)) {
$kurtosis+=$hash{$count}*(($count - $avg)/sqrt($var))**4;
}
$kurtosis = -3+$kurtosis/$tweight;
$num = $avg*$k2;
print "($k1 | $m | $nbigstrings) avg ~ sum2| var | excess_kurt | tweight | missing : $avg ~ $sum2 | $var | $kurtosis | $tweight | $hash{0}\n";
for ($block=0; $block<$k2; $block++) {
# print "block: $block\t$blocksum[$block]\n";
print OUT "block\tblocklist\t$block\t$hlist{$block}\n";
}
close(OUT);
Context
I am checking if all blocks of $k=5$ binary digits are distributed as expected (that is, randomly) in the first $m$ binary digits of a bunch of quadratic irrational numbers. To test my hypothesis that this is the case, I need to know the exact distribution of the test statistic for the null hypothesis. The exact distribution is the distribution attached to $Z$. More about this project can be found on Math.StackExchange, here.
10
is $\frac14$ which is smaller than the variance of occurrences of00
of $\frac12$ $\endgroup$