views:

461

answers:

3

Imagina a sine wave oscillating about a zero line. My task is to calculate the slope at several random points along the wave using a fairly coarse x axis scale. (yes this has a real application)

When the wave is in +ve terrirtory (above the zero line) slope can be calculated from:

Slope = (y(n) / y(n-1)) - 1

This yeilds +ve slope vlaues heading up and -ve heading down.

The problem is that this must be switched when we are in -ve territory and then two more expressions are required when one of the vlaues is zero for a total of four expressions that must be chosen programatically with a conditional statements.

I would like to find ONE expression that covers all four condtions as this is at the center of a heavily travelled algorithm and clks count!

I am sure this would be a trivial solution for a math genius, but to these tired eyes, it eludes me...

Added:

The "sine wave" is actually an MACD indicator that is derived from (random) price action of financial markets. an example would be here:

http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_average_conve

The slope (of the thick black line in the lower graph for example) is what I need to calculate here defined simply as up or down (where heading up is +ve)

The problem is that both +ve and -ve slope can occur above and below zero. A slope calcualtion may also occur using increments that cross the zero line and at the zero line.

It would be nice to find a solution that not involve a ton of IF statements... like for example, shifing all the y values by a fixed amount so they become +ve and then calculating slope in the +ve region. I would need to pick a number that historically, y has never been below, like a couple of orders of magnitude for example (99) and then I could perform the offest and one slope calculation?

A: 

If you're looking for the slope of the sine wave, it's done via standard calculus functions.

The slope of the line f(x)=sin(x) is f'(x)=cos(x).

However, based on your formula, I'm not entirely sure that's what you're after. Maybe you need to clarify it.

But if you want the approach as shown in your question, I think your concern about performance is misplaced here. In order to do this calculation as a single arithmetic expression, you're going to have to multiply by -1 depending on whether you're above or below the x-axis (and whether you're transitioning across that axis from f(n-1) to f(n)).

That's likely to be slower (or at least no faster) than a simple sequence of if-statements once it's been compiled down to machine language.

As with all optimizations, you should choose the easiest-to-develop approach first then see if it has performance problems. Otherwise you may be wasting a lot of effort up front for an unneeded boost.

paxdiablo
Very good advice. I was hoping there was an elegant solution like shifing all the values by a fixed offset so they become +v and then calculating slope
Mike Trader
+3  A: 

what do you mean by 'sine wave'? do you mean a mathematically generated plot of y = a sin(bx) or a smooth curve fitted to some experimental points via splines that just happens to resemble a sine wave in that it oscillates around the x axis? if the former, you can differentiate it mathematically and get the exact slope at any point. if the latter, the formula you're looking for is

slope(x) = (y(x-delta) - y(x+delta))/(2 * delta)

experiment with different values of delta. there're no conditionals involved; the signs of the numerator and denominator will automatically ensure that you get the right sign for the slope.

Martin DeMello
Shouldn't you be dividing by 2 delta?
John Pirie
Thank you for a very informative answer. I should have explained the underlying data series is essentially random (see added above)What would the differential look like as a function?
Mike Trader
thanks john, fixed that
Martin DeMello
mike: the concept of a slope at a point requires that your curve is continuous. if your plot is a series of discrete, random points you need to first fit a smooth curve through it (see http://en.wikipedia.org/wiki/Curve_fitting for some ways to do this) and then calculate the slope of the curve based on the equation you used to generate it. if you are more interested in the trend of the curve, you can use the formula i supplied above. what it does is take a window (delta) around the point you want, draw two vertical lines, see where they cross the curve and join those points with a line.
Martin DeMello
Hi Matin, Thankyou for the reply. Yes the curve must be smooth. This is a function of the timeframe and the lookbackk used for the indicator. It is essentially smoothed as it is based on a moving average of longer period. Th vertical lines are in fact the increment of the timeframe (y-axis)
Mike Trader
mike: one thing i think you need to be careful about is to calculate the slope at a point x by taking a window *around* x, rather than a window whose right hand side is x. also, if your curve is plotted via discrete ticks, even a moving average isn't really "smooth" in the mathematical sense, but that doesn't really matter for your purposes.
Martin DeMello
+2  A: 

The slope is just

slope = y(n)-y(n-1)

Or if you have units on the x-axis, divide this by one x-axis step (i.e. x(n)-x(n-1)).

This formula is sort-of the definition, and it doesn't matter which side of the x-axis your are on. (I say "sort-of" here, since the slope is a limit of this equation as you sample bins get very close together, so this equation is an approximation, and other approximations can be used.)

Keep in mind that the slope of noisy data will look even more noisy.

I suspect the first few paragraphs of the slope page on wikipedia will help you out with this.

tom10
yes, but when the values are -ve the slope becomes y(n-1) - y(n). When one value is +ve or zero it is different again...
Mike Trader
No, the formula really doesn't change. For example, shift the curve up or down, positive or negative doesn't change the slope, in reality, or in this formula. (Here's a little proof: To shift up or down, add a constant, say C, to every point, so Y(n) = y(n) + C. Then Y(n)-Y(n-1) = (y(n)+C) - (y(n-1)+C) = y(n)-y(n-1) + C - C = y(n) - y(n-1). That is the **same** slope.) Maybe try putting the real data aside, and try this type of thing with a real sine curve, for example.
tom10
Maybe an example would clarify? Say y(n)=30 and y(n-1)=10; then the slope is 30-10=20. Shift down by 20, y(n)=10, and y(n-1)=-10; then slope = 10-(-10) = 10+10 = 20. Shift the original down by 40, y(n)=-10 and y(n-1)=-30; then slope=-10-(-30)=-10+30=20. The same every time with exactly the same formula.
tom10
Tom, you are not understanding. When the the curve is in the -ve domain a rising slope has lesser negative y values. In the +ve domain it has increasing values of the y-axis. Meaning, going up in the negative domain means decreasing -ve values whereas goin up in the posative domain means INCREASING +ve values. Then if you add in the cases where one value is the opposite sign or zero, you have 6 or so cases to define. Shifting the values into the +ve doamin solves this issue.
Mike Trader
Mike, I might be misunderstanding. Can you give me, or post in your question statement, an example using actual numbers, sort of like I did in the comment above, where the direct subtraction in the above equation doesn't give the correct slope.
tom10