Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 07-04-2014 04:00 AM
(1341 views)

Hi everyone,

I'm facing an issue when doing an itrative sum using factorials. This is the first time I use both an iterative sum and the FACT function so I guess there are a lot of things wrong in my code.

Want I want is the formula I hereby attached. The output should always be between 0 and 1 since Pt is always around 0,5. Nevertheless ith my formula I obtain totlly different numbers that can be super high.

Here is my code:

**dATA** LSV.right;

set lsv.left;

right=**0**;

do i=**0** to n;

calc1=fact(n) / fact(i) / fact(n-i);

calc2= calc1 * i/n ;

calc3=calc2* (**1**-Pt)**(n-i) ;

calc4= calc3* abs ((i/n) -Pt);

right=calc4 + right;

abs=abs((i/n)-Pt);

Prod=calc1*calc2*calc3*calc4;

end;

run;

Could you please help me with that ? thanks

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Yes, your code now seems to match your formula.

It's possible that you are running into overflow issues, in which case you'll need to be more careful about the order of operations.

What sort of values are you expecting, and what are you getting instead? (We don't have your data.)

What is your formula supposed to calculate? Are you maybe missing a Pt**i factor?

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Looks like you are multiplying everything twice. You can just skip the last two lines (abs and Prod) in the body of the loop.

Also, you can use the COMB function to compute calc1 directly, without using FACT.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks I've corrected my formula but it's still giving me the same numbers that are way too high.

dATA LSV.right;

set lsv.left;

right=0;

do i=0 to n;

right= comb(n,i)* (i/n) * ((1-Pt)**(n-i)) * abs ((i/n) -Pt) +right;

end;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Yes, your code now seems to match your formula.

It's possible that you are running into overflow issues, in which case you'll need to be more careful about the order of operations.

What sort of values are you expecting, and what are you getting instead? (We don't have your data.)

What is your formula supposed to calculate? Are you maybe missing a Pt**i factor?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

A colleague has suggested using a log transformation to reduce the scale and help avoid overflow. He sent this code:

data left;

input n pt;

cards;

1000 0.9995

1000 0.95

1000 0.5

1000 0.005

run;

data right;

set left;

sum = 0;

/****************************************************/

/* when i = 0 term = 0 */

/* the last term is always bounded by max(1,abs(pt) */

/* with pt small log1px(-pt) should be used */

/* consider using lbeta instead of lfacts */

/****************************************************/

log1mpt = log(1-pt);

do i=0 to n;

if (i = 0) then logterm = -10000000;

else do;

logterm = lfact(n) - lfact(i) - lfact(n-i) + log(i/n) + (n-i) * log1mpt;

end;

sum= sum + exp(logterm) * abs(i/n-pt);

end;

put sum=;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks a lot. i just fund out my problem. I was a misintepretation of the formula. As you said earlier I had forgotten a Pt**i factor.

Thank you very much for your precious help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Glad to help. Please mark this question as Answered.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.