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()))
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))
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]:
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)
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)
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)
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)
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)
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))
In [14]:
selection = d_five_p10_90[['rule', 'pi_deviation', 'mean_deviation', 'p_value_deviation', 'Serial-Correlation']]
In [15]:
selection
Out[15]:
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
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 [ ]: