Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWMA in PRQL with DuckDB #4

Open
eitsupi opened this issue Oct 13, 2023 · 1 comment
Open

EWMA in PRQL with DuckDB #4

eitsupi opened this issue Oct 13, 2023 · 1 comment

Comments

@eitsupi
Copy link
Owner

eitsupi commented Oct 13, 2023

Just a link to a book that compares this:
https://eitsupi.github.io/querying-with-prql/timeseries#sec-moving-ave

Very cool!

I also used an SMA like that in my presentations, as well as an EWMA which you can find in this Google Colab if you want to include that as well?
https://colab.research.google.com/drive/1asOWKjQbv6VWW9WjWR2Mer7PLmm0S0dT?usp=sharing

Originally posted by @snth in PRQL/prql#3618 (comment)

@eitsupi
Copy link
Owner Author

eitsupi commented Oct 13, 2023

@snth Thanks for sharing that! That looks awesome!

I don't see myself adding it anytime soon, but it would be great to have it shared.

let calc_ewma = alpha:null span:1 rel -> (
  from rel
  filter t==1
  select {t, x, ewma=x}
  loop (
    join rel (that.t==this.t+1)
    select {t, x, ewma = (alpha ?? 2/(span+1))*x + (1-(alpha ?? 2/(span+1)))*ewma}
  )
)

let data = (
  from tris_with_sma
  sort Date
  derive {t=row_number this, x=google_tri}
)

from data
#calc_ewma alpha:2/(10+1)
calc_ewma span:10
derive google_ewma10 = ewma
select {Date=t, google_tri=x, google_ewma10=ewma}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant