-
-
Save ranbenbasat/9959d1c70471fe870424eefbecd3e13c to your computer and use it in GitHub Desktop.
Lower bound for the expected squared error when sending a real [0,1] number using a single bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kmeans1d | |
import random | |
from math import sqrt | |
pts = 1000000 | |
w = 0.270279 #this is an approximation, the real formula is implicit | |
a = (-2*w**2+w-2*sqrt(-(w-1)*w)+1)/(4*w**2-6*w+2) | |
nrZeros = int(pts*w) | |
nrMid = pts-2*nrZeros | |
inc = (1-2*a)/nrMid | |
start = a + inc/2 | |
x = [0] * nrZeros + [1] * nrZeros + [start+i*inc for i in range(nrMid)] | |
k = 2 | |
clusters, centroids = kmeans1d.cluster(x, k) | |
err = 0 | |
for i,xi in enumerate(x): | |
err += (xi-centroids[clusters[i]])**2 | |
print('Err:', err/len(x)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment