This post was originally published on September 3rd, 2007. It has been slightly modified from a previous version of the site. N.B.: This code has been tested in Matlab R2009b and R2010a and still works fine.
Kaplan and Knowles, extending the original work of Shadwick and Keating as well as that of Kazemi, Schneeweis, and Gupta, describe a generalized downside risk-adjusted measure Kappa. By design, the Omega and Sortino measures are special cases of the Kappa measure, and, as such, the Kappa function is capable of calculating both easily.
So that this very helpful function can be used by those in the community who are having difficulty with the original works, I am publishing a single-line Kappa function for Matlab.
% D : return series vector
% r : return threshold
% n : Kappa order
function k = kappa(D, r, n)
k = (mean(D) - r) ./ nthroot(mean((D < r) .* (r-D).^n), n);
To calculate the Omega measure, put n=1 and add 1 to the result, i.e. kappa(D, r, 1) + 1.
To calculate the Sortino ratio, put n=2, i.e. kappa(D,r,2).