<?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: Get Tangency Portfolio - Mean Variance Model in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945569#M6281</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;&lt;/P&gt;&lt;P&gt;This is extremely helpful, thank you. I ran some simulations with 3 asset classes based on the code from the 2017 sas paper, and it's very convenient!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question - I noticed that in the final graph (Tangency with EF), the "Star" i.e., the tangent portfolio, doesn't lie on the EF (blue line), as the number of assets increases. I tried it for 15 to 20 asset classes, the "Start" gets pushed more and more on the top half of the graph, away from EF. I even checked the Frontier and Tangency datasets, and the tangent (return, std) values, do not feature in the frontier data in these instances.&lt;/P&gt;&lt;P&gt;Any idea what's the reason for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: I didn't change anything from your code. just increased N from 3 to 15, and so on. Even the number of simulated rows (10,000) and rest of the distribution parameters are the same.&lt;/P&gt;</description>
    <pubDate>Sun, 29 Sep 2024 04:31:45 GMT</pubDate>
    <dc:creator>AbhisekGhosh</dc:creator>
    <dc:date>2024-09-29T04:31:45Z</dc:date>
    <item>
      <title>Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/258744#M2644</link>
      <description>Please don't reply . Just give someone who need it .
&lt;PRE&gt;
proc iml;
/* the number of assets */
n=2;
/* riskless rate - Government's bond rate */
f=0.02 ;




/* Start Calculated */
/* two assets and populate data */
asset=j(100,n,.);
call randseed(1234);
call randgen(asset,'normal');

/* the expect return of two assets (mean) */
return=asset[:,];

/* the variance and covariance of two assets */
cov=cov(asset);

/* the variance of portfolio */
new_cov=2#cov-diag(vecdiag(cov));
col=col(new_cov);
row=row(new_cov);
idx=loc(col&amp;gt;=row);
p_cov=row[idx]||col[idx]||new_cov[idx];


/* p[1] is expect return of porfolio
   p[2] is variance of porfolio  */
