Risk and Asset Allocation
A toolbox for risk and asset allocation from Attilio Meucci that allows for advanced risk and portfolio management.
These routines support the book “Risk and Asset Allocation” Springer Finance, by A. Meucci, see http://www.symmys.com
The routines include many new features:
- - more uni-, multi- and matrix-variate distributions
- - more copulas
- - more graphical representations
- - more analyses in terms of the location-dispersion ellipsoid.
- - best replication / best factor selection
- - FFT-based projection of a distribution to the investment horizon
- - caveats about delta/gamma pricing
- - step-by-step evaluation of a generic estimator
- - non-parametric estimators
- - multivariate elliptical maximum-likelihood estimators
- - shrinkage estimators: Stein and Ledoit-Wolf, Bayesian classical equivalent
- - robust estimators: Hubert M, high-breakdown minimum volume ellipsoid
- - missing-data techniques: EM algorithm, uneven-series conditional estimation
- - stochastic dominance
- - extreme value theory for VaR
- - Cornish-Fisher approximation for VaR
- - kernel-based contribution to VaR and expected shortfall from different risk-factors
- - mean-variance analysis and pitfalls (different horizons, compounded vs. linear returns, etc…)
- - Bayesian estimation (multivariate analytical, Monte Carlo Markov Chains, priors for correlation matrices)
- - estimation risk evaluation: opportunity cost of estimation-based allocations
- - Black Litterman allocation
- - robust optimization (calls SeDuMi to perform cone programming)
- - robust Bayesian allocation
- - more…
In addition to these MATLAB routines, at www.symmys.com the reader can find other freely downloadable complementary materials:
- - the “Technical Appendices”, a booklet with the proofs of the results presented in the books and used in the routines
- - the “Slides”, a set of presentations that walk the reader through the whole book
- - the “Errata”, a few typos in the first two reprints of the book
- - the “Sample”, an excerpt of the book.
Any feedback on the above materials is highly appreciated: please refer to www.symmys.com to contact the author.
Popularity: 1% [?]
The Fastest way to sort 3 values
Yesterday I was surfing the web looking for some optimized C++ code to sort 3 numbers. Here is an excerpt from the first page returned by Google.
The problem of sorting 3 numbers can be described as follow
| a < b | a < c | b < c | a < b < c here, sorted!!! |
| a < c <= b here, sorted | |||
| c <= a < b here, sorted!!! | |||
| b < c | a < c | b <= a < c here, sorted!!! | |
| b < c <= a here, sorted!!! | |||
| c <= b <= a here, sorted!!! | |||
A binary tree is used to reach the solution.
This leads to the following code:
! ------------------------------------------------------- ! This program reads in three INTEGERs and displays them ! in ascending order. ! ------------------------------------------------------- PROGRAM Order IMPLICIT NONE INTEGER :: a, b, c READ(*,*) a, b, c IF (a < b) THEN ! a < b here IF (a < c) THEN ! a < c : a the smallest IF (b < c) THEN ! b < c : a < b < c WRITE(*,*) a, b, c ELSE ! c <= b : a < c <= b WRITE(*,*) a, c, b END IF ELSE ! a >= c : c <= a < b WRITE(*,*) c, a, b END IF ELSE ! b <= a here IF (b < c) THEN ! b < c : b the smallest IF (a < c) THEN ! a < c : b <= a < c WRITE(*,*) b, a, c ELSE ! a >= c : b < c <= a WRITE(*,*) b, c, a END IF ELSE ! c <= b : c <= b <= a WRITE(*,*) c, b, a END IF END IF END PROGRAM Order
Another guy suggested that this solution is too complicated and proposed:
int a, b, c; #define swap(x, y) do {int tmp; tmp = x; x = y; y = tmp; } while (0) if (b < a) swap(b, a); if (c < b) swap(c, b); if (b < a) swap(b, a);
This can also be easly extended to 4 numbers :
if (b < a) swap(b, a); if (c < b) swap(c, b); if (d < c) swap(d, c); if (b < a) swap(b, a); if (c < b) swap(c, b); if (b < a) swap(b, a);
This program is nothing less than bubble sort. It can be generated with the following program:
main() for (i = 0; i < n - 1; i++) for (j = 0; j < n - 1 - i; j++) printf("if (%c < %c) swap(%c, %c);\n", 'a' + j + 1, 'a' + j, 'a' + j + 1, 'a' + j); }
This code may be easier to write but it is slower, I have compared the easy solution (with swaps form bubble sort) with the more complicated one that uses a binary tree.
Sorting 10′000′000 3 numbers buckets took 187ms with the bubble sort, 158mswith binary tree. I coded a in place version of a 3 numbers sort function.
Here is the fastest code I know to sort in place 3 numbers:
void sort3(double* a){ double temp; if (a[0]<a[1]){ if (a[1]<a[2]){ //012 } else{ if (a[0]<a[2]){ //021 temp=a[1]; a[1]=a[2]; a[2]=temp; } else{ //201 temp=a[0]; a[0]=a[2]; a[2]=temp; temp=a[1]; a[1]=a[2]; a[2]=temp; } } } else{ if (a[0]<a[2]){ //102 temp=a[0]; a[0]=a[1]; a[1]=temp; } else{ if (a[1]<a[2]){ //120 temp=a[0]; a[0]=a[1]; a[1]=a[2]; a[2]=temp; } else{ //210 temp=a[0]; a[0]=a[2]; a[2]=temp; } } } }
Notice that there is no swap macros. Swaps are optimized avoiding to copy the temp value when is not necessary. So now I have to propose you some interesting challenge.
Can this be called the “QuickestSort algorithm” ?
Can you find or code something faster? Let me know.
And can you figure out what is the fastest way to sort 4 numbers?
keep in touch with blog and you will see how.
Popularity: 1% [?]
GLTree Matlab Example: Nearest Neighbor Filter Function
Filed under: Algorithms, Computational Geometry, Optimization
KNNSearch2D query a GL-tree for k nearest neighbor(kNN)
SYNTAX
[cutdist,ndel,idel]=KNNFilter2D(p,ptrtree,’r',r);% radius mode
[cutdist,ndel,idel]=KNNFilter2D(p,ptrtree,’f',f);% consolidator mode
INPUT PARAMETERS
- p: [2xN] double array coordinates of reference points
- ptrtree: a pointer to the previously constructed GLtree.Warning if the pointer is incorrect it will cause a crash, there is no way to check this in the mex routine, you have to check it in your script.
- ‘r’ or ‘f’: string. Specify the RADIUS mode or the CONSOLIDATOR mode.
- r or f: double parameter for the mode.
OUTPUT PARAMETERS
- cutdist: in the dataset no couple of points will be closer than cutdist. In the radius mode cutdist==radius.
- ndel: number of points removed from the tree
- iddel: ids of points removed from the tree
GENERAL INFORMATIONS
- -RADIUS mode: if 2 points are closer than r only one will survive.
- -CONSOLIDATOR mode: the nearest neighbour graph is computed and the mean distance between points is evaluated. The cutdistance is set to meandist/f.
- -points removed form the tree are not physically deleted (memory deaollocated), they are just skipped during search.
For question, suggestion, bug reports
giaccariluigi@msn.com
Author: Luigi Giaccari
N=300;%reference points Cuboid=[.2 ,.5 ,.2 ,.5 ]'; p=rand(N,2);%reference points fprintf('RANDOM POINTS GENERATED\n\n') fprintf('BUILDING THE DATA STRUCTURE:\n') tic ptrtree=BuildGLTree2D(p'); fprintf('\tGLTree built in %4.4f s\n\treturned pointer %4.0f:\n\n',toc,ptrtree); fprintf('START FILTER:\n') tic [idc]=CuboidSearch2D(p',Cuboid,ptrtree); fprintf('\t %4.0f Points found in range\n\n',length(idc)); fprintf('DELETING THE TREE\n\n') DeleteGLTree2D(ptrtree); fprintf('TEST SUCCESFULLY COMPLETED !!!\n\n') %plot the NNG figure(1) title('Cuboid Search','fontsize',14); axis equal hold on x=[Cuboid(1),Cuboid(1),Cuboid(2),Cuboid(2),Cuboid(1)]; y=[Cuboid(3),Cuboid(4),Cuboid(4),Cuboid(3),Cuboid(3)]; h1=plot(p(:,1),p(:,2),'g.'); h2=plot(x,y,'b-'); h3=plot(p(idc,1),p(idc,2),'r.'); legend([h1,h2,h3],'Reference points','Cuboid','Inside points');
RESULTS
Points inside the cut radius are found

And deleted..
Go to main post
Popularity: 1% [?]




















































