# How to measure how erratic is a function between a and b

I need to compare the outputs of some functions and to rate their "erratness".

Given a function, the less erratic function between a and b would be a straight line and the more erratic function would probably be a triangular or sine wave, the bigger its amplitude or its frequency the bigger their erratness. I'm not sure if it's clear what I mean.

I've thought that a way to calculate it could be to calculate the length of the line generated by the function between a and b. The smaller the length the less erratic will be the function.

The questions are:

1. Do you think this is a good way to achieve what I need?
2. How can the length of the output of a function be calculated?

-
You could look up the arclength integral... – J. M. Apr 14 '12 at 9:21
No idea on what are you talking about :) (I'm not a mathematic) but I will take a look at wikipedia. Thanks! – Ignacio Soler Garcia Apr 14 '12 at 9:29
Right, so type the words "arclength" and "integral" into your favorite search engine, and enjoy. Note that most such integrals do not have nice simple forms, and numerics would be needed. – J. M. Apr 14 '12 at 9:32
Do you mean that they cannot be calculated by an algorithm? I'm developing software. – Ignacio Soler Garcia Apr 14 '12 at 9:38
Stock Trading ... – Ignacio Soler Garcia Apr 14 '12 at 9:52

There is something called the total variation of a function. If the function is differentiable, it's computed as $\int_a^b|f'(x)|\,dx$. There are standard methods for computing the derivative and the integral numerically. If the function is not differentiable, there's a different definition, which should also be suitable for numerical computation. See the discussion at http://en.wikipedia.org/wiki/Total_variation.

-
You rock man. That's what I was looking for and the wiki explanation is great. Thanks again. – Ignacio Soler Garcia Apr 14 '12 at 20:25

If the end-use is an algorithm that must run quickly, your best bet is probably the sum squared difference (or SSD). If the line running between points $a$ and $b$ is: $$y = mx +n$$ Then you want to sum the squared difference between each point $(x_i,y_i)$ and the line: $$SSD\equiv\sum{(y_i - mx_i -n)^2}$$

The smaller the $SSD$, the less "erratic" your function. Also note that if you have many points and not many computer cycles available, you may want to choose a small random subset of the points instead of summing over all of them.

-
Looks as a good way. I specially like the quadratic feature to penalize more the more erratic points. Thanks, you rock too. – Ignacio Soler Garcia Apr 14 '12 at 20:27