Given a symmetric matrix such as
What’s the nearest correlation matrix? A 2002 paper by Manchester University’s Nick Higham which answered this question has turned out to be rather popular! At the time of writing, Google tells me that it’s been cited 394 times.
Last year, Nick wrote a blog post 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 superb commercial implementation by The Numerical algorithms group.
I noticed that there was no Python implementation of Nick’s code so I ported it myself.
Here’s an example IPython session using the module
In : from nearest_correlation import nearcorr In : import numpy as np In : A = np.array([[2, -1, 0, 0], ...: [-1, 2, -1, 0], ...: [0, -1, 2, -1], ...: [0, 0, -1, 2]]) In : X = nearcorr(A) In : X Out: array([[ 1. , -0.8084125 , 0.1915875 , 0.10677505], [-0.8084125 , 1. , -0.65623269, 0.1915875 ], [ 0.1915875 , -0.65623269, 1. , -0.8084125 ], [ 0.10677505, 0.1915875 , -0.8084125 , 1. ]])
This module is in the early stages and there is a lot of work to be done. For example, I’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.
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.
- github repository for the Python NCM module, nearest_correlation
- Nick Higham’s original MATLAB code.
- NAG’s commercial implementation – callable from C, Fortran, MATLAB, Python and more. A superb implementation that is significantly faster and more robust than this one!
I follow a lot of software developers on twitter so I get to see a lot of opinions and comments about git. Here are a few of my recent favourites
Opinions on git
I find that git is a technology that polarizes people. They either love it or they hate it…often both at the same time.
I didn’t know git would make make programming more addictive. Ups frequency of reward, via miniature accomplishments: commits.
— Alex Holcombe (@ceptional) November 12, 2014
Every time you write, “learn git” and follow this advice with “it’s dead simple!” a kitten cries.
— Anja Boskovic (@damedebugger) November 7, 2014
tries to teach 2 lab groups “git & github” .. actually spends most of the time working on installing/setup. oh Twitter. Just hold me.
— Andrew MacDonald (@polesasunder) November 6, 2014
I find that I learn something new every time I read a different tutorial
http://t.co/0OLz7jeghF walkthrough git from internals to (hopefully) better beginners/intermediate usage. Feedback welcomed!
— mrchlblng (@marchelbling) November 12, 2014
— Jon Reid (@qcoding) November 11, 2014
There’s a new version of the Pro Git Book, and you can read it for free online http://t.co/LaIikATlk1
— Mike Hay (@Hay) November 7, 2014
Git tips and tricks
No matter how long you’ve used git, you can always learn a new trick or two.
duh! Didn’t know about git clean -fdx to clean out gitignored files. I’d been using powershell cmd to del bin/obj & manually del pkg folder
— Julie Lerman (@julielerman) November 6, 2014
alias hadouken="git push origin master -f"
— Chris Oliver (@excid3) November 6, 2014
— Elijah Manor (@elijahmanor) November 5, 2014
RLink is Mathematica’s interface to the R language – a feature that has been extremely popular since its debut in Mathematica version 9. It’s a great package but has one or two issues. For example, RLink makes use of a built in version of R which is currently stuck at the rather old version 2.14. Official support for the use of external versions of R and adding third-party libraries varies by operating system and version of Mathematica. Windows support is great — OS X support, not so much.
Expert Mathematica user Szabolcs Horvát has written the definitive guide on how to get RLink up and running with the latest version of R on all three major operating systems, building on earlier work by Leonid Shifrin and members of the Mathematica Stack Exchange community. Thanks to this work, we can now enjoy any version of R we like with Mathematica!