TradingView
RicardoSantos
1 de Abr de 2022 08:05

FunctionPolynomialFit 

U.S. Dollar Currency IndexTVC

Descrição

Library "FunctionPolynomialFit"
Performs Polynomial Regression fit to data.
In statistics, polynomial regression is a form of regression analysis in which
the relationship between the independent variable x and the dependent variable
y is modelled as an nth degree polynomial in x.
reference:
en.wikipedia.org/wiki/Polynomial_regression
bragitoff.com/2018/06/polynomial-fitting-c-program/

gauss_elimination(A, m, n) Perform Gauss-Elimination and returns the Upper triangular matrix and solution of equations.
  Parameters:
    A: float matrix, data samples.
    m: int, defval=na, number of rows.
    n: int, defval=na, number of columns.
  Returns: float array with coefficients.

polyfit(X, Y, degree) Fits a polynomial of a degree to (x, y) points.
  Parameters:
    X: float array, data sample x point.
    Y: float array, data sample y point.
    degree: int, defval=2, degree of the polynomial.
  Returns: float array with coefficients.
note:
p(x) = p[0] * x**deg + ... + p[deg]

interpolate(coeffs, x) interpolate the y position at the provided x.
  Parameters:
    coeffs: float array, coefficients of the polynomial.
    x: float, position x to estimate y.
  Returns: float.
Comentários
slowcoconut
Well done, love to see matrices being implemented by the Wizards... I learned a bit of computational linear algebra in the past so im familiar, but I didn't give it the time it deserved... fast forward to today and I'm finding it essential to building a great trading strategy...time to dig in and master it... apparently it has applications in ML/AI as well? If true, it's even more useful for sophisticated Strats...thanks to you and a few others I've some neat scripts to learn from... much appreciated Ricardo.
gorx1
That's cool ty, all good by me cuz I'm planning to use only quad reg, but I think 3+ orders don't work as intended
RicardoSantos
@gorx1, can you expand on that? it seems fine on my view but is totally possible that i misspelled a , or a . somewhere ;)
gorx1
@RicardoSantos, Yeah ofc, so I been testing it, and also comparing it with another implementation here on TV ("Moving Regression", called the same way). 1st and 2nd degrees coincide perfectly in both implementations and look reasonable for me. After 3+ degrees they don't coincide. Perhaps because of tick sizes? I been testing in on ES1!
RicardoSantos
@gorx1, i see what you mean, not sure of the cause right now so ill have to dig it up, thank you for the feedback :)
gorx1
@RicardoSantos, Yeah no problem, the script is great just as all your other work (ain't no flattery, facts 4 real)
RicardoSantos
@gorx1, so since both methods produced different result in the few online calculators i tried, im going to assume it has to do with the method used to solve the linear equation, im using gaussian elimination while tbiktag is using qr decomposition :), me, not being much of a mathematician going to drop it at that before i drop into the rabbit hole >.>
gorx1
@RicardoSantos, been working on the stuff, getting success with the "solve directly" method aka Normal Equation. When I started using "Length - index" instead of bar_index, suddenly things got better
EagleMoxy
oh wow!! love it
Mais