{"id":5588,"date":"2014-11-17T18:49:43","date_gmt":"2014-11-17T17:49:43","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5588"},"modified":"2014-11-17T18:52:05","modified_gmt":"2014-11-17T17:52:05","slug":"solving-the-nearest-correlation-matrix-problem-using-python","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5588","title":{"rendered":"Solving the nearest correlation matrix problem using Python"},"content":{"rendered":"<p>Given a symmetric matrix such as<\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 65px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/walkingrandomly.com\/wp-content\/ql-cache\/quicklatex.com-0ba648b6b45c6e624aefe7341a911896_l3.png\" height=\"65\" width=\"96\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#108;&#105;&#103;&#104;&#116;&#32;&#92;&#91;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#123;&#99;&#99;&#99;&#125; &#49;&#32;&#38;&#32;&#49;&#32;&#38;&#32;&#48;&#32;&#92;&#92; &#49;&#32;&#38;&#32;&#49;&#32;&#38;&#32;&#49;&#32;&#92;&#92; &#48;&#32;&#38;&#32;&#49;&#32;&#38;&#32;&#49;&#32;&#92;&#101;&#110;&#100;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#92;&#93;&#32;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p>What&#8217;s the nearest <a href=\"http:\/\/en.wikipedia.org\/wiki\/Covariance_matrix\">correlation matrix<\/a>? A <a href=\"http:\/\/eprints.ma.man.ac.uk\/232\/01\/covered\/MIMS_ep2006_70.pdf\">2002 paper by Manchester University&#8217;s Nick Higham<\/a> which answered this question has turned out to be rather popular! At the time of writing, Google tells me that it&#8217;s been cited 394 times.<\/p>\n<p>Last year, Nick <a href=\"http:\/\/nickhigham.wordpress.com\/2013\/02\/13\/the-nearest-correlation-matrix\/\">wrote a blog post<\/a> about the algorithm he used and included some MATLAB code. He also included links to applications of this algorithm and implementations of various NCM algorithms in languages such as MATLAB, R and SAS as well as details of the <a href=\"http:\/\/www.nag.co.uk\/numeric\/CL\/nagdoc_cl24\/html\/G02\/g02aac.html\">superb commercial implementation by The Numerical algorithms group<\/a>.<\/p>\n<p>I noticed that there was no Python implementation of Nick&#8217;s code <a href=\"https:\/\/github.com\/mikecroucher\/nearest_correlation\">so I ported it myself<\/a>.<\/p>\n<p>Here&#8217;s an example IPython session using the module<\/p>\n<pre>In [1]: from nearest_correlation import nearcorr\r\n\r\nIn [2]: import numpy as np\r\n\r\nIn [3]: A = np.array([[2, -1, 0, 0], \r\n   ...:               [-1, 2, -1, 0],\r\n   ...:               [0, -1, 2, -1], \r\n   ...:               [0, 0, -1, 2]])\r\n\r\nIn [4]: X = nearcorr(A)\r\n\r\nIn [5]: X\r\nOut[5]: \r\narray([[ 1.        , -0.8084125 ,  0.1915875 ,  0.10677505],\r\n       [-0.8084125 ,  1.        , -0.65623269,  0.1915875 ],\r\n       [ 0.1915875 , -0.65623269,  1.        , -0.8084125 ],\r\n       [ 0.10677505,  0.1915875 , -0.8084125 ,  1.        ]])<\/pre>\n<p>This module is in the early stages and there is a lot of work to be done. For example, I&#8217;d like to include a lot more examples in the test suite, add support for the commercial routines from NAG and implement other algorithms such as the one by Qi and Sun among other things.<\/p>\n<p>Hopefully, however, it is just good enough to be useful to someone. Help yourself and let me know if there are any problems. Thanks to Vedran Sego for many useful comments and suggestions.<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/mikecroucher\/nearest_correlation\"><span style=\"line-height: 13px;\">github repository for the Python NCM module, nearest_correlation<\/span><\/a><\/li>\n<li><a href=\"http:\/\/nickhigham.wordpress.com\/2013\/02\/13\/the-nearest-correlation-matrix\/\">Nick Higham&#8217;s original MATLAB code<\/a>.<\/li>\n<li><a href=\"http:\/\/www.nag.co.uk\/numeric\/CL\/nagdoc_cl24\/html\/G02\/g02aac.html\">NAG&#8217;s commercial implementation<\/a> &#8211; callable from C, Fortran, MATLAB, Python and more. A superb implementation that is significantly faster and more robust than this one!<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a symmetric matrix such as &nbsp; &nbsp; What&#8217;s the nearest correlation matrix? A 2002 paper by Manchester University&#8217;s Nick Higham which answered this question has turned out to be rather popular! At the time of writing, Google tells me that it&#8217;s been cited 394 times. Last year, Nick wrote a blog post about the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6,73,28,76,7,31],"tags":[],"class_list":["post-5588","post","type-post","status-publish","format-standard","hentry","category-general-math","category-linear-algebra","category-nag-library","category-numerics","category-programming","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1s8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5588"}],"version-history":[{"count":9,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5588\/revisions"}],"predecessor-version":[{"id":5617,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5588\/revisions\/5617"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}