Start posting on Advancedmcode:
To write posts just get an author account, you will soon receive your password, then you can Log In in the post editor. Or simply contact us at advancedmcode@gmail.com.
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[0]<a[2]){ if (a[1]<a[2]){ // 012 } else { // 021 temp=a[0]; a[1]=a[2]; a[2]=a[1]; a[0]=temp; } } else { // 201 temp=a[0]; a[0]=a[2]; a[2]=a[1]; a[1]=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: 2% [?]
Accelerated Computer Vision in MATLAB
Filed under: Algorithms, Code Optimization, Image processing
Leave a Comment
A recent post on the AccelerEyes’ blog discusses computer vision algorithm acceleration in MATLAB. The key problem in using MATLAB for computer vision is that:
“Matlab and the M language is great for linear algebra where blocks of matrices are the typical access pattern, but not for Computer Vision where algorithms typically operate on patches of imagery.”
In order to get around this problem, a windows function is introduced:
“The command windows signals to Jacket that we’re doing a patched access pattern that can then be optimized on the GPU.”
Anyway, this may be interesting for those involved in computer vision and interested in GPUs!
Popularity: 2% [?]
SEA-MAT: Matlab Tools for Oceanographic Analysis
The complete Oceanographic tools directory:
Time Series Tools
- T_Tide: A full-featured tidal analysis package written in Matlab! By Rich Pawlowicz, Bob Beardsley and Steve Lentz.
- Air-Sea: Compute surface wind stress and heat flux components from buoy and shipboard atmospheric and near-surface oceanographic time series measurements. Developed by Bob Beardlsley and Rick Pawlowicz. This is version 2.0.
- Timeplt: Stacked x-y and vector stick plots with Gregorian time labeling from Rich Signell. See also Timeplt5
- Bobstuff Vector Correlation, Complex Correlation, and other tools from Bob Beardsley (WHOI)
- RPS Stuff Miscellaneous time series tools from Rich Signell, a PPM image reader and writer, a program to read SeaBird CTD .CNV files, and more.
- ADCP i/o package Simple but robust tools to read RDI raw and processed binary data into MATLAB. Tested for a number of different WH, NB, and BB instruments with various firmware versions. From Rich Pawlowicz.
- RDI Workhorse ADCP==>NetCDF: This toolbox is for fixed platform self-contained Workhorse data collection. Developed by Jessica Cote, Marinna Martini, Fran Hotchkiss, and Chuck Denham at the USGS Woods Hole Field Center.
- ADCP Tool Box: This toolbox is a set of documented Matlab scripts that convert RDI data into NetCDF format. From Andree Ramsey.
- Cross Wavelet and Wavelet Coherence – for examining relationships in time frequency space between two time series. From Aslak Grinsted,Arctic Centre, University of Lapland.
- JLAB: Matlab freeware for data analysis by J. M. Lilly. 300+ heavily tested functions including industrial stength wavelet and time series analysis tools.
Numerical Modeling Tools
- OM-Viz:Visualization and Analysis tools for SCRUM, ECOM and POM Ocean Models from Rich Signell.
- HOPS GUI: (No longer active) - http://es.geocities.com/pedrobelchi/Hopsgui/introductionhopsgui.html – Visualization tools for the Harvard Ocean Prediction System (HOPS) by Pedro Velez.
- HISWA-Viz:Visualization and Analysis tools for the HISWA wave model from Jeff list.
- Ocean Processes Numerical Modeling Laboratory (OPNML) Matlab library: Tools for working with the Dartmouth Finite Element Circulation Models.
- RSliCE – a nice GUI for ROMS model visualization, including horizontal z or s level slicing, arbitrary vertical slices along splined paths, velocity vector overlays, and more. Can access netcdf files locally or remotely via OpenDAP.
Mapping Tools
- Mapping tools : Projections, inverse-projections, coastline & elevation databases and more. From – who else? Rich Pawlowicz!
- Map Stuff: Coastline and lon/lon labeling stuff from Rich Signell. Check out this sample image. If you need coastline data, you might want to check out the Coastline Extractor.
- Read Global Topographic Data: Two Matlab functions to extract the data from two global topographic data base, ETOPO2v2 and GEOBEC.
Hydrographic Tools
- EasyKrig2.0 Toolkit: A gui-driven collection of routines to make Kriging of hydrographic and other spatial data a simple process, by Dr. Dezhang Chu.
- OCEANS toolbox: A collection of routines useful for oceanographers, including the UNESCO routines (specific heat capacity, the equation of state, etc.), various sound-speed equations, T/S diagrams, distances on an ellipsoidal earth, and more.
- CSIRO Seawater Toolkit: Tools for computing properties of seawater, including potential density, specific volume anamoly, Brunt-Vaisala frequency, speed of sound, and more.
- The “gsw” software library (Gibbs-sea-water library) below contains computer code that evaluates the TEOS-10 based thermodynamic properties of seawater. The code for evaluating Absolute Salinity is also included in the gsw library.
- Dynamic Normal Modes routine from John Klinck.
- Optimal Multi-Parameter (OMP) Water Mass Mixing Analysis from the University of Hamburg. These m-files facilitate water mass mixing analysis for hydrographic data. The analysis is an inverse method looking for the best linear fit in a n-dimensional parameter space of tracers. A Users Guide is included!
- STAPLOT : A simple and flexible package designed to facilitate graphical analysis of hydrographic or other irregularly spaced data.
- WOCE tools A collection of WOCE water property data utilities from Paul Robbins (WHOI).
- FloatAxis: Plot data with different units (eg. temperature, salinity and density versus depth) on the same axes having the x-axis for the parameters “floating” below. The number of parameters plotted is not limited. From Blair Greenan.
- Temperature-Salinity Plot: The tsplot toolbox builds on the tsdiagram function found in the OCEANS toolbox at the Woods Hole Oceanographic Institution. You can produce TS plots using either a command line function (tsdiagram) or a Graphical User Interface (tsplot). From Blair Greenan.
- RPS Stuff Miscellaneous time series tools from Rich Signell, a PPM image reader and writer, a program to read SeaBird CTD .CNV files, and more.
Data Interface Tools
- MEXCDF: A complete and efficient interface between MATLAB and NetCDF files, implemented through a mex file.
- NetCDF toolbox for Matlab 5. An powerful and elegant syntax for working with NetCDF data from within Matlab 5.
- MEXEPS: An interface between MATLAB and EPIC, NOAA PMEL’s system for management, display, and analysis of oceanographic data.
- CSIRO NetCDF access tools. These tools allow simplified extraction of data from NetCDF files using either an interactive or command line interface.
- MSQUERY enables you to transfer the results of a query in the Microsoft Access 97 database program directly into your Matlab workspace using MS Windows ActiveX technology. The columns headings from the Access query are parsed and assigned as the variable names of the appropriate columns in your Matlab workspace. From Blair Greenan.
Miscellaneous Other Tools
- m_pack is a collection of matlab tools and several utilities related with ROMS visualization and analysis (including SpectrHA) From Martinho Marta Almeida.
- Float tracking software for Matlab 5 from Eurofloat.
- Mooring dynamics and design tools: A MATLAB 5 package to design, analyze and model oceanographic moorings, from Richard Dewey.
- Tidal Ellipse Tools from Zhigang Xu. These convert between u and v amplitude and phase parameters and tidal ellipse parameters (major axis, ellipticity, inclination and phase). There is also a program to estimate vertical profiles of tidal ellipse parameters, given sea-elevation gradients. Postscript documentation is provided.
- Spatial and Geometric Analysis (SaGA) toolkit. Gridding, tesselation, and more.
- EasyKrig2.0 Toolkit: A gui-driven collection of routines to make Kriging of hydrographic and other spatial data a simple process, by Dr. Dezhang Chu.
- EXTCONTOUR Extended contouring capabilities for Matlab 4 & 5. Can handle parametric surfaces, filled contours, publication-quality labelling and more.
- COLORBARF Produces a colorbar for a filled contour plot with the appropriate colors and tick marks. From Blair Greenan.
- Carter corrects single-beam echosounder depths using Carter’s tables. (As you undoubtedly know, most ship’s echosounders assume sound velocity to be 1500m/s, which is only very approximate. Carter’s tables were thus produced to enable a correction to a more realistic depth, using an assumed mean sound speed that depends on latitude and longitude.) From Mike Meredith.
- Schlee makes a ternary diagram for sand, mud and gravel, assuming that mud=(silt+clay).
- Shepard_Ternary_Plot plots sand, silt and clay fraction data on a ternary plot that is marked with the Shepard classification scheme labels .
- Sand/Silt/Clay plotting routine based on a different classification scheme (Folk, 1954)
- MATHEMATICA-based Wavelet tidal analysis software (No longer active) – Tidal analysis tools for non-stationary tidal processes (e.g., barotropic river tides and continental shelf internal tides). Developed by David Jay and Ed Flinchem. Note: although this software isn’t in Matlab, it seemed useful enough to link here anyway!
- Sturges - For some purposes we want to remove the annual cycle in a long data set, particularly for cross spectra with other data. Trivial for monthly data, but less so for weekly or daily data.
Related Web sites
- Mathworks link Exchange: links to User-contributed and Mathworks-contributed m-files.
- Mathworks File Exchange: library of files contributed by users and developers of MATLAB, Simulink and related products.
- Mat2html
- MathTools list of Matlab toolboxes: A surprisingly long list, and most toolboxes are free!
- Matlab Snack Bar from Chuck Denham at USGS.
- MACE is a MATLAB toolbox collects MATLAB .m functions for coastal engineers and researchers. The main purpose of this site is exchange our resources between the people in the coastal research area.
Popularity: 3% [?]
mp3read and mp3write: listen to your favourite music inside Matlab
Mp3 is probably the most common format to download and listen music. In the recent years many websites has offered free mp3 songs and the compact music format has spread out all over the world. Here we report these great tools from Dan Ellis that will allow you to edit your mp3 files inside Matlab.
MP3 reading and writing
These function, mp3read and mp3write, aim to exactly duplicate the operation of wavread and wavwrite for accessing soundfiles, except the soundfiles are in Mpeg-Audio layer 3 (MP3) compressed format. All the hard work is done by external binaries written by others: mp3info to query the format of existing mp3 files, mpg123 to decode mp3 files, and lame to encode audio files. Binaries for these files are widely available (and may be included in this distribution).
These functions were originally developed for access to very large mp3 files (i.e. many hours long), and so avoid creating the entire uncompressed audio stream if possible. mp3read allows you to specify the range of frames you want to read (as a second argument), and mp3read will construct an mpg123 command that skips blocks to decode only the part of the file that is required. This can be much quicker (and require less memory/temporary disk) than decoding the whole file.
mpg123 also provides for “on the fly” downsampling at conversion to mono, which are supported as extra options in mp3read.
For more information, including advice on handling MP4 files, see http://labrosa.ee.columbia.edu/matlab/mp3read.html
Example usage
Here, we read a wav file in, then write it out as an MP3, then read the resulting MP3 back in, and compare it to the original file.
% Read an audio waveform [d,sr] = wavread('piano.wav'); % Save to mp3 (default settings) mp3write(d,sr,'piano.mp3'); % Read it back again [d2,sr] = mp3read('piano.mp3'); % mp3 encoding involves some extra padding at each end; we attempt % to cut it off at the start, but can't do that at the end, because % mp3read doesn't know how long the original was. But we do, so.. % Chop it down to be the same length as the original d2 = d2(1:length(d),:); % What is the SNR (distortion)? ddiff = d - d2; disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']); % Do they look similar? subplot(211) specgram(d(:,1),1024,sr); subplot(212) plot(1:5000,d(10000+(1:5000),1),1:5000,d2(10000+(1:5000))); % Yes, pretty close % % NB: lame followed by mpg123 causes a little attenuation; you % can get a better match by scaling up the read-back waveform: ddiff = d - 1.052*d2; disp(['SNR is ',num2str(10*log10(sum(d(:).^2)/sum(ddiff(:).^2))),' dB']);
SNR is 21.9693 dB
SNR is 24.0399 dB
External binaries
The m files rely on three external binaries, each of which is available for Linux, Mac OS X, or Windows:
mpg123 is a high-performance mp3 decoder. Its home page is http://www.mpg123.de/ .
mp3info is a utility to read technical information on an mp3 file. Its home page is http://www.ibiblio.org/mp3info/ .
lame is an open-source MP3 encoder. Its homepage is http://lame.sourceforge.net/ .
The various authors of these packages are gratefully acknowledged for doing all the hard work to make these Matlab functions possible.
Installation
The two routines, mp3read.m and mp3write.m, will look for their binaries (mpg123 and mp3info for mp3read; lame for mp3write) in the same directory where they are installed. Binaries for different architectures are distinguished by their extension, which is the standard Matlab computer code e.g. “.mac” for Mac PPC OS X, “.glnx86″ for i386-linux. The exception is Windows, where the binaries have the extension “.exe”.
Temporary files will be written to (a) a directory taken from the environment variable TMPDIR (b) /tmp if it exists, or (c) the current directory. This can easily be changed by editing the m files.
% Last updated: $Date: 2007/02/04 04:12:56 $ % Dan Ellis <dpwe@ee.columbia.edu>
Popularity: 3% [?]























































