<jupyter_start><jupyter_text>Summary Author: <NAME>Computing the Signal to Noise Ratio Optimal Hamming Distance.<jupyter_code>%load_ext autoreload
%autoreload 2
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom, norm
from scipy.sparse import csc_matrix, coo_matrix, csr_matrix
import pandas as pd
from scipy.integrate import quad
import time
from numpy import sqrt as sqrt
from SDM_Circ_Inter_Funcs import *
from Implementations_Associative_Memory import softmax
n = 64#1000
r = 2**n#10**18#2**n#1000000
m=60000#24
len(str(2**n))<jupyter_output><empty_output><jupyter_text>SNR Optimal<jupyter_code>optimal_p = 1/(2*m*r)**(1/3)
print('Optimal p', optimal_p)
print('Optimal d', space_frac_to_hamm_radius(n, [optimal_p]))<jupyter_output>Optimal p 7.673020240510549e-09
Optimal d [10]<jupyter_text>Memory Capacity Optimal<jupyter_code>def memory_derivative(z,r):
    return 1/2*(z**4/(2*r**4*z**2 + r**3*z**6 + 2*sqrt(r**8*z**4 + r**7*z**8))**(1/3) + (2*r**4*z**2+ r**3*z**6 + 2*sqrt(r**8*z**4 + r**7 *z**8))**(1/3)/r**2 + z**2/r)

# z_score so the overall retrieval is 99%
prob_retrieval_of_whole_pattern = 0.5
prob_per_bit = prob_retrieval_of_whole_pattern**(1/n)
z_score_per_bit = norm.ppf(prob_per_bit)
print('z score per bit', z_score_per_bit)
#z = 2.33 # z-score

optimal_p =  memory_derivative(z_score_per_bit,r)
print('Optimal p', optimal_p)
print('Optimal d', space_frac_to_hamm_radius(n, [optimal_p]))<jupyter_output>z score per bit 2.298313131383402
Optimal p 1.979930592478041e-13
Optimal d [5]<jupyter_text>Memory capacity derivation<jupyter_code>from sympy import *
p, m, r, z = symbols('p m r z')
init_printing(use_unicode=True)

eq = ( (p*r)**2/z**2 - p*r )/( p**2*r + (p**2*r)**2 )
eq
def memory_capacity_equation(z,r,p):
    return ( (p*r)**2/z**2 - p*r )/( p**2*r + (p**2*r)**2 ) +1

r = 1000000
z = 2.33
p = np.logspace(-20,-2)
res = og_eq(z,r,p)
plt.plot(p, res)
print(p[np.argmax(res)])
plt.xscale('log')
plt.ylim(bottom=0, top = np.max(res))
 1/2*(z**4/(2*r**4*z**2 + r**3*z**6 + 2*sqrt(r**8*z**4 + r**7*z**8))**(1/3) + (2*r**4*z**2+ r**3*z**6 + 2*sqrt(r**8*z**4 + r**7 *z**8))**(1/3)/r**2 + z**2/r)
# https://www.wolframalpha.com/input/?i=0+%3D++%28-2+r%5E2+x%5E3+%2B+3+r+x%5E2+z%5E2+%2B+z%5E2%29%2F%28x%5E2+z%5E2+%28r+x%5E2+%2B+1%29%5E2%29+solve+for+x


def wolf_deriv(z,r):
    return 1/2*(z**4/(2*r**4*z**2 + r**3*z**6 + 2*sqrt(r**8*z**4 + r**7*z**8))**(1/3) + (2*r**4*z**2+ r**3*z**6 + 2*sqrt(r**8*z**4 + r**7 *z**8))**(1/3)/r**2 + z**2/r)
z = 2.33
r = 1000000000

wolf_deriv(z, r)
# derivative of memory equation: https://www.wolframalpha.com/input/?i=differentiate+%28+%28xr%29%5E2%2Fz%5E2+-+xr+%29%2F%28+x%5E2r+%2B+%28x%5E2r%29%5E2+%29++wrt+x
d1 = (-2 * r**2 * p**3 + 3*r*p**2 *z**2 + z**2)/(p**2 *z**2 *(r* p**2 + 1)**2)
d1
def derivative_eq(z,r,p):
    return (-2 * r**2 * p**3 + 3*r*p**2 *z**2 + z**2)/(p**2 *z**2 *(r* p**2 + 1)**2)

r = 1000000
z = 2.33
p = np.logspace(-8,-2)
res = derivative_eq(z,r,p)
plt.plot(p, res)
print(p[np.argmax(res)])
plt.xscale('log')
res
#plt.ylim(bottom=0, top = 100)
derivative_eq(z,r,1.39767608017285*10**(-6))<jupyter_output><empty_output><jupyter_text>SNR Derivation and misc<jupyter_code>eq = ((p*r)/(1+p*m+p**3*m*r))
eq

res = diff( eq , p)
res
s = Eq(0, (r-2*p**3*m*r**2)/( (1+p*m+p**3 *m*r)**2 ))
s
p_res = solve(s,p)[0]
p_res
solve(res,p=p_res)

m=100
r=1000000

2**(2/3)*(1/(m*r))**(1/3)/2
binom.ppf(0.0035, 1000, 0.5)
np.epp(-1)/(np.epp(-1)+np.epp(3))
27**(2/3)
m=np.linspace(100,1024, 100)
rs = 2**64#np.linspace(10000, 10000000000000, 100)
pvals = 1/(2*m*rs)**(1/3)
hamm_vals = space_frac_to_hamm_radius(n, pvals)
plt.plot(m, hamm_vals)
#plt.yscale('log')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24085613654836088, 'fraction_numerical': 0.11704145218098076, 'mean_word_length': 6.369261477045908, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '312476', 'license': 'MIT', 'n_tokens_mistral': 2093, 'n_tokens_neox': 1862, 'n_words': 291, 'path': 'Optimal_d.ipynb', 'repo_name': 'TrentBrick/attention-approximates-sdm', 'size': '3700'}