In [2]:
import json
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from scipy.stats import chisqprob
from gmpy2 import digits

import recipe

In [3]:
s_rands_paper = recipe.rands_paper
d_five = recipe.five
d_five['Entropy_norm'] = d_five['Entropy'] / 8
d_five['Entropy'] = d_five['Entropy_norm']
d_five = d_five.drop('Entropy_norm', axis=1)

d_five['p_value_deviation'] = np.abs(d_five['p-value'] - 0.5)

In [4]:
d_five_p10_90 = d_five[(d_five['p-value'] > 0.1) & (d_five['p-value'] < 0.9)]
d_five_p05_95 = d_five[(d_five['p-value'] > 0.05) & (d_five['p-value'] < 0.95)]
d_rands_paper = d_five[d_five.rule.isin(s_rands_paper)]

len_five_p10_90 = len(d_five_p10_90)
len_five_p05_95 = len(d_five_p05_95)
len_rands_paper = len(d_rands_paper)

print("Random according to paper: #%d " % len_rands_paper, end="")
print(list(d_rands_paper.rule.sort_values()))
print("Between  5 - 95%%: #%d " % len_five_p05_95, end="")
print(list(d_five_p05_95.rule.sort_values()))
print("Between 10 - 90%%: #%d " % len_five_p10_90, end="")
print(list(d_five_p10_90.rule.sort_values()))


Random according to paper: #28 [15, 30, 45, 60, 75, 85, 86, 89, 90, 101, 102, 105, 106, 120, 135, 149, 150, 153, 154, 165, 166, 169, 170, 180, 195, 210, 225, 240]
Between  5 - 95%: #21 [30, 45, 60, 75, 86, 90, 101, 102, 105, 106, 120, 122, 135, 149, 150, 153, 161, 165, 169, 195, 225]
Between 10 - 90%: #19 [30, 45, 60, 75, 86, 90, 102, 105, 106, 122, 135, 149, 150, 153, 161, 165, 169, 195, 225]

In [5]:
s_five_p05_p95 = set(d_five_p05_95.rule)
s_five_p10_p90 = set(d_five_p10_90.rule)

print("Random from paper but not according to us: ", end="") 
print(set(d_rands_paper.rule) - set(d_five_p10_90.rule))

print()

print("Random from paper but not according to us: ", end="") 
print(set(d_five_p10_90.rule) - set(d_rands_paper.rule))


Random from paper but not according to us: {101, 166, 170, 15, 240, 210, 180, 85, 120, 89, 154}

Random from paper but not according to us: {161, 122}

In [6]:
d_five[d_five.rule.isin(set(d_rands_paper.rule) - set(d_five_p10_90.rule))][['Entropy', 'mean_deviation', 'p-value']]


Out[6]:
Entropy mean_deviation p-value
15 0.903346 4.724540 0.000000
85 0.872812 0.162692 0.000000
89 0.999961 0.089662 0.950925
101 0.999960 0.053356 0.943729
120 0.999947 0.171428 0.050093
154 0.905389 0.104222 0.000000
166 0.908783 6.754538 0.000000
170 0.880623 3.419920 0.000000
180 0.888805 1.506982 0.000000
210 0.891130 12.487088 0.000000
240 0.902752 11.236362 0.000000

Plots

Entropy vs langton

blue = all, red = our random


