× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



It has been pointed out to me (off-line) that there are other forms of
standard deviations. The calculation I provided is for a population
standard deviation. If you're after a sample standard deviation then a
minor change is needed (and can be found on the net).

On Sun, Mar 25, 2012 at 5:32 PM, Bruce Vining <bvining@xxxxxxxxxxxxxxx>wrote:

%elem returns the number of defined elements (10 in your case). If you
only want to use the *first* N array elements then you need the following
changes:

dData s 5p 3 dim(10)
dMean s 5p 3
dStdDev s 8f
dN s 10u 0 inz(4)

/free
Mean = %xfoot(%subarr(Data :1 :N)) / N;
StdDev = %sqrt(%xfoot(((%subarr(Data :1 :N) - Mean) ** 2)) / N);

/end-free

With this change only elements 1 through N (4 as N is currently
initialized above) will be used in the calculation.

Bruce Vining

On Sun, Mar 25, 2012 at 2:34 PM, James Newman, CDP <newmanas400@xxxxxxxxx>wrote:

Bruce,

Looks to me like that would work. I have to admit I'm not very familiar
with the "free" functions and have never used %elem before. If you use it
on a 10 element array and only have data in 4, what number does it return?

Thanks so much for the help...now just have to wrap the rest of the code
around it.


Jim

On Sun, Mar 25, 2012 at 8:10 AM, Bruce Vining <bvining@xxxxxxxxxxxxxxx
wrote:

I can't say that I've done it prior to this morning, but the following
appears to work in a quick and very simple (read "disclaimer inserted
here"
as my last standard deviation was in college econ stats -- quite a few
years ago) test case. I would recommend additional testing, obviously
changing to the precision you really have/need, etc...):

dData s 5p 3 dim(10)
dMean s 5p 3
dStdDev s 8f

/free

Mean = %xfoot(Data) / %elem(Data);
StdDev = %sqrt(%xfoot(((Data - Mean) ** 2)) / %elem(Data));

*inlr = *on;
return;

/end-free
As an aside, I would also recommend loading the data array strictly
base on
a file containing "days off" where days off includes weekends. Based on
past experience an implementatin loading values based on (not Saturday,
not
Sunday, and not a Holiday) is (typically the day following the code
install) followed by a company decision to start working on the last
Saturday of each month due to increased demand...

Bruce Vining

On Sat, Mar 24, 2012 at 6:16 PM, James Newman, CDP <
newmanas400@xxxxxxxxx
wrote:

Chuck,

Thanks for the expertise and advice. I left out some info for the
sake
of
brevity...sorry.

I'm at v5r2...Al Barsa used to howl each time I'd tell him that.

The data for the array comes from a table. After performing the
required
functions, I throw out the oldest record, add a new one, and start the
process all over again - a "moving average" for example. I could
change
the beginning and ending dates and use SQL to perform the functions.
I
wonder about the efficiency of that versus reading one record at a
time,
updating the array, and performing the calculations on the array.
Also,
giving SQL a beginning and ending date is problematic, as I don't
consider
weekends or holidays and would have to account for that somehow.

For this specific application, the number of days considered would be
10.

TIA.


James R. Newman, CDP



On Sat, Mar 24, 2012 at 5:40 PM, CRPence <CRPbottle@xxxxxxxxx> wrote:

On 24-Mar-2012 11:27 , James Newman, CDP wrote:
So I have 10 items in an array and I'd like to figure out a mean
(average) and standard deviation. For the mean, I can XFOOT, then
divide by the number of elements in the array. I've only used SQL
with tables - can I use SQL against an array?


The SQL may not be used as easily as one might desire with
arrays,
for lack of both an ARRAY data type or TABLE(:array_name)
capability,
but there are ways. Easier with IBM i 7.1 SQL I presume, with an
ARRAY
data type and associated functions [using a User Defined Type (UDT)
however], but I have neither experience with the support nor read
much
about the v7r1 array type support; perhaps moot, as the OS release
was
left undisclosed, for what might be available to the OP.

Per "10 items", does that imply there will always be a specific
and\or limited number of items below a specific count? What is the
origin and storage location for the data values which eventually
become
elements of an array declared in the program? Irrespective of the
answers to those questions, whatever loads the array could instead
provide the data as row data via an external user defined TABLE
function.

A limited number of items could be elements of a values-clause [or
similarly selected as UNION set of data], a derived result TABLE,
against which the STDDEV, AVG, and COUNT aggregate functions could
operate.

An example using dynamic SQL that works at v5r3 for four values;
though a values row expression, since v5r4, is much prettier than
the
UNION ALL:

with SD (d8) as
( select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
)
select int(avg(d8))
, dec(stddev(d8), 10, 2)
, int(count(*))
from SD
;
open abovePreparedStmt using :e1, :e2, :e3, :e4
; -- :HV e1 to e4 are array[1] to array[4]

Regards, Chuck
--
This is the RPG programming on the IBM i / System i (RPG400-L)
mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.


--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.




--
Regards,
Bruce
www.brucevining.com
www.powercl.com
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.


--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.




--
Regards,
Bruce
www.brucevining.com
www.powercl.com





As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.