I am trying to calculate BIC in python. In python, there is no inbuilt library for computing BIC. I referenced the following link to compute variance and BIC further:- Using BIC to estimate the number of k in KMEANS The variance formula given in the highest voted answer is not working for my code design as centers is a two-dimensional array and in my case it's a single dimensional array. So I converted the variance formula given in the link to calculate but I learned it's not the correct way. Here is my python code:-
def compute_variance_grayscale(self, clusters, centroids, gImage, k):
# compute variance
variance = 0
for i in range(len(clusters)): #iterate over the available clusters
cluster = clusters[i]
sum = 0;
centroid = centroids[i]
for j in cluster: #for each data point in cluster j compute the sum
sum += (gImage.getImageValueAt(j[0], j[1]) - centroid) ** 2
variance += sum / (len(cluster) - k); #add the sum to the given variance
return variance
def compute_variance_rgb(self, clusters, centroids, cImage, k):
# compute variance
variance = 0
for i in range(len(clusters)):
cluster = clusters[i]
sum = 0;
centroid = centroids[i]
for j in cluster:
b,g,r = cImage.getImageValueAt(j[0], j[1])
b_e = (b - centroid[0]) ** 2
g_e = (g - centroid[1]) ** 2
r_e = (r - centroid[2]) ** 2
sum += (b_e + g_e + r_e)
variance += sum / (len(cluster) - k);
return variance
Can anyone please point to the right way to calculate variance? Also, it would be great if someone could explain to me the computation of BIC and use of const terms as mentioned in the highest voted answer in the above link:-
np.sum([n[i] * np.log(n[i]) -
n[i] * np.log(N) -
((n[i] * d) / 2) * np.log(2*np.pi*cl_var) -
((n[i] - 1) * d/ 2) for i in range(m)]) - const_term