Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to solve a mathematical problem in two different ways and output is a curve in both the cases. I want to compare these output curves for similarity in python. Is there any function or framework which provides this functionality?


share|improve this question

closed as not a real question by MainMa, DKnight, David Thornley, Walter, Jonathan Khoo Aug 11 '11 at 23:26

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

You need to define what you mean by "similar" to get a meaningful answer. – quant_dev Aug 11 '11 at 11:40

The classic Pearson's correlation coefficient is perhaps the most popular measure of curve similarity. SciPy's pearsonr function gives you that.

Correlation coefficient measures shape similarity and is (somewhat, not completely) insensitive to bias and scaling.

Another way to measure similarity is to directly measure the average difference of the curves. You could use RMS difference. No libraries needed, simply something like this:

# rms difference of vectors a and b:
rmsdiff = 0
for (x, y) in zip(a, b):
    rmsdiff += (x - y) ** 2  # NOTE: overflow danger if the vectors are long!
rmsdiff = math.sqrt(rmsdiff / min(len(a), len(b))

RMS difference accentuates large deviations, even if they're local, and masks small deviations, even if they're global. For example, vectors

[1 2 3 4 5] and [2 1 4 3 6]

are more similar in RMS sense than

[1 2 3 4 5] and [1 2 3 4 2]

There's no one and only "right" measure of similarity. Different methods accentuate different (dis)similarities.

share|improve this answer
Correlation between two curves will be insensitive to shifts and scaling of both, so this may not be what the OP wants. – quant_dev Aug 11 '11 at 11:40
@quant_dev: True, it's a bit unclear what he wants. I'll add some methods. – Joonas Pulakka Aug 11 '11 at 11:43
Thanks guys. Mine is very simple application in 2D. Scales are same for both plots. So, i don't need to worry for scaling and shifts. Thanks Joonas for answering, it solves my problem. Thanks Quant_dev for making valid point. – user14299 Aug 11 '11 at 12:34
user14299: Functions y = x and functions y = 2x + 1 have a 100 % correlation, even though the curve is completely different. Just make sure you compare your functions in a meaningful way. – Joonas Pulakka Aug 11 '11 at 17:33

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