p=j(1,2,.);
/* the tangency portfolio */
start max_slope(x) global(f,p,p_cov,return);
p[1]=(return#x)[+];
p[2]=(x[p_cov[,1]]#x[p_cov[,2]]#p_cov[,3])[+];
/*riskless rate's variance is zero*/
k=(p[1]-f)/p[2];
return (k);
finish;

ods output IterHist=IterHist;
con=repeat({0,1,1},1,n)||{. .,. .,0 1};
x=j(1,n,1/n);
optn={1 3};
call nlptr(xres,rc,"max_slope",x,optn,con);

quit;


/************************************/
/************************************/
/************************************/
/************************************/
/* Test if it is tangency portfolio */
/* Take two assets above for example*/
data _null_;
 set IterHist;
 call symputx('k',OptCrit);
 call symputx('f','0.02');
run;
proc iml;
/* The same code as above. Start */
n=2;
f=0.02 ;
asset=j(100,n,.);
call randseed(1234);
call randgen(asset,'normal');
return=asset[:,];
cov=cov(asset);
new_cov=2#cov-diag(vecdiag(cov));
col=col(new_cov);
row=row(new_cov);
idx=loc(col&amp;gt;=row);
p_cov=row[idx]||col[idx]||new_cov[idx];
/* The same code as above. End*/


p=j(100,2);
x=j(1,n,.);
do i=1 to 100;
 x[1]=0.01#i;
 x[2]=1-x[1];
 p[i,1]=(return#x)[+];
 p[i,2]=(x[p_cov[,1]]#x[p_cov[,2]]#p_cov[,3])[+];
end;
create test from p[c={return variance}];
append from p;
close;
quit;
ods graphics/reset;
proc sgplot data=test;
series x=variance y=return/ lineattrs=(thickness=2) legendlabel='Portfolio' curvelabel='Efficient Frontier';
lineparm x=0 y=&amp;amp;f  slope=&amp;amp;k/ legendlabel='Tangency Portfolio' lineattrs=graphdata2(thickness=2);
refline &amp;amp;f / axis=y;
xaxis label='Portfolio Variance';
yaxis label='Portfolio Return';
run;
/* Bingo !!!! */
/************************************/
/************************************/
/************************************/
/************************************/

&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Mar 2016 07:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/258744#M2644</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-03-24T07:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/347004#M3388</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thanks for this program. If I used correlated assets, I don't seem to get the correct tangency curve. &amp;nbsp;What needs to change if you use the following?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sigma = {1 0.2, 0.2 1};&amp;nbsp;&lt;BR /&gt;mu = {0 0};&amp;nbsp;&lt;BR /&gt;asset = randnormal(100, mu, Sigma); /* generate obs from MVN(mu, Sigma) */&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 11:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/347004#M3388</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-04-04T11:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/347336#M3389</link>
      <description>&lt;PRE&gt;
@Rick,
You are right. I just simulate lots of portfolios ,and can't guarantee you get efficient portfolio frontier.
If you want get that efficient portfolio frontier, you need solve lots of optimize problem.
Something like :



return=asset[:,];

start min_variance(x) global(cov);
/*a portfolio 's variance*/
k=x*cov*x`;
return (k);
finish;

do r=0.01 to 0.1; 

/*constraint portfolio's return is 0.01,0.02,0.03,0.04...........*/
/*    return*x`=0.01,0.02,0.03,0.04    */
/*change the following CON, I forgot syntax*/
con=repeat({0,1,1},1,n)||{. .,. .,0 return};
x=j(1,n,1/n);
optn={1 3};
call nlptr(xres,rc,"min_variance",x,optn,con);

print xres ;

end;



&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Apr 2017 13:02:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/347336#M3389</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-05T13:02:42Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/348145#M3390</link>
      <description>&lt;P&gt;&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;Finally I have some time to go through.&lt;/P&gt;
&lt;P&gt;Better make mu great than 0 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code above is too old.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can get my new code at SAS Global Forum 2017 ,&lt;/P&gt;
&lt;P&gt;Search "Get Tangency Portfolio" at SGF2017.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
Sigma = {1 0.2, 0.2 1}; 
mu = {0.2 0.2}; 
asset = randnormal(10000, mu, Sigma); /* generate obs from MVN(mu, Sigma) */

/* the expect return of two assets (mean) */
return=asset[:,];

/* the variance and covariance of two assets */
cov=cov(asset);


start min_variance(x) global(cov); 
return (x*cov*x`);
finish;

x=j(1,2,0.5);
optn={0 1 0.001};
do r=0.02 to 0.34 by 0.02; 
con={ 0 0 . .,
      1 1 . .}//
     (return||0||r);
call nlpnrr(rc,xres,"min_variance",x,optn,con);

r_cov=r_cov//(r||xres*cov*xres`);
end;

print r_cov;
call series x=r_cov[,2] y=r_cov[,1];
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8207i7B86216EE089BD64/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="download.png" title="download.png" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 15:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/348145#M3390</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-07T15:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945569#M6281</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;&lt;/P&gt;&lt;P&gt;This is extremely helpful, thank you. I ran some simulations with 3 asset classes based on the code from the 2017 sas paper, and it's very convenient!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question - I noticed that in the final graph (Tangency with EF), the "Star" i.e., the tangent portfolio, doesn't lie on the EF (blue line), as the number of assets increases. I tried it for 15 to 20 asset classes, the "Start" gets pushed more and more on the top half of the graph, away from EF. I even checked the Frontier and Tangency datasets, and the tangent (return, std) values, do not feature in the frontier data in these instances.&lt;/P&gt;&lt;P&gt;Any idea what's the reason for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: I didn't change anything from your code. just increased N from 3 to 15, and so on. Even the number of simulated rows (10,000) and rest of the distribution parameters are the same.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Sep 2024 04:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945569#M6281</guid>
      <dc:creator>AbhisekGhosh</dc:creator>
      <dc:date>2024-09-29T04:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945579#M6282</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Hi,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The code posted yesterday is not right&amp;nbsp; because&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;I did not consider constrain(sum of these proportional =1) when to calculated Efficient Frontier.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Now The code has been updated.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Try the following code .&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;You need to change 'r' to be small enough&amp;nbsp;to make your model convergency.&lt;/P&gt;
&lt;PRE&gt;do r=0 to 0.04 by 0.001; /*expect return of portfolio*/&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi. That long time ago, my memory is almost faded away. As I said I just simulate lots and lots of portfolios ,NOT actually calculated Efficient Frontier. If you want it you need solve many optimal problems.&lt;/P&gt;
&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=20;





proc iml;
/* the number of assets */
n=&amp;amp;n.;
/* riskless rate - Government's bond rate */
f=0.02 ;

/* Start Calculated */
/* two assets and populate data */
asset=j(1000,n,.);
call randseed(1234);
call randgen(asset,'normal');

/* the expect return of two assets (mean) */
return=asset[:,];

/* the variance and covariance of two assets */
cov=cov(asset);

/* p[1] is expect return of porfolio
   p[2] is variance of porfolio  */
p=j(1,2,.);
/* the tangency portfolio */
start max_slope(x) global(f,p,cov,return);
p[1]=(return#x)[+];
p[2]=x*cov*x`;   
/*riskless rate's variance is zero*/
k=(p[1]-f)/p[2];
return (k);
finish;

ods output IterHist=IterHist;
 con=repeat({0,1,1},1,n)||{. .,. .,0 1};   /*&amp;lt;---- sum of these proportional =1 */
x=j(1,n,1/n);
optn={1 3};
call nlptr(xres,rc,"max_slope",x,optn,con);

quit;










/************************************/
/************************************/
/************************************/
/************************************/
/* Test if it is tangency portfolio */
/* Take two assets above for example*/
data _null_;
 set IterHist;
 call symputx('k',OptCrit);
 call symputx('f','0.02');
run;
proc iml;
/* The same code as above. Start */
n=&amp;amp;n.;
f=0.02 ;
asset=j(1000,n,.);
call randseed(1234);
call randgen(asset,'normal');
return=asset[:,];
cov=cov(asset);
/* The same code as above. End*/






start min_variance(x) global(cov); 
return (x*cov*x`);
finish;

x=j(1,n,1/n);
optn={0 1 0.001};
do r=0 to 0.04 by 0.001;   /*expect return of portfolio*/
con=(repeat({0,1,1},1,n)||{. .,. .,0 1})//(return||0||r);
call nlpnrr(rc,xres,"min_variance",x,optn,con) ;
r_cov=r_cov//(r||xres*cov*xres`);
end;

create test from r_cov[c={return variance}];
append from r_cov;
close;
quit;

ods graphics/reset;
proc sgplot data=test;
series x=variance y=return/ lineattrs=(thickness=2) legendlabel='Portfolio' curvelabel='Efficient Frontier';
lineparm x=0 y=&amp;amp;f  slope=&amp;amp;k/ legendlabel='Tangency Portfolio' lineattrs=graphdata2(thickness=2);
refline &amp;amp;f / axis=y;
xaxis label='Portfolio Variance';
yaxis label='Portfolio Return';
run;
/* Bingo !!!! */
/************************************/
/************************************/
/************************************/
/************************************/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1727661411558.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100755i7DD8C6A215B0AAE0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1727661411558.png" alt="Ksharp_0-1727661411558.png" /&gt;&lt;/span&gt;&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Actually it is a&amp;nbsp;Quadratic Programming. You could solve this mean-variance model by SAS/IML builded-in functions : CALL LCP() or CALL QPSOLVE() or CALL NLPQUA()&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A href="https://blogs.sas.com/content/iml/2024/07/24/new-quadratic-optimization.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2024/07/24/new-quadratic-optimization.html&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A href="https://blogs.sas.com/content/iml/2024/07/15/isotonic-regression.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2024/07/15/isotonic-regression.html&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1727598092102.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100742i75E0D31E6DA4239B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1727598092102.png" alt="Ksharp_0-1727598092102.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_1-1727598132126.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100743i0EA5A5C8CD699F79/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1727598132126.png" alt="Ksharp_1-1727598132126.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_2-1727598180379.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100744iD8C95061C9AA6F97/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_2-1727598180379.png" alt="Ksharp_2-1727598180379.png" /&gt;&lt;/span&gt;&lt;/P&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 02:02:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945579#M6282</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-01T02:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945585#M6283</link>
      <description>&lt;P&gt;Thank you so much for such a quick and detailed response&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;(amazing after 7 years :)).&lt;/P&gt;&lt;P&gt;I will definitely check and try this out.&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Sep 2024 09:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945585#M6283</guid>
      <dc:creator>AbhisekGhosh</dc:creator>
      <dc:date>2024-09-29T09:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945702#M6285</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;I ran this with a few modifications&lt;/P&gt;&lt;P&gt;- the part to create assets returns data (I am using market data)&lt;/P&gt;&lt;P&gt;- used NLPNRA for both deriving and validating the tangency portfolio (was having trouble with convergence with NLPTR and NLPRR)&lt;/P&gt;&lt;P&gt;- adjusted the range of r in the simulation to get the display area in the graph.&lt;/P&gt;&lt;P&gt;It works!&lt;/P&gt;&lt;P&gt;Again, thanks a ton!&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2024 14:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945702#M6285</guid>
      <dc:creator>AbhisekGhosh</dc:creator>
      <dc:date>2024-09-30T14:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945712#M6286</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, couple of quick clarifications:&lt;/P&gt;&lt;P&gt;1. On Portfolio Variance: Should it be&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;p[2]=x*cov*x`&lt;/LI-CODE&gt;&lt;P&gt;or&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;p[2]=sqrt(x*cov*x`)&lt;/LI-CODE&gt;&lt;P&gt;usually, Sqrt would be the standard deviation, however I was looking back at the 2017 paper, and there the variance has been defined with Sqrt.&lt;/P&gt;&lt;P&gt;2. Calculation of K Slope (Sharpe Ratio);&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;k=(p[1]-f)/p[2];&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;where&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;p[2]=x*cov*x`; &lt;/LI-CODE&gt;&lt;P&gt;Sharpe ratio is defined in terms of Standard deviation. So, if this is the definition of p[2], then should the Sharpe ratio be?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;k=(p[1]-f)/p[2];
/*where p[2]=sqrt(x*cov*x`)*/&lt;/LI-CODE&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2024 17:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945712#M6286</guid>
      <dc:creator>AbhisekGhosh</dc:creator>
      <dc:date>2024-09-30T17:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945740#M6287</link>
      <description>Did you try SAS/IML builded-in subroutines?  CALL LCP() or CALL QPSOLVE() or CALL NLPQUA() .&lt;BR /&gt;As I posted with pictures.</description>
      <pubDate>Tue, 01 Oct 2024 00:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945740#M6287</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-01T00:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945741#M6288</link>
      <description>&lt;P&gt;1)&lt;BR /&gt;Portfolio Variance should be&amp;nbsp; p[2]=x*cov*x` .&lt;BR /&gt;Portfolio&amp;nbsp; standard deviation should be p[2]=sqrt(x*cov*x`).&lt;BR /&gt;In the picture with 2017 paper should be STD at X axis side, if it was not , that might be my mistake.&lt;BR /&gt;&lt;BR /&gt;2)&lt;BR /&gt;Calculation of K Slope (Sharpe Ratio);&lt;BR /&gt;Yes. you are right. But here I only consider about Tangency Point(a.k.a the biggest slope with Efficient Frontier Curve).&lt;BR /&gt;Here I calculated Efficient Frontier Curve with Portfolio Variance, not STD.&lt;BR /&gt;Therefore you need to use Variance to calculate slope K ,not STD.&lt;BR /&gt;&lt;BR /&gt;If you get Efficient Frontier Curve with Portfolio STD. then you should use p[2]=sqrt(x*cov*x`) to get slope K as I showed in my paper.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 01:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945741#M6288</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-01T01:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945744#M6289</link>
      <description>&lt;P&gt;As you wish I used Portfolio Standard Deviation to calcualte Efficient Frontier.&lt;/P&gt;
&lt;P&gt;But it looks like it is not easy to get convergency of model with STD. &lt;STRONG&gt;So I suggest to use Portfolio Variance ,not STD .&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n=20;





proc iml;
/* the number of assets */
n=&amp;amp;n.;
/* riskless rate - Government's bond rate */
f=0.02 ;

/* Start Calculated */
/* two assets and populate data */
asset=j(1000,n,.);
call randseed(1234);
call randgen(asset,'normal');

/* the expect return of two assets (mean) */
return=asset[:,];

/* the variance and covariance of two assets */
cov=cov(asset);

/* p[1] is expect return of porfolio
   p[2] is variance of porfolio  */
p=j(1,2,.);
/* the tangency portfolio */
start max_slope(x) global(f,p,cov,return);
p[1]=(return#x)[+];
p[2]=sqrt(x*cov*x`);   
/*riskless rate's variance is zero*/
k=(p[1]-f)/p[2];
return (k);
finish;

ods output IterHist=IterHist;
 con=repeat({0,1,1},1,n)||{. .,. .,0 1};   /*&amp;lt;---- sum of these proportional =1 */
x=j(1,n,1/n);
optn={1 3};
call nlpnra(xres,rc,"max_slope",x,optn,con);

quit;










/************************************/
/************************************/
/************************************/
/************************************/
/* Test if it is tangency portfolio */
/* Take two assets above for example*/
data _null_;
 set IterHist;
 call symputx('k',OptCrit);
 call symputx('f','0.02');
run;
proc iml;
/* The same code as above. Start */
n=&amp;amp;n.;
f=0.02 ;
asset=j(1000,n,.);
call randseed(1234);
call randgen(asset,'normal');
return=asset[:,];
cov=cov(asset);
/* The same code as above. End*/






start min_std(x) global(cov); 
return (sqrt(x*cov*x`));
finish;

x=j(1,n,1/n);
optn={0 1 0.001};
do r=0 to 0.046 by 0.001;   /*expect return of portfolio*/
con=(repeat({0,1,1},1,n)||{. .,. .,0 1})//(return||0||r); /*&amp;lt;---- sum of these proportional =1 */
call nlpnra(rc,xres,"min_std",x,optn,con) ;
r_cov=r_cov//(r||sqrt(xres*cov*xres`));
end;

create test from r_cov[c={return std}];
append from r_cov;
close;
quit;

ods graphics/reset;
proc sgplot data=test;
series x=std y=return/ lineattrs=(thickness=2) legendlabel='Portfolio' curvelabel='Efficient Frontier';
lineparm x=0 y=&amp;amp;f  slope=&amp;amp;k/ legendlabel='Tangency Portfolio' lineattrs=graphdata2(thickness=2);
refline &amp;amp;f / axis=y;
xaxis label='Portfolio STD';
yaxis label='Portfolio Return';
run;
/* Bingo !!!! */
/************************************/
/************************************/
/************************************/
/************************************/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1727745655437.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100772i136AC04213DC3FB7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1727745655437.png" alt="Ksharp_0-1727745655437.png" /&gt;&lt;/span&gt;&lt;/P&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, 01 Oct 2024 01:31:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945744#M6289</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-01T01:31:42Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945746#M6290</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;for clearing up the Variance v. STD, and K v Sharpe ratio topics.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried both ways, and you are correct - Variance approach is the friendlier one.&lt;/P&gt;&lt;P&gt;Regarding QP : I have set up the QP code, but still figuring out ways to get all the outputs together in one go. I also have to validate that QP is getting proper data based on how I have defined the assets in IML. WIP!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 02:06:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945746#M6290</guid>
      <dc:creator>AbhisekGhosh</dc:creator>
      <dc:date>2024-10-01T02:06:08Z</dc:date>
    </item>
    <item>
      <title>Re: Get Tangency Portfolio - Mean Variance Model</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945748#M6291</link>
      <description>I also have interest in searching that topic. &lt;BR /&gt;Good Luck.</description>
      <pubDate>Tue, 01 Oct 2024 02:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Get-Tangency-Portfolio-Mean-Variance-Model/m-p/945748#M6291</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-01T02:42:31Z</dc:date>
    </item>
  </channel>
</rss>

