• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Easily Calculating Statistical Functions

    December 6, 2006 Hey, Ted

    The code associated with this article is available for download.

    Would you be interested in publishing a program that calculates statistical values, such as average, standard deviation, maximum value, and minimum value? I have such a program, which I use in to calculate average usage and standard deviation of the usage of the items we handle.

    –Victor Pisman

    Thanks to faithful reader Victor Pisman for an interesting program. When I think of calculating an average, I think in terms of accumulating an amount and counting before dividing. Victor took a different approach, and I like it.

    Victor’s RPG program, GETSTAT, has the following parameter list.

    Parameter

    Description

    Direction

    Amount

    The next
    item in the list of numbers

    In

    Count

    The number
    of items in the list of numbers

    In/Out

    Sum

    The sum of
    the items in the list of numbers

    In/Out

    Average

    The
    average of the items in the list of numbers

    In/Out

    Max

    The
    highest number in the list

    In/Out

    Min

    The lowest
    number in the list

    In/Out

    Deviation

    The
    standard deviation of the items in the list

    In/Out

    Reset

    Y=Reset
    the In/Out parameters to zero

    In

    Call this program for each number in the list. That is, if you need the average of seven numbers, call the program seven times. On the first call for the group, pass a value of Y through the RESET parameter in order to zero out all the aggregate values. On subsequent calls for the group, pass a RESET value of N.

     * Example caller of GETSTAT 
     
     * reset accumlators on first call for the group
    C            move      'Y'           @@reset
     *           
     * loop to process all the numbers in the group    
    C            dow                  
    *            
     * load the next item in the list into @@parm at this point
    C            z-add         @@parm         
     * call the calculation program.                           
    C            call      'GETSTAT'                    
    C            parm                    @@parm           11 2
    C            parm                    @@count           5 0 
    C            parm                    @@sum            11 2 
    C            parm                    @@average        11 2 
    C            parm                    @@max            11 2 
    C            parm                    @@min            11 2 
    C            parm                    @@deviation      11 2 
    C            parm                    @@reset           1   
     *                                                             
     * don't reset accumlators after first call for the group
    C            move      'N'           @@reset            
    C            enddo
    

    On each call, GETSTAT counts, accumulates, saves min and max, and recalculates average and standard deviation.

     ***** GETSTAT - calculate statistical values
    
    C     *entry plist                                         
    C            parm                    @@parm           11 2 
    C            parm                    @@count           5 0 
    C            parm                    @@sum            11 2 
    C            parm                    @@average        11 2 
    C            parm                    @@max            11 2 
    C            parm                    @@min            11 2 
    C            parm                    @@deviation      11 2 
    C            parm                    @@reset           1   
     *                                                                
     * Clear fields on first record of a group.
    C            if        @@reset='Y'                         
    C            z-add     *zero         @@count               
    C            z-add     *zero         @@sum                 
    C            z-add     *zero         @@average             
    C            z-add     *zero         @@max                 
    C            z-add     *zero         @@min                 
    C            z-add     *zero         @@deviation           
    C            endif                                         
     *                                           
     * Count and accumulate                     
    C            eval      @@count=@@count+1
    C            eval      @@sum=@@sum+@@parm
     *                     
     * Recalculate average and standard deviation
    C            eval(h)   @@average=(@@sum/@@count)
    *            
    C            eval(h)   @@deviation=((@@count-1)*               
    C                      @@deviation*@@deviation+                
    C                      (@@parm-@@average)*(@@parm-svaverage))/ 
    C                      @@count                                 
    C            sqrt(h)   @@deviation   @@deviation               
     *                                           
     * Track lowest and highest numbers of the group
    C            if        @@parm>@@max or @@count=1               
    C            eval      @@max=@@parm                            
    C            endif                                             
     *           
    C            if        @@parm
    
                              

    After the call for the last item in the list, the In/Out parameters have the values of the group functions.

    I like Victor's program. It's simple, easy to understand, and easy to use. I would think this program would work especially well in an interactive environment, where small lists of numbers are being manipulated.

    I should also mention that SQL has these statistical functions. Here's an example that returns the same values that Victor's program calculates.

    select item, sum(SomeNumber), count(*),               
           dec(avg(SomeNumber),11,2) as avg,
           max(SomeNumber), min(SomeNumber),              
           dec(stddev(SomeNumber),11,2) as stddev
      from somefile group by item
    

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    ARCAD Software

    [Webinar Series] Demystifying DevOps on the IBM i

    Join us for this 3-Part Roundtable Webinar Series, where ARCAD experts will demystify the move to Git and an automated process with options that work for everyone!

    This Series will be an engaging discussion on key DevOps topics as Git, Builds, Automation and much more.

    Part 1: IBM i & Git – Developer Tools (Thursday, January 26th, at 12:00PM ET / 9:00AM PT)

    During this 1st session, we will discuss Developer Tools and all the ways to use Git including iProject, Merlin, VS Code, ARCAD’s Centralized option and more.

    Part 2: Branching & Building (Thursday, February 9th, at 12:00PM ET / 9:00AM PT)

    During this 2nd session, we will discuss Feature/Release and Branch Management and building the branches with tools like Bob and ARCAD.

    Part 3: DevOps – Automated Workflow (Thursday, February 23rd, at 12:00PM ET / 9:00AM PT)

    During this 3rd session, we will discuss what can be automated in the IBM i DevOps process starting from the build to other steps in your workflow.  We’ll discuss pipeline tools like Jenkins and the new automation features of Git packages.

    Register NOW

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    COMMON:  Join us at the Annual 2007 Conference & Expo, April 29 - May 3, in Anaheim, California
    Bug Busters Software Engineering:  Quality software solutions for the iSeries since 1988
    Profound Logic Software:  Experience RPGsp - the #1 iSeries Web development tool

    BOS Reduces Its Losses in the Third Quarter, Affirms 2006 Guidance The Business Case for the System iWant

    Leave a Reply Cancel reply

Volume 6, Number 43 -- December 6, 2006
THIS ISSUE SPONSORED BY:

IBS
WorksRight Software
Guild Companies

Table of Contents

  • Easily Calculating Statistical Functions
  • More About Commands and Variable-Length Parameters
  • Admin Alert: Quick and Dirty Ways to Find Job Gone Wild

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • N2i Gains Traction Among IBM i Newbies
  • Realizing The Promise Of Cross Platform Development With VS Code
  • 2023 IBM i Predictions, Part 3
  • Four Hundred Monitor, January 25
  • Join The 2023 IBM i Marketplace Survey Webinar Tomorrow
  • It Is Time To Have A Group Chat About AI
  • 2023 IBM i Predictions, Part 2
  • Multiple Vulnerabilities Pop Up In Navigator For i
  • Participate In The 2023 IBM i Marketplace Survey Discussion
  • IBM i PTF Guide, Volume 25, Number 4

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.