In [7]:
# Plot Entropy of all rules against the langton parameter
ax1 = plt.gca()
d_five.plot("langton", "Entropy", ax=ax1, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Entropy", ax=ax1, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax1 = plt.gca()
d_five.plot("langton", "Entropy", ax=ax1, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Entropy", ax=ax1, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/entropy-langton.png', format='png', dpi=400)

ax1 = plt.gca()
d_five.plot("langton", "Entropy", ax=ax1, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Entropy", ax=ax1, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/entropy-langton.svg', format='svg', dpi=400)

Chi-square vs langton

blue = all, red = our random


In [11]:
# Plot Chi-Square of all rules against the langton parameter
ax2 = plt.gca()
d_five.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax2 = plt.gca()
d_five.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/chisquare-langton.png', format='png', dpi=400)

ax2 = plt.gca()
d_five.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Chi-square", ax=ax2, logy=True, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/chisquare-langton.svg', format='svg', dpi=400)

Mean vs langton

blue = all, red = our random


In [10]:
# Plot Mean of all rules against the langton parameter
ax3 = plt.gca()
d_five.plot("langton", "Mean", ax=ax3, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Mean", ax=ax3, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax3 = plt.gca()
d_five.plot("langton", "Mean", ax=ax3, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Mean", ax=ax3, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/mean-langton.png', format='png', dpi=400)

ax3 = plt.gca()
d_five.plot("langton", "Mean", ax=ax3, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Mean", ax=ax3, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/mean-langton.svg', format='svg', dpi=400)

Monte-Carlo-Pi vs langton

blue = all, red = our random


In [30]:
# Plot Monte Carlo of all rules against the langton parameter
ax4 = plt.gca()
d_five.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax4 = plt.gca()
d_five.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/monte-carlo-langton.png', format='png', dpi=400)

ax4 = plt.gca()
d_five.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Monte-Carlo-Pi", ax=ax4, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/monte-carlo-langton.svg', format='svg', dpi=400)

Serial-Correlation vs langton

blue = all, red = our random


In [37]:
# Plot Serial Correlation of all rules against the langton parameter
ax5 = plt.gca()
d_five.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax5 = plt.gca()
d_five.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/serial-correlation-langton.png', format='png', dpi=400)

ax5 = plt.gca()
d_five.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "Serial-Correlation", ax=ax5, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/serial-correlation-langton.svg', format='svg', dpi=400)

p-value vs langton

blue = all, red = our random


In [41]:
# Plot p-value of all rules against the langton parameter
ax6 = plt.gca()
d_five.plot("langton", "p-value", ax=ax6, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "p-value", ax=ax6, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.show()

ax6 = plt.gca()
d_five.plot("langton", "p-value", ax=ax6, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "p-value", ax=ax6, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/p-value-langton.png', format='png', dpi=400)

ax6 = plt.gca()
d_five.plot("langton", "p-value", ax=ax6, kind="scatter", marker='o', alpha=.5, s=40)
d_five_p10_90.plot("langton", "p-value", ax=ax6, kind="scatter", color="r", marker='o', alpha=.5, s=40)
plt.savefig('plots/p-value-langton.svg', format='svg', dpi=400)

In [ ]:


In [ ]:


In [ ]:


In [11]:
# Cutoff rules with high Chi-Square (not random)
d_rands_paper_chi = d_rands_paper[(d_rands_paper["Chi-square"] < 300)] # 300 or 1E5 is same cutoff

print("Number of random rules according to paper: %d" % len(d_rands_paper))
print("Number of paper rules with high Chi-Square: %d " % (len(d_rands_paper) - len(d_rands_paper_chi)), end="")
print(set(d_rands_paper.rule) - set(d_rands_paper_chi.rule))


Number of random rules according to paper: 28
Number of paper rules with high Chi-Square: 8 {166, 170, 15, 240, 210, 180, 85, 154}

In [14]:
selection = d_five_p10_90[['rule', 'pi_deviation', 'mean_deviation', 'p_value_deviation', 'Serial-Correlation']]

In [15]:
selection


Out[15]:
rule pi_deviation mean_deviation p_value_deviation Serial-Correlation
30 30 0.007324 0.009032 0.270481 -0.002542
45 45 0.000836 0.026764 0.290890 0.000934
60 60 0.003140 0.049358 0.215533 0.001424
75 75 0.004052 0.026974 0.224544 -0.001767
86 86 0.008420 0.109408 0.045251 -0.001377
90 90 0.000116 0.064950 0.347723 -0.000760
102 102 0.002716 0.014618 0.367914 0.001276
105 105 0.002140 0.012444 0.395692 0.000286
106 106 0.003284 0.033468 0.388576 0.000740
122 122 0.004828 0.114394 0.227844 -0.000534
135 135 0.017645 0.111598 0.165868 -0.001099
149 149 0.010588 0.205130 0.052170 0.001348
150 150 0.009388 0.132046 0.073749 -0.000408
153 153 0.002660 0.085626 0.295055 -0.000457
161 161 0.008564 0.321102 0.387520 -0.000821
165 165 0.002428 0.085176 0.333809 -0.000892
169 169 0.002708 0.025158 0.312015 -0.000215
195 195 0.001276 0.082446 0.013847 -0.001481
225 225 0.000460 0.028304 0.054775 -0.001858

In [ ]:
p_value_top_10 = selection.sort_values(by='p_value_deviation').head(10)
mean_top_10 = selection.sort_values(by='mean_deviation').head(10)
pi_top_10 = selection.sort_values(by='pi_deviation').head(10)

In [ ]:
print("Top 10 p-value: \t", end="")
print(p_value_top_10.rule.values)
print("Top 10 Mean: \t\t", end="")
print(mean_top_10.rule.values)
print("Top 10 Monte-Carlo-Pi: \t", end="")
print(pi_top_10.rule.values)

print()

print("Both in top 10 p-value and Mean: ", end="")
print(set(p_value_top_10.rule.values) & set(mean_top_10.rule.values))
print("In all three top 10s: ", end="")
print(set(p_value_top_10.rule.values) & set(mean_top_10.rule.values) & set(pi_top_10.rule.values))

In [ ]:
selection[selection.rule.isin(set(p_value_top_10.rule.values) & set(mean_top_10.rule.values) & set(pi_top_10.rule.values))]

In [ ]:
p_value_top_10

In [ ]:
mean_top_10

In [ ]:
pi_top_10

Python's and linux' RNG

Python's random.randint and linux' /dev/urandom


In [ ]:
def read_results(filename):
    results = (File_bytes, Monte_Carlo_Pi, Rule, Serial_Correlation, Entropy, Chi_square, Mean) = [[] for _ in range(7)]
    with open(filename) as f:
        data = json.load(f)
    variables = {"File-bytes": File_bytes, "Monte-Carlo-Pi": Monte_Carlo_Pi, "Rule": Rule, "Serial-Correlation": Serial_Correlation,
                 "Entropy": Entropy, "Chi-square": Chi_square, "Mean": Mean}
    for k, v in variables.items():
        v.append(data[k])
    results = np.array([np.array(r) for r in results]).T
    headers = ["File-bytes", "Monte-Carlo-Pi", "Rule", "Serial-Correlation", "Entropy", "Chi-square", "Mean"]
    return pd.DataFrame(results, columns=headers)

python = read_results('python_1466717839.json')
urandom = read_results('urandom_1466717941.json')

In [ ]:
for d in (python, urandom):
    d["pi_deviation"] = np.abs(d["Monte-Carlo-Pi"] - np.pi)
    d["mean_deviation"] = np.abs(d["Mean"] - 255 / 2)
    d["p-value"] = chisqprob(d["Chi-square"], 255)
    d['Entropy_norm'] = d['Entropy'] / 8
    d['Entropy'] = d['Entropy_norm']
    d['p_value_deviation'] = np.abs(d['p-value'] - 0.5)

In [ ]:
python[['pi_deviation', 'mean_deviation', 'p_value_deviation']]

In [ ]:
urandom[['pi_deviation', 'mean_deviation', 'p_value_deviation']]

In [ ]:
selection

In [ ]: