0

Disclaimer: I'm very outsider in Maths.. so maybe what I'm asking is very basic. Or maybe I need help to reformulate the question.

I would like to measure numerically the consistency of several sequences so I can compare and order them base on this consistency number.

These are some possible examples os sequences sorted (based on my opinion) from less consistent to more consistent, the examples are using a binary values, but it also can be any number of options:

  • 10101
  • 1010
  • 101
  • 10
  • 0 (*)
  • 1 (*)
  • 110
  • 11
  • 111
  • 1111

(*) equal consistency

There are other scenarios I don't really know how to sort them, check the following pairs, I don't really know which would be defined as more consistent:

Pair 1:

  • 111111111111101
  • 11

Pair 2:

  • 110
  • 1100

Pair 3:

  • 1010101010
  • 1111100000

I'm asking for any kind of insight about how I should proceed to calculate this measurement: formules, links to docs, suggestions, anything is welcome.

1

2 Answers 2

1

I could count the number of "changes" (from 0 to 1 or viceversa) and divide it by the total number of elements in the sequence.

If the sequences can have things different from 1s and 0s, I would make the "distance" between each element count. So a change from 0 and 1 "costs" 1, but a change from 0 to 2 "costs" 2, etc.

def get_consistency(sequence)
  change = 0
  count = 0
  previous = nil
  sequence.each do |element|
    if previous then
      # define distance as whatever you want. For numbers, its abs(element-previous)
      change += distance(element, previous)
      count += 1
    end
    previous = element
  end
  count == 0 ? 0 : change / count
end
7
  • Looks nice.. I kind of like the version 3 of this experiment: labs.codecademy.com/C6Rh/1#:workspace what do you think?
    – fguillen
    Oct 16, 2014 at 15:49
  • I see that you didn't include my example for the distance, using the abs of the difference. I've included it here (step 4): labs.codecademy.com/C6Rh/2#:workspace . Also, I realized that "count" was "counting one more element that in should" (it should count the transitions, not the elements). I've fixed that in codeacadamy and my answer
    – kikito
    Oct 16, 2014 at 16:07
  • I saw your example of the distance, but I wanted to play with categorical variables this is because they are equal or they are not, doesn't matter the real value. I like the previous version of the count because if not the sequences 1,2 and 1,2,3,4 have the same value.. same for the sequences 1,1 and 1,1,1,1.. and (IMHO) they shouldn't. The VERSION 4 of your example is not confusing for me because the most consistent versions have less value and there is not difference between 1 and 1,1,1.
    – fguillen
    Oct 16, 2014 at 16:22
  • Check the VERSION 5 labs.codecademy.com/C6Rh/3#:workspace If you allow me I would like to modify your code with that version.
    – fguillen
    Oct 16, 2014 at 16:23
  • I think I prefer it when consistency is 0 for "perfectly constant" sequences and then it goes up as change happens. I don't like having negative values, there; they will make sorting more difficult. Can't you use two numbers to sort? Sort first by consistency, and then sort sequences with same consistency by length.
    – kikito
    Oct 16, 2014 at 16:36
1

Based on the @kikito implementation but with another flavor:

def get_consistency(sequence)
  proximity =
    sequence.each_with_index.map do |element, index|
      index > 0 ? distance(element, sequence[index-1]) : 0
    end.reduce(:-)

  proximity / sequence.length.to_f
end

def distance(value1, value2)
  value1 == value2 ? -1 : 1 # Categorical variable
end

Results:

0.7500 <- 1,1,1,1
0.4000 <- 1,1,1,1,2
0.5714 <- 1,1,1,1,2,2,2
0.2857 <- 1,1,1,1,2,2,3
0.0000 <- 1
0.0000 <- 2
-0.5000 <- 1,2
-0.7500 <- 1,2,3,4
-0.2500 <- 1,2,2,3
0.9000 <- 1,1,1,1,1,1,1,1,1,1
0.9900 <- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
0.9703 <- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
-0.9000 <- 1,2,3,4,5,6,7,8,9,10
-0.9000 <- 1,0,1,0,1,0,1,0,1,0
0.7000 <- 1,1,1,1,1,0,0,0,0,0
0.2500 <- 1,1,0,0
1
  • I'm using my answer but the original credits of my algorithm are for the @kikito's answer.
    – fguillen
    Oct 20, 2014 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.