Serving the Quantitative Finance Community

 
User avatar
skullx
Topic Author
Posts: 0
Joined: January 27th, 2012, 5:43 pm

real-time curve fitting

November 13th, 2013, 6:06 am

Hello, I'm quite new in programming and also my maths are quite rusty. Could you please advise me on how can I fit a curve of type (a - 2*b*const1 + c * const2) on the fly using C++. I need it to refit on every market data change.I think least squares are not able to do this? Any advice on model & code are appreciated!
Last edited by skullx on November 12th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
skullx
Topic Author
Posts: 0
Joined: January 27th, 2012, 5:43 pm

real-time curve fitting

November 13th, 2013, 11:12 am

Thank you, but it seems a little bit hard for me in terms of math. Maybe there are some tutorials? I just need an optimisation of 3 parameters over 8-12 data points, which is more than enough I think.
 
User avatar
farmer
Posts: 63
Joined: December 16th, 2002, 7:09 am

real-time curve fitting

November 13th, 2013, 1:02 pm

1) compute on eleven most recent data points, plus a hypothetical data point equal to the most recent one, using 5 possible values of each parameter, or 125 tests, save this best set2) do the same test with the hypothetical data point slightly above or below, save these two additional best sets3) if next data point arrives at this point, select one of the three saved sets as your current parameter set, return to 1 4) find two more best parameter sets using two further-out possibilities for next data point5) if next data point arrives, select from five saved sets based on which one used hypothetical value closest to actual6) compute on 11+1 data points using 5 possible values of each parameter as smaller variations on the saved setsThis way, you attempt to do most of your calculations before the most recent data point is known, and then just look up your fitted parameter set from a saved table.
Antonin Scalia Library http://antoninscalia.com
 
User avatar
skullx
Topic Author
Posts: 0
Joined: January 27th, 2012, 5:43 pm

real-time curve fitting

November 13th, 2013, 2:16 pm

QuoteOriginally posted by: outrunQuoteOriginally posted by: skullxThank you, but it seems a little bit hard for me in terms of math. Maybe there are some tutorials? I just need an optimisation of 3 parameters over 8-12 data points, which is more than enough I think.There is indeed no point in finding an optimized algorithm, just recompute an ordinary least squares fit on the 12 data points every tick? How about you first try to write (or find) a simple C++ routine that does y=ax+b, and then go from there.y = ax + b is done, just don't know how to numerically step to three parameters calculation.
Last edited by skullx on November 12th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

real-time curve fitting

November 13th, 2013, 3:24 pm

Least squares can handle multiple parameters. You just need to turn the crank. Minimizing the sum of the residual errors (∑(eps_i)^2) means finding where the partial derivative of the sum of the errors with respect to the unknown parameters crosses zero. 1. start with your equation: y_i = a - 2*b*x1_i + c * x2_i - eps_i2. rewrite it as eps_i = something3. square both sides to get (eps_i)^2 = something messy but really not that hard4. form the sum across i: ∑(eps_i)^2 = messy equation #3 with a ∑ on each term5. factor the parameters out of each sum (e.g., rewrite ∑(c * x2_i) as c*∑(x2_i) )6. take the partial derivative of equation #5 with respect to a, b, and c to form 3 equations in the 3 unknowns7. set each equation from #6 to zero and solve for a, b, and c in terms of all those messy summations.It may seem daunting, but it's all pretty mechanical. You just need to work through all the fiddly terms carefully. At the end, you can feed some simulated data that has known a, b, & c to confirm there's not a mistake anywhere.
 
User avatar
skullx
Topic Author
Posts: 0
Joined: January 27th, 2012, 5:43 pm

real-time curve fitting

November 14th, 2013, 5:32 am

Awwwwww...That's why I love this community - senior guys are always open-minded and ready to help noobies!Thank you very much, all three answers are very clear and useful. Went coding
 
User avatar
skullx
Topic Author
Posts: 0
Joined: January 27th, 2012, 5:43 pm

real-time curve fitting

November 18th, 2013, 10:23 am

Maybe I do something wrong, but when I differentiate by a,b,c I return to the initial equation in all three cases
 
User avatar
daveangel
Posts: 5
Joined: October 20th, 2003, 4:05 pm

real-time curve fitting

November 19th, 2013, 8:01 am

check out Alglib .. they do OLS and its very fast. I am not connected with Alglib
knowledge comes, wisdom lingers