This post was updated on .
How do you compute a faculty in SPSS ?
3! = 3*2*1. Couldn't find a function yet. (we found a solution: http://www-01.ibm.com/support/docview.wss?uid=swg21479801) COMPUTE nfact = EXP(LNGAMMA(n+1)) . EXECUTE . (but isn't there a more simple way to do that...)
Dr. Frank Gaeth
|
Administrator
|
You mean Factorial!
COMPUTE F=1. LOOP #=2 TO n. COMPUTE F=F*#. END LOOP. -- OTOH: In stats these things are usually part of something else and it is better to apply cancellations of some sort along the way! For example in evaluating binomial coefficients C(N,k) you would CERTAINLY NOT want to do something stupid and directly evaluate N!/(N-k)! k! from the 3 separate factorials. Enough rope Frank? --
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by drfg2008
>
> How do you compute a faculty in SPSS ? > > 3! = 3*2*1. > > Couldn't find a function yet. > define !factorial (!positional !tokens(1)), compute factorial = exp(lngamma(!1+1)). !enddefine. !factorial 3. (as found somewhere in the vaults of the IBM website) ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
The syntax below seems to work but I'm kinda surprised it doesn't raise an error for the first case. It requests a loop from 1 to 0... which I thought would be not valid...
data list free/id. begin data 0 1 2 3 4 5 6 7 8 9 10 end data. comp fac=id. loop #=1 to id. if # > 1 fac=fac*(id-#+1). end loop. exe. > Date: Wed, 21 Nov 2012 02:14:27 -0800 > From: [hidden email] > Subject: Re: Faculty > To: [hidden email] > > > > > How do you compute a faculty in SPSS ? > > > > 3! = 3*2*1. > > > > Couldn't find a function yet. > > > > define !factorial (!positional !tokens(1)), > compute factorial = exp(lngamma(!1+1)). > !enddefine. > > !factorial 3. > > (as found somewhere in the vaults of the IBM website) > > ===================== > To manage your subscription to SPSSX-L, send a message to > [hidden email] (not to SPSSX-L), with no body text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the command > INFO REFCARD |
From the CSR,
�� If the expression for the initial value is greater than the terminal value, the loop is not executed. For example, #J=X TO Y is a zero-trip loop if X is 0 and Y is –1. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Ruben van den Berg <[hidden email]> To: [hidden email], Date: 11/21/2012 03:34 AM Subject: Re: [SPSSX-L] Faculty Sent by: "SPSSX(r) Discussion" <[hidden email]> The syntax below seems to work but I'm kinda surprised it doesn't raise an error for the first case. It requests a loop from 1 to 0... which I thought would be not valid... data list free/id. begin data 0 1 2 3 4 5 6 7 8 9 10 end data. comp fac=id. loop #=1 to id. if # > 1 fac=fac*(id-#+1). end loop. exe. > Date: Wed, 21 Nov 2012 02:14:27 -0800 > From: [hidden email] > Subject: Re: Faculty > To: [hidden email] > > > > > How do you compute a faculty in SPSS ? > > > > 3! = 3*2*1. > > > > Couldn't find a function yet. > > > > define !factorial (!positional !tokens(1)), > compute factorial = exp(lngamma(!1+1)). > !enddefine. > > !factorial 3. > > (as found somewhere in the vaults of the IBM website) > > ===================== > To manage your subscription to SPSSX-L, send a message to > [hidden email] (not to SPSSX-L), with no body text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the command > INFO REFCARD |
Administrator
|
In reply to this post by Ruben Geert van den Berg
Here are all 3 methods that have been suggested.
* Compute X! by 3 methods. * Read in some sample data. data list free/x. begin data 0 1 2 3 4 5 6 7 8 9 10 end data. * [1] David's method. COMPUTE Fac1=1. LOOP #=2 TO x. COMPUTE Fac1=Fac1*#. END LOOP. * [2] Ruben's method. compute Fac2=x. loop #=1 to x. if # > 1 Fac2=Fac2*(x-#+1). end loop. * [3] Albert-Jan's method (but without the macro). compute Fac3 = exp(lngamma(x+1)). formats x (f5.0) / fac1 fac2 fac3 (f12.0). List. * Ruben's method is giving the wrong result for 0!. OUTPUT: x Fac1 Fac2 Fac3 0 1 0 1 1 1 1 1 2 2 2 2 3 6 6 6 4 24 24 24 5 120 120 120 6 720 720 720 7 5040 5040 5040 8 40320 40320 40320 9 362880 362880 362880 10 3628800 3628800 3628800 Number of cases read: 11 Number of cases listed: 11 HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Well, if you want to be complete, here's
the simplest method of all.
spssinc trans result = fac /formula "math.factorial(x)". Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From: Bruce Weaver <[hidden email]> To: [hidden email], Date: 11/21/2012 01:57 PM Subject: Re: [SPSSX-L] Faculty Sent by: "SPSSX(r) Discussion" <[hidden email]> Here are all 3 methods that have been suggested. * Compute X! by 3 methods. * Read in some sample data. data list free/x. begin data 0 1 2 3 4 5 6 7 8 9 10 end data. * [1] David's method. COMPUTE Fac1=1. LOOP #=2 TO x. COMPUTE Fac1=Fac1*#. END LOOP. * [2] Ruben's method. compute Fac2=x. loop #=1 to x. if # > 1 Fac2=Fac2*(x-#+1). end loop. * [3] Albert-Jan's method (but without the macro). compute Fac3 = exp(lngamma(x+1)). formats x (f5.0) / fac1 fac2 fac3 (f12.0). List. * Ruben's method is giving the wrong result for 0!. OUTPUT: x Fac1 Fac2 Fac3 0 1 0 1 1 1 1 1 2 2 2 2 3 6 6 6 4 24 24 24 5 120 120 120 6 720 720 720 7 5040 5040 5040 8 40320 40320 40320 9 362880 362880 362880 10 3628800 3628800 3628800 Number of cases read: 11 Number of cases listed: 11 HTH. Ruben van den Berg wrote > The syntax below seems to work but I'm kinda surprised it doesn't raise an > error for the first case. It requests a loop from 1 to 0... which I > thought would be not valid... > data list free/id.begin data0 1 2 3 4 5 6 7 8 9 10end data. > comp fac=id.loop #=1 to id.if # > 1 fac=fac*(id-#+1).end loop.exe. > > >> Date: Wed, 21 Nov 2012 02:14:27 -0800 >> From: > fomcl@ >> Subject: Re: Faculty >> To: > SPSSX-L@.UGA >> >> > >> > How do you compute a faculty in SPSS ? >> > >> > 3! = 3*2*1. >> > >> > Couldn't find a function yet. >> > >> >> define !factorial (!positional !tokens(1)), >> compute factorial = exp(lngamma(!1+1)). >> !enddefine. >> >> !factorial 3. >> >> (as found somewhere in the vaults of the IBM website) >> >> ===================== >> To manage your subscription to SPSSX-L, send a message to >> > LISTSERV@.UGA > (not to SPSSX-L), with no body text except the >> command. To leave the list, send the command >> SIGNOFF SPSSX-L >> For a list of commands to manage subscriptions, send the command >> INFO REFCARD ----- -- Bruce Weaver [hidden email] http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." NOTE: My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Faculty-tp5716377p5716400.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
Administrator
|
Simplest only for people who have Python installed already (which rules out David with his v11.5 -- and most of the academic users I know, who have never even heard of Python). ;-)
The beauty of Albert-Jan's method is that it requires only SPSS Base, so it will work for everyone. So will David's, method, of course. David's method is more transparent, but I like the elegance of A-J's one liner. Here are some stats. Character counts include spaces. David’s method: 58 characters, requires only SPSS Base Albert-Jan’s Method: 33 characters (including spaces); requires only SPSS Base Jon’s Python-based extension command method: 57 spaces; requires fiddling around installing Python plus extension command stuff (if one has a recent enough version of SPSS)! Donning my flame retardant suit and ducking under the desk... ;-)
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
In reply to this post by Bruce Weaver
Oops! I thought factorial 0 was supposed to be zero (which is why I explicitly included zero in my own test data). But now even my own hand calculator seems to be against me... Good catch Bruce! > Date: Wed, 21 Nov 2012 12:56:12 -0800 > From: [hidden email] > Subject: Re: Faculty > To: [hidden email] > > Here are all 3 methods that have been suggested. > > * Compute X! by 3 methods. > * Read in some sample data. > > data list free/x. > begin data > 0 1 2 3 4 5 6 7 8 9 10 > end data. > > * [1] David's method. > COMPUTE Fac1=1. > LOOP #=2 TO x. > COMPUTE Fac1=Fac1*#. > END LOOP. > * [2] Ruben's method. > compute Fac2=x. > loop #=1 to x. > if # > 1 Fac2=Fac2*(x-#+1). > end loop. > * [3] Albert-Jan's method (but without the macro). > compute Fac3 = exp(lngamma(x+1)). > > formats x (f5.0) / fac1 fac2 fac3 (f12.0). > List. > > * Ruben's method is giving the wrong result for 0!. > > OUTPUT: > > x Fac1 Fac2 Fac3 > > 0 1 0 1 > 1 1 1 1 > 2 2 2 2 > 3 6 6 6 > 4 24 24 24 > 5 120 120 120 > 6 720 720 720 > 7 5040 5040 5040 > 8 40320 40320 40320 > 9 362880 362880 362880 > 10 3628800 3628800 3628800 > > Number of cases read: 11 Number of cases listed: 11 > > HTH. > > > Ruben van den Berg wrote > > The syntax below seems to work but I'm kinda surprised it doesn't raise an > > error for the first case. It requests a loop from 1 to 0... which I > > thought would be not valid... > > data list free/id.begin data0 1 2 3 4 5 6 7 8 9 10end data. > > comp fac=id.loop #=1 to id.if # > 1 fac=fac*(id-#+1).end loop.exe. > > > > > >> Date: Wed, 21 Nov 2012 02:14:27 -0800 > >> From: > > > fomcl@ > > >> Subject: Re: Faculty > >> To: > > > SPSSX-L@.UGA > > >> > >> > > >> > How do you compute a faculty in SPSS ? > >> > > >> > 3! = 3*2*1. > >> > > >> > Couldn't find a function yet. > >> > > >> > >> define !factorial (!positional !tokens(1)), > >> compute factorial = exp(lngamma(!1+1)). > >> !enddefine. > >> > >> !factorial 3. > >> > >> (as found somewhere in the vaults of the IBM website) > >> > >> ===================== > >> To manage your subscription to SPSSX-L, send a message to > >> > > > LISTSERV@.UGA > > > (not to SPSSX-L), with no body text except the > >> command. To leave the list, send the command > >> SIGNOFF SPSSX-L > >> For a list of commands to manage subscriptions, send the command > >> INFO REFCARD > > > > > > ----- > -- > Bruce Weaver > [hidden email] > http://sites.google.com/a/lakeheadu.ca/bweaver/ > > "When all else fails, RTFM." > > NOTE: My Hotmail account is not monitored regularly. > To send me an e-mail, please use the address shown above. > > -- > View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Faculty-tp5716377p5716400.html > Sent from the SPSSX Discussion mailing list archive at Nabble.com. > > ===================== > To manage your subscription to SPSSX-L, send a message to > [hidden email] (not to SPSSX-L), with no body text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the command > INFO REFCARD |
Administrator
|
In reply to this post by Bruce Weaver
FWIW: Evaluation of Binomial coefficient.
** N!/x! (N-x)! =Prod((Max(N-x+1),(x+1)):N)/Prod(1:N-x). NEW FILE. DATA LIST LIST / N X. BEGIN DATA 250 5 250 245 100 3 100 97 END DATA. ** Using cancellation **. COMPUTE CNX=1. COMPUTE ##=1. LOOP #= MAX(N-X+1,X+1) TO N . COMPUTE CNX=CNX*#/##. COMPUTE ##=##+1. END LOOP. ** REALLY BAD STUPID WAY **. COMPUTE NF=1. LOOP #=2 TO N. COMPUTE NF=NF*#. END LOOP. COMPUTE XF=1. LOOP #=2 TO X. COMPUTE XF=XF*#. END LOOP. COMPUTE NXF=1. LOOP #=2 TO N-X. COMPUTE NXF=NXF*#. END LOOP. COMPUTE CNX2=NF/(XF*NXF). FORMAT ALL (F20.0). LIST. N X CNX NF XF NXF CNX2 250 5 7817031300 . 120 . . 250 245 7817031300 . . 120 . 100 3 161700 9.3326215443944+157 6 9.6192759682482+151 161700 100 97 161700 9.3326215443944+157 9.6192759682482+151 6 161700 Number of cases read: 4 Number of cases listed: 4 >Warning # 536 >In the course of executing the transformations programs, at least one >calculation resulted in an infinite number. Likely causes are >multiplication that generated a number beyond the capacity of the computer >or division by zero. The result of the transformation has been assigned >the system missing value.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |