Quantzone

Matlab Finance Code

Pages: Prev 1 2 Next

FBD – “Find the Best Distribution” tool

With this GUI you can find the best distribution that fits your data.

Matrix Decomposition

Matrix decomposition using, e.g. the Cholesky decomposition requires the correlation matrix to be positive definite. That is, the eigenvalues must all be positive. In finance, this is rarely the case, and one often observes negative eigenvalues, or zero eigenvalues. These two functions do essentially the same thing. One adjusts only the <= 0 eigenvalues, while the other adjusts those eigenvalues, but then also increases the other non-negative eigenvalues to compensate for the higher ‘weight’ given to the smaller eigenvalues.

errorbarjitter

This command plots the mean (or median) ± SD of one or more samples alongside the raw data. The raw data are “jittered”. In data arrays, samples are in rows, categories are in columns. There can be differences in sample sizes between categories. Several parameters of the plot can be altered at the command line, including plotting mean to the left or right of the raw data and plotting SD with or without caps. Categories can be sorted in ascending order by mean (or median). Category names are provided in separate cell array (colheaders). “data” and “colheaders” are the default names for data imported from an Excel file.

Geometric Processing Package

Geometric Processing Package This package is on basic geometric processing, including common used algebraic/topological/graphical functions. Detailed documentation is available. It is planned to add more functions. Your suggestions are highly appreciated.

Particle Swarm Optimization (PSO)

Pattern Recognition Toolbox

Least squares spline modeling using shape primitives

If you could only download one curve fitting tool to your laptop on a desert island, this should be it.

For many years I have recommended that people use least squares splines for their curve fits, with a caveat. Splines offer tremendous flexibility to build a curve in any shape or form. They can nicely fit almost any set of data you will throw at them. This same flexibility is their downfall at times too. Like polynomial models, splines can be too flexible if you are not careful. The trick is to bring your knowledge of the system under study to the problem.

As a scientist, engineer, data analyst, etc., you often have knowledge of a process that you wish to model. Sometimes that knowledge comes from physical principles, sometimes it arises from experience, and sometimes the knowledge just comes from looking at a plot of the data. Regardless of the source, we often want to build in this prior knowledge of a process into our modeling efforts. This is perhaps the biggest reason why nonlinear regression tools are used, and I’ll argue, the worst reason. If you are fitting a sigmoid function to your data only because it happens to be monotone and your data appear to have that property, then you have made the wrong choice of modeling tool. (If you are fitting a sigmoid because this is known to be the proper model for your process, then go ahead and fit the sigmoid.)

I’ll argue the proper tool when you merely need a monotonic curve fit is a least squares spline, but a spline that is properly constrained to have the fundamental shape you know to be there. This is a very Bayesian approach to modeling, and a very useful one in my experience.

The SLM tools provided here give you an easy to use interface to build an infinite number of curve types from data. SLM stands for Shape Language Modeling. The idea is to provide a prescription for a curve fit using a set of shape primitives. If your curve is monotone, then build that information into the model, so you can estimate the monotone curve that best fits your data. What you will find is that once you employ the proper set of constraints, you will wonder why you ever used nonlinear regression in the past!!!

For example, the screenshot for this file was generated for the following data:

x = (sort(rand(1,100)) – 0.5)*pi; y = sin(x).^5 + randn(size(x))/10;

slm = slmengine(x,y,’plot’,'on’,'knots’,10,’increasing’,'on’, …
‘leftslope’,0,’rightslope’,0)

slm =

form: ‘slm’
degree: 3            knots: [10x1 double]
coef: [10x2 double]     prescription: [1x1 struct]
x: [100x1 double]                y: [100x1 double]

You can evaluate the spline or its derivatives using slmeval.

slmeval(1.3,slm)

ans =

0.79491

You plot these splines using plotslm.

plotslm(slm)

The plotslm function is nice because it is a simple gui, allowing you to plot the curve, residuals, its derivatives or the integral. You can also evaluate various parameters of the spline, such as the maximum function value over an interval, the minimum or maximum slope, etc.

slmpar(slm,’maxslope’)

ans =

1.5481

You provide all this information to slmengine using a property/value pair interface. slmset mediates this interaction, so you can use it to create the set of properties that will be used. The default set of properties and their values are given by slmset. Everything about the shape, slopes, curvature, values, etc., about your function can be controlled by a simple command. SLMENGINE also offers the ability to generate splines of various orders, as well as free knot splines.

For a complete set of examples of the SLM tools in action, see the included published tutorial with this submission. There is also a small treatise included on the concept of Shape Language Modeling for curve fitting.

The SLM toolkit will be considerably improved at some time in the future. I will add a graphical interface. As well, if I have missed any natural shape primitives, please let me know. While I have tried to be very inclusive, surely there is something I’ve missed. If I can add your favorite to the list above I will try to do so.

Finally, the SLM tools require the optimization toolbox to solve the various estimation problems.

Fractal Stock Price Generator

?Download download.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fractal stock price simulator
% M. Hanchak, 18OCT10
% Reference:
% Benoit B. Mandelbrot, A Multifractal Walk down Wall Street,
%    Scientific American, February 1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
% User inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
low_price = 15;
high_price = 55;
start_time = 0;
end_time = 10;
multiplier = 2/3;
num_dimensions = 7;
% setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y = [low_price high_price];
x = [start_time end_time];
tol = (high_price-low_price) * multiplier;
hold all
 
% Loop through number of fractal levels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:num_dimensions
    N = 3^i+1; % keep subdividing by three
    xn = linspace(x(1),x(end),N); % make new x vector
    y = interp1(x,y,xn); % interpolate values
 
    % add/subtract at key points to create fractal %%%%%%%%%%%%%%%%%%%%%%%%
    for j = 2:N-1
        if mod(j-1,3) ~= 0 % don't affect previous points.
            % add to existing values - alternate sign and reduce magnitude
            y(j) = y(j) + tol/exp(i/2)*(-1)^(j); 
        end
    end
 
    % plotting %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    x = xn;
    plot(x,y);drawnow;pause(0.2)
    axis([start_time,end_time,low_price,high_price])
end

cellstrfind

Cell find strings, StrCell={ ’000300.SH’ ’000805.SH’ ’000806.SH’ ’000905.SH’ ‘H11002.SH’}
Pattern=’000300.SH’ ,Index=1;

?Download download.txt
1
2
3
4
5
function Index=cellstrfind(StrCell,Pattern)
%code by ariszheng.com
%2012-4-11
Idx=strcmp(Pattern,StrCell);
Index=find(Idx==1);

.

Code for Politis and White’s (2004) automatic block-length selection procedure.

This Matlab m-file (saved as a “txt” file) returns an estimate of the optimal block size for bootstrap methods (stationary or circular) of estimating the asymptotic variance of the mean of dependent data, given a sample from a univariate time series.R code for this procedure, written by Jeff Racine and Chris Parmeter is available here, and a PDF help file is here. Alternatively, if you install R and the “np” package you will have access to the function “b.star”.

Pages: Prev 1 2 Next
keep looking »
  • About

    Matlab Finance Code Collector

  • Tag Cloud

  • Recent Comments

  • Meta