Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7060667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:22:48+00:00 2026-05-28T04:22:48+00:00

I’m trying to implement an exponential moving average (EMA) on postgres, but as I

  • 0

I’m trying to implement an exponential moving average (EMA) on postgres, but as I check documentation and think about it the more I try the more confused I am.

The formula for EMA(x) is:

EMA(x1) = x1
EMA(xn) = α * xn + (1 - α) * EMA(xn-1)

It seems to be perfect for an aggregator, keeping the result of the last calculated element is exactly what has to be done here. However an aggregator produces one single result (as reduce, or fold) and here we need a list (a column) of results (as map). I have been checking how procedures and functions work, but AFAIK they produce one single output, not a column. I have seen plenty of procedures and functions, but I can’t really figure out how does this interact with relational algebra, especially when doing something like this, an EMA.

I did not have luck searching the Internets so far. But the definition for an EMA is quite simple, I hope it is possible to translate this definition into something that works in postgres and is simple and efficient, because moving to NoSQL is going to be excessive in my context.

Thank you.

PD: here you can see an example:
https://docs.google.com/spreadsheet/ccc?key=0AvfclSzBscS6dDJCNWlrT3NYdDJxbkh3cGJ2S2V0cVE

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:22:49+00:00Added an answer on May 28, 2026 at 4:22 am

    You can define your own aggregate function and then use it with a window specification to get the aggregate output at each stage rather than a single value.

    So an aggregate is a piece of state, and a transform function to modify that state for each row, and optionally a finalising function to convert the state to an output value. For a simple case like this, just a transform function should be sufficient.

    create function ema_func(numeric, numeric) returns numeric
      language plpgsql as $$
    declare
      alpha numeric := 0.5;
    begin
      -- uncomment the following line to see what the parameters mean
      -- raise info 'ema_func: % %', $1, $2;
      return case
                  when $1 is null then $2
                  else alpha * $2 + (1 - alpha) * $1
             end;
    end
    $$;
    create aggregate ema(basetype = numeric, sfunc = ema_func, stype = numeric);
    

    which gives me:

    steve@steve@[local] =# select x, ema(x, 0.1) over(w), ema(x, 0.2) over(w) from data window w as (order by n asc) limit 5;
         x     |      ema      |      ema      
    -----------+---------------+---------------
     44.988564 |     44.988564 |     44.988564
       39.5634 |    44.4460476 |    43.9035312
     38.605724 |   43.86201524 |   42.84396976
     38.209646 |  43.296778316 |  41.917105008
     44.541264 | 43.4212268844 | 42.4419368064
    

    These numbers seem to match up to the spreadsheet you added to the question.

    Also, you can define the function to pass alpha as a parameter from the statement:

    create or replace function ema_func(state numeric, inval numeric, alpha numeric)
      returns numeric
      language plpgsql as $$
    begin
      return case
             when state is null then inval
             else alpha * inval + (1-alpha) * state
             end;
    end
    $$;
    
    create aggregate ema(numeric, numeric) (sfunc = ema_func, stype = numeric);
    
    select x, ema(x, 0.5 /* alpha */) over (order by n asc) from data
    

    Also, this function is actually so simple that it doesn’t need to be in plpgsql at all, but can be just a sql function, although you can’t refer to parameters by name in one of those:

    create or replace function ema_func(state numeric, inval numeric, alpha numeric)
      returns numeric
      language sql as $$
    select case
           when $1 is null then $2
           else $3 * $2 + (1-$3) * $1
           end
    $$;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.