Skip to content

Instantly share code, notes, and snippets.

@ranbenbasat
Last active February 10, 2021 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranbenbasat/9959d1c70471fe870424eefbecd3e13c to your computer and use it in GitHub Desktop.
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
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