Bob-ob
Bob-ob

Reputation: 1618

Object/Array comparison algorithms to determine commonality/similarity

I am trying to figure out the best approach to determining commonality or similarity between various objects or arrays and would be interested in getting the community's input. I am currently building an early research prototype in javascript and I need to adopt a smart way of comparing objects in order to identify emerging patterns or trends. By identifying patterns the application I am working on would be capable of making more informed decisions.

So for example given 6 simplified objects:

A_obj = {w: 0.66, x: 0.36, y: 0.88, z: 0.34},
B_obj = {w: 0.46, x: 0.29, y: 0.91, z: 0.37},
C_obj = {w: 0.69, x: 0.40, y: 0.95, z: 0.38},
D_obj = {w: 0.78, x: 0.37, y: 0.84, z: 0.43},
E_obj = {w: 0.14, x: 0.41, y: 0.85, z: 0.53},
F_obj = {w: 0.85, x: 0.33, y: 0.96, z: 0.22};

By looking at the above, it is clear that there is a greater degree of commonality in the x and y traits, while there is greater variance in w and z traits.

I'm hoping to find a relatively lightweight solution that is easy to replicate in other languages also. All thoughts and comments are welcome.

Upvotes: 0

Views: 543

Answers (1)

RedGreenCode
RedGreenCode

Reputation: 2299

If the traits are independent, you could just calculate the variance of each trait separately. Since you want to be language independent, here is some pseudocode:

for each trait
    for each object
        add the current trait value to a variable to get a cumulative total
    divide the total by the number of objects to get the mean
    for each object
        subtract the trait value from the mean, and square the result
        add the result to a variable to get a cumulative total
    divide by the number of objects to get the mean of the squared differences

That will give you the variance for each trait. Variance is an average of positive values, so it's always positive. Lower values mean less variance, and higher values mean more variance.

Upvotes: 1

Related Questions