pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
computing z-scores
5+ messages / 3 participants
[nested] [flat]

* computing z-scores
@ 2018-05-24 15:15 Martin Mueller <martinmueller@northwestern.edu>
  2018-05-24 15:22 ` Re: computing z-scores David G. Johnston <david.g.johnston@gmail.com>
  2018-05-24 15:26 ` Re: computing z-scores Ron <ronljohnsonjr@gmail.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Martin Mueller @ 2018-05-24 15:15 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>; +Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>

You construct a z-score for a set of values by subtracting the average from the value and dividing the result by the standard deviation. I know how to do this in a two-step procedure. First, I compute the average and standard deviation. In a second run I use the formula and apply it to each value. 

Is there a way of doing this in a single-step procedure or can you chain the two parts together in one query?  This goes beyond my SQL competence. 

Martin Mueller    



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: computing z-scores
  2018-05-24 15:15 computing z-scores Martin Mueller <martinmueller@northwestern.edu>
@ 2018-05-24 15:22 ` David G. Johnston <david.g.johnston@gmail.com>
  2018-05-24 15:32   ` Re: computing z-scores David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2018-05-24 15:22 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>

On Thu, May 24, 2018 at 8:15 AM, Martin Mueller <
martinmueller@northwestern.edu> wrote:

> You construct a z-score for a set of values by subtracting the average
> from the value and dividing the result by the standard deviation. I know
> how to do this in a two-step procedure. First, I compute the average and
> standard deviation. In a second run I use the formula and apply it to each
> value.
>
> Is there a way of doing this in a single-step procedure or can you chain
> the two parts together in one query?  This goes beyond my SQL competence.
>

Window functions provide the easiest means to apply aggregated values to
individual rows.

SELECT v, (v - (AVG(v) OVER ()) / (stddev(v) OVER ())) AS z_v
FROM  (
VALUES (1),(2),(3)
) vals (v);

//-1, 0, 1

​https://www.postgresql.org/docs/10/static/tutorial-window.html

David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: computing z-scores
  2018-05-24 15:15 computing z-scores Martin Mueller <martinmueller@northwestern.edu>
  2018-05-24 15:22 ` Re: computing z-scores David G. Johnston <david.g.johnston@gmail.com>
@ 2018-05-24 15:32   ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2018-05-24 15:32 UTC (permalink / raw)
  To: Martin Mueller <martinmueller@northwestern.edu>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>; pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>

On Thursday, May 24, 2018, David G. Johnston <david.g.johnston@gmail.com>
wrote:

> On Thu, May 24, 2018 at 8:15 AM, Martin Mueller <
> martinmueller@northwestern.edu> wrote:
>
>> You construct a z-score for a set of values by subtracting the average
>> from the value and dividing the result by the standard deviation. I know
>> how to do this in a two-step procedure. First, I compute the average and
>> standard deviation. In a second run I use the formula and apply it to each
>> value.
>>
>> Is there a way of doing this in a single-step procedure or can you chain
>> the two parts together in one query?  This goes beyond my SQL competence.
>>
>
> Window functions provide the easiest means to apply aggregated values to
> individual rows.
>
> SELECT v, (v - (AVG(v) OVER ()) / (stddev(v) OVER ())) AS z_v
> FROM  (
> VALUES (1),(2),(3)
> ) vals (v);
>
> //-1, 0, 1
>
> ​https://www.postgresql.org/docs/10/static/tutorial-window.html
>
> David J.
>

I think I have misplaced a parenthesis though...order of operations needs
one added around the subtraction.

Note, this is not the correct list for questions like this.  The -general
list is the one you want to be using.

David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: computing z-scores
  2018-05-24 15:15 computing z-scores Martin Mueller <martinmueller@northwestern.edu>
@ 2018-05-24 15:26 ` Ron <ronljohnsonjr@gmail.com>
  2018-05-24 15:30   ` Re: computing z-scores David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Ron @ 2018-05-24 15:26 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 05/24/2018 10:15 AM, Martin Mueller wrote:
> You construct a z-score for a set of values by subtracting the average from the value and dividing the result by the standard deviation. I know how to do this in a two-step procedure. First, I compute the average and standard deviation. In a second run I use the formula and apply it to each value.
>
> Is there a way of doing this in a single-step procedure or can you chain the two parts together in one query?  This goes beyond my SQL competence.

What about this?

SELECT value, (value - AVG(value))/stddev(value) as zvalue
FROM sometable
WHERE some conditions
GROUP by value


-- 
Angular momentum makes the world go 'round.




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: computing z-scores
  2018-05-24 15:15 computing z-scores Martin Mueller <martinmueller@northwestern.edu>
  2018-05-24 15:26 ` Re: computing z-scores Ron <ronljohnsonjr@gmail.com>
@ 2018-05-24 15:30   ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2018-05-24 15:30 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thursday, May 24, 2018, Ron <ronljohnsonjr@gmail.com> wrote:

> On 05/24/2018 10:15 AM, Martin Mueller wrote:
>
>> You construct a z-score for a set of values by subtracting the average
>> from the value and dividing the result by the standard deviation. I know
>> how to do this in a two-step procedure. First, I compute the average and
>> standard deviation. In a second run I use the formula and apply it to each
>> value.
>>
>> Is there a way of doing this in a single-step procedure or can you chain
>> the two parts together in one query?  This goes beyond my SQL competence.
>>
>
> What about this?
>
> SELECT value, (value - AVG(value))/stddev(value) as zvalue
> FROM sometable
> WHERE some conditions
> GROUP by value
>

That is syntactically correct but doesn't provide the correct answer.

David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-05-24 15:32 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-24 15:15 computing z-scores Martin Mueller <martinmueller@northwestern.edu>
2018-05-24 15:22 ` David G. Johnston <david.g.johnston@gmail.com>
2018-05-24 15:32   ` David G. Johnston <david.g.johnston@gmail.com>
2018-05-24 15:26 ` Ron <ronljohnsonjr@gmail.com>
2018-05-24 15:30   ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox