## Get pdf from proc fmm (truncated normal distribution)

Solved
Occasional Contributor
Posts: 6

# Get pdf from proc fmm (truncated normal distribution)

Hello,

I'm fitting a mixture of one truncated normal distribution and 1 normal distribution to my data. The code is:

proc fmm data=observations plot=density(bins=56);

model x = / dist=normal kmax=1 parms(8 1);

model +/ dist=truncnormal(3,.);

I want to export the resulting pdf for both distributions and added together. I've read through FMM manual, but I couldn't find the easy way of doing it, so I used another approach:

data pdf;

set example;

do x = 0 to 13 by 0.01;

f1 = Prior_1 * (1/ ((Var_1)**(1/2) * 2.506628)) * exp(-((x - Pred_1)**2) / (2 * (Var_1)**(1/2) * (Var_1)**(1/2)));

f2 = Prior_2 * (1/(cunormal(3-Pred_2)/(Var_2)**(1/2))(1/ ((Var_2)**(1/2) * 2.506628)) * exp(-((x - Pred_2)**2) / (2 * (Var_2)**(1/2) * (Var_2)**(1/2)));

f = f1 + f2;

output pdf;

keep f1 f2 f x;

end;

run;

It works fine with normal distribution, but how can I get the term  (1/(cunormal(1.38-Pred_2)/(Var_2)**(1/2)) to work? Since there is no such function is SAS. Could someone please advise me how I can get the actual pdf for truncated normal and export it?

Thank you.

Accepted Solutions
Solution
‎11-21-2014 10:21 AM
SAS Super FREQ
Posts: 3,835

## Re: Get pdf from proc fmm (truncated normal distribution)

Yes, you can implement the function in the DATA step. The CDF and PDF functions are both available in the DATA step. In place of the CHOOSE function, you can use an IF-THEN/ELSE statement.

You can inline the computation, so no need to define the pdfTN function.

All Replies
SAS Super FREQ
Posts: 3,835

## Re: Get pdf from proc fmm (truncated normal distribution)

You should use the PDF function to produce the PDF for the normal distribution. For details on the PDF function (and the related CDF, QUANTILE, and RAND functions) see Four essential functions for statistical programmers - The DO Loop

You can also use the PDF of the normal distribution to compute the PDF for the truncated normal distribution. See http://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas/

Occasional Contributor
Posts: 6

## Re: Get pdf from proc fmm (truncated normal distribution)

Thank you for your reply. Is there a way to write the following code without using proc iml?

start pdfTN(x, mu, sigma, a, b);

/* Support one-sided truncation. Define Phi(.M)=0 and Phi(.P)=1 */

cdf_a = choose(a=., 0, cdf("Normal", a, mu, sigma)); /* Phi(a) */

cdf_b = choose(b=., 1, cdf("Normal", b, mu, sigma)); /* Phi(b) */

return( pdf("Normal", x, mu, sigma) / (cdf_b - cdf_a) );

finish;

Or how can I access the function pdfTN in my code?

data pdf;

set example;

do x = 0 to 13 by 0.01;

f1=Prior_1*pdf("Normal",x,Pred_1,Var_1);

f2=Prior_2*pdfTN(x,Pred_2,Var_2,3,8);

output pdf;

keep f1 f2;

end;

run;

When I run the code, the error message is  "The function PDFTN is unknown, or cannot be accessed."

Solution
‎11-21-2014 10:21 AM
SAS Super FREQ
Posts: 3,835

## Re: Get pdf from proc fmm (truncated normal distribution)

Yes, you can implement the function in the DATA step. The CDF and PDF functions are both available in the DATA step. In place of the CHOOSE function, you can use an IF-THEN/ELSE statement.

You can inline the computation, so no need to define the pdfTN function.

Occasional Contributor
Posts: 6

## Re: Get pdf from proc fmm (truncated normal distribution)

Thank you for your help. It was very useful and my code works now.

🔒 This topic is solved and locked.