<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: The Re: Calculating the CDF of a multivariate T in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311682#M16440</link>
    <description>&lt;P&gt;Thanks for the quick response!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Apologies, I probably wasn't explicit enough, yes for 2D problems with continous and discrete the conditional can be used, but when you have a 3D situation (say with responses Continous X1, Discrete X2 and Discrete X3), I think you would need the CDF for the X2 and X3 evaluation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. I am not very strong in IML, so thanks for explaining a better way to do it - I will investigate. However, if I am aiming to have a function to help&amp;nbsp;evaluate the likelihood in MCMC, I think I would need something to work out the likelihood for each line of data (or possibly all the data in a matrix using the JOINTMODEL option). So I think that would mean a call to R for each set of parameter values generated in MCMC, which as you say will not be efficient.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your useful input!&lt;/P&gt;</description>
    <pubDate>Tue, 15 Nov 2016 14:10:08 GMT</pubDate>
    <dc:creator>_TomDrury</dc:creator>
    <dc:date>2016-11-15T14:10:08Z</dc:date>
    <item>
      <title>Re: The Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311682#M16440</link>
      <description>&lt;P&gt;Thanks for the quick response!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Apologies, I probably wasn't explicit enough, yes for 2D problems with continous and discrete the conditional can be used, but when you have a 3D situation (say with responses Continous X1, Discrete X2 and Discrete X3), I think you would need the CDF for the X2 and X3 evaluation.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. I am not very strong in IML, so thanks for explaining a better way to do it - I will investigate. However, if I am aiming to have a function to help&amp;nbsp;evaluate the likelihood in MCMC, I think I would need something to work out the likelihood for each line of data (or possibly all the data in a matrix using the JOINTMODEL option). So I think that would mean a call to R for each set of parameter values generated in MCMC, which as you say will not be efficient.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your useful input!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 14:10:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311682#M16440</guid>
      <dc:creator>_TomDrury</dc:creator>
      <dc:date>2016-11-15T14:10:08Z</dc:date>
    </item>
    <item>
      <title>Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310212#M16448</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to be able to calculate the CDF of the Bivariate (and multivariate) T distribution. Has anyone got a reliable and accurate way to do this in SAS? I suspect IML would be needed hence posting here. R has a package mvtnorm that&amp;nbsp;can do it, and&amp;nbsp;I think it is based methods in the paper by Genz &amp;amp; Bretz (attached). Rather than try to digest what looks to be very complicated separation algebra in the paper, I hoped someone may have a solution&amp;nbsp;already?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A very simple way to do it would be a MC integration algorithm; generating large numbers of MVT values from IML and averaging them. I have tried this&amp;nbsp;for the bivariate T (see embedded SAS program) but I don’t think this would be accurate and quick enough (I compared the accuracy of the process for the MVN in the code with the SAS function PROBBNRM and it isn’t great).&lt;/P&gt;&lt;P&gt;Any ideas or solutions would be welcome!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Tom.&lt;/P&gt;&lt;P&gt;Using SAS 9.3 and 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;**********************************************;
***    TEST BASIC MONTE CARLO INEGRATION   ***;
**********************************************;

ods html close;
ods listing;

*** MACRO TO GENERATE MONTE CARLO INTEGRATED MVT CDF ESTIMATE IN IML ***;
%macro mc_mvt;
  proc iml;
    call randseed(&amp;amp;seed.);
    mvector = {0 0};
    smatrix = {1 &amp;amp;rho., &amp;amp;rho. 1};
    yvector = randmvt(10000, &amp;amp;df., mvector, smatrix);
    ind = (yvector[,1] &amp;lt;= &amp;amp;x1.) &amp;amp; (yvector[,2] &amp;lt;= &amp;amp;x2.);
    probt = ind[:,];
    call symput ("probt",char(probt));
  quit;
  run;
%mend;

*** MACRO TO GENERATE MONTE CARLO INTEGRATED MVN CDF ESTIMATE IN IML ***;
%macro mc_mvn;
  proc iml;
    call randseed(&amp;amp;seed.);
    mvector = {0 0};
    smatrix = {1 &amp;amp;rho., &amp;amp;rho. 1};
    yvector = randnormal(10000, mvector, smatrix);
    ind = (yvector[,1] &amp;lt;= &amp;amp;x1.) &amp;amp; (yvector[,2] &amp;lt;= &amp;amp;x2.);
    probn = ind[:,];
    call symput ("probn",char(probn));
  quit;
  run;
%mend;

*** FCMP FUNCTION TO RUN MACRO FROM WITHIN DATASTEP ***;
proc fcmp outlib = work.stats.functions;

  function cdf_mvt(x1,x2,rho,df,seed);
    rc = run_macro("mc_mvt",x1,x2,rho,df,seed,probt);
    if rc = 0 then return(probt);
    else return(.);
  endsub;

quit;
run;

*** FCMP FUNCTION TO RUN MACRO FROM WITHIN DATASTEP ***;
proc fcmp outlib = work.stats.functions;

  function cdf_mvn(x1,x2,rho,seed);
    rc = run_macro("mc_mvn",x1,x2,rho,seed,probn);
    if rc = 0 then return(probn);
    else return(.);
  endsub;

quit;
run;

*** CALL FUNCTIONS FROM DATASTEP ***;
options cmplib = (work.stats);
data results;
  do i = -1 to 1 by 0.25;
  do j = -1 to 1 by 0.25;
    r=0.8;
    v=2;
    seeds=12348765;
    mc_probt  = cdf_mvt(i,j,r,v,seeds);
    mc_probn  = cdf_mvn(i,j,r,seeds);
    sas_probn = probbnrm(i,j,r);
    output;
  end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Nov 2016 19:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310212#M16448</guid>
      <dc:creator>_TomDrury</dc:creator>
      <dc:date>2016-11-08T19:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310301#M16449</link>
      <description>&lt;PRE&gt;
You found the right place to post. Here are some paper for help.

http://blogs.sas.com/content/iml/2013/09/25/compute-contours-of-the-bivariate-normal-cdf.html

http://blogs.sas.com/content/iml/2013/09/20/gradient-of-the-bivariate-normal-cumulative-distribution.html

http://blogs.sas.com/content/iml/2012/07/11/visualize-the-bivariate-normal-cumulative-distribution.html


or you could use PROC COPULA to simulate multivariate T distribution and further get the CDF ?


&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 07:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310301#M16449</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-09T07:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310384#M16450</link>
      <description>&lt;P&gt;There's nothing intrinsically wrong with your simulation code, except that the sample size is too small for this computation. The Monte Carlo error is proportional to 1/sqrt(n), so I'd use at least 100,000 simulated observations. Depending on the application, you might want to simulate 1 million obs, especially if you are interested in behavior in the tails.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change your simulation code to use 100,000 like this:&lt;/P&gt;
&lt;P&gt;yvector = randnormal(&lt;FONT color="#FF0000"&gt;100000&lt;/FONT&gt;, mvector, smatrix);&lt;/P&gt;
&lt;P&gt;and then run this PROC SGPLOT to compare the MC results with the PROBBNRM computations. You'll see that the curves line up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sgplot data=results;
series x=j y=mc_probn / group=i curvelabel;
series x=j y=sas_probn / group=i curvelabel;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are going to use many points of the CDF, &amp;nbsp;you might want to pre-compute the CDF values on a fine grid and save the results to a permanent data set. &amp;nbsp;Then when you want a particular value of x you can look up the nearest grid points and interpolate from the pre-computed values. That approach is only feasible in low dimensions, but you said "bivariate", so it will certainly work for dim=2. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that if you intend to compute many values, you don't need to generate a new random sample for every call. Make a single (huge) random sample and use it to estimate the CDF on a fine grid.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 14:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310384#M16450</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-11-09T14:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310550#M16451</link>
      <description>&lt;PRE&gt;
@Rick,

Can you use Markov chain Monte Carlo (MCMC) simulation  to get it ,just as PROC MCMC did ?
That would be very interesting.



&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Nov 2016 02:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/310550#M16451</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-10T02:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311657#M16452</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp﻿&lt;/a&gt;&amp;nbsp;&amp;amp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS﻿&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help and ideas! I will look at increasing the number of simulations and see if there is a way to use this for what I want. The reason for asking for&amp;nbsp;a quick function to evaluate the bivariate T CDF is because I would like to fit joint bayesian t-copula models for mixed continous and discrete data in SAS. To do this finite differencing of the joint CDF is used for&amp;nbsp;the discrete portions&amp;nbsp;of the likelihood. I have successfully done this for&amp;nbsp;the gaussian copula, as I can use PROBBNRM.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For completeness I also tried cheating by calling the function from R using IML (code below) however this is not very quick either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*** OPTION TO SET UP WHERE R SOFTWARE IS STORED ON COMPUTER ***;
options set=R_HOME='C:\Program Files\R\R-2.15.3';

*** MACRO TO GENERATE R CALL ***;
%macro r_mvt;
%let workpath = %sysfunc(getoption(work));
filename Rcall "&amp;amp;workpath.\Rcall.sas";
data _null_;
   file Rcall;
   put @1 "*** IML CODE TO SUBMIT TO R ***;" /;
   put @1
"proc iml;"/ 
"  rmatrix = {1 &amp;amp;rho., &amp;amp;rho. 1};"/
"  xvector = {&amp;amp;x1., &amp;amp;x2.};"/
"  df      = &amp;amp;df.;"/
"  call ExportMatrixToR(rmatrix, 'rmat');"/
"  call ExportMatrixToR(xvector, 'xvec');"/
"  call ExportMatrixToR(df, 'dfs');"/
"  submit / R;"/
"    library('mvtnorm');"/
"    ll &amp;lt;- c(-Inf,-Inf);"/
"    ul &amp;lt;- as.numeric(xvec);"/
"    df &amp;lt;- as.numeric(dfs);"/
"    probt &amp;lt;- pmvt(lower=ll, upper=ul, df=df, corr=rmat, algorithm = GenzBretz());"/
"  endsubmit;"/
"  run ImportMatrixFromR(prob_t, 'probt');"/
"  call symput ('probt',char(prob_t));"/
"quit;"/
"run;"
;
run;
%include "&amp;amp;workpath.\Rcall.sas";
%mend;

*** FCMP FUNCTION TO RUN MACRO FROM WITHIN DATASTEP OR MCMC ***;
proc fcmp outlib = work.stats.functions;

  function r_pmvt(x1,x2,rho,df);
    rc = run_macro("r_mvt",x1,x2,rho,df,probt);
    if rc = 0 then return(probt);
    else return(.);
  endsub;

quit;
run;

*** CALL FUNCTIONS FROM DATASTEP ***;
options cmplib = (work.stats);
data results;
  do i = -1 to 1 by 1;
  do j = -1 to 1 by 1;
  do k = -1 to 1 by 0.1;
    v=2;
    r_probt  = r_pmvt(i,j,k,v);
    output;
  end;
  end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 11:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311657#M16452</guid>
      <dc:creator>_TomDrury</dc:creator>
      <dc:date>2016-11-15T11:51:40Z</dc:date>
    </item>
    <item>
      <title>The Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311669#M16453</link>
      <description>&lt;P&gt;1. You say "&lt;SPAN&gt;finite differencing of the joint CDF is used for&amp;nbsp;the discrete portions&amp;nbsp;of the likelihood." If I understand this sentence correctly, you might not need the joint CDF because the derivative of the joint CDF is simply the conditional density. For example, dF(x,y)/dy = F(x | y), which reduces it to a 1D problem. If you have a gradient direction, I believe you can still reduce this problem to a 1D integration of the MVT PDF, which you can then transform into a univariate CDF of the t distribution by using a change of variables. Think about it.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2. Your "calling R" code is very inefficient. Your code starts and stops PROC IML and R for every scalar value of (i,j,k). There is no need to wrap PROC IML in a macro and call it from the DATA step. You can do this all from IML. First&amp;nbsp;use the EXPANDGRID function to create a grid of (i,j,k) values as an Nx3 matrix. &amp;nbsp;Transfer that matrix to R. Call whatever R functions you want on the vectors. Transfer back the vectors. It should require starting IML and R only one time. For details and examples of the correct way to call R from SAS/IML, see&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;The &lt;A href="http://support.sas.com/documentation/cdl/en/imlug/68150/HTML/default/viewer.htm#imlug_r_toc.htm" target="_self"&gt;SAS/IML documentation chapter on Calling R&lt;/A&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.sas.com/content/iml/2011/05/13/calling-r-from-sasiml-software.html" target="_self"&gt;A blog post on calling R&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.sas.com/content/iml/2011/10/31/video-calling-r-from-the-sasiml-language.html" target="_self"&gt;A video about calling R from SAS/IML&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;The article &lt;A href="http://blogs.sas.com/content/iml/2013/11/25/twelve-advantages-to-calling-r-from-the-sasiml-language.html" target="_self"&gt;"Twelve advantages to calling R from teh SAS/IML language"&lt;/A&gt;&amp;nbsp;also addresses reasons why it is better to call R directly from a SAS/IML program than from a macro.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 13:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311669#M16453</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-11-15T13:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: The Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311688#M16441</link>
      <description>&lt;P&gt;Are you planning to evaluate the likelihood function in PROC MCMC? If so, I can move this discussion to the Statistical Procedures community where the MCMC experts are more likely to see it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Nov 2016 14:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311688#M16441</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-11-15T14:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: The Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311693#M16442</link>
      <description>If that is possible - that would be great! Thanks!</description>
      <pubDate>Tue, 15 Nov 2016 14:28:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/311693#M16442</guid>
      <dc:creator>_TomDrury</dc:creator>
      <dc:date>2016-11-15T14:28:13Z</dc:date>
    </item>
    <item>
      <title>Re: The Re: Calculating the CDF of a multivariate T</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/499919#M25797</link>
      <description>&lt;P&gt;Prof Frank Bretz wrote several programs for computation of probabilities from multivariate normal and t distributions:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://www.biostat.uni-hannover.de/89.html#c137" target="_blank"&gt;https://www.biostat.uni-hannover.de/89.html#c137&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 14:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Re-The-Re-Calculating-the-CDF-of-a-multivariate-T/m-p/499919#M25797</guid>
      <dc:creator>ROBIZ7</dc:creator>
      <dc:date>2018-09-28T14:46:03Z</dc:date>
    </item>
  </channel>
</rss>

