Some often used Functions in Computational Finance
Here are some oft-needed functions for Computational Finance. Most are obvious, but still it's good to know what all is available. For a given matrix x of size, say, N X k:
>> length(x)
% gives max(N, k)
>> [n, p] = size(x)
% gives n = N, and p = k
>> n = size(x, 1)
% gives n = N
>> p = size(x, 2)
% gives p = k
i.e. size(x, dimension) returns the length of a particular dimension of the matrix - number of rows (dimension = 1) or number of columns (dimension = 2).
>> sum(x)
% gives the output as a row vector containing sum of each column
Functions min(x), max(x), mean(x), median(x), std(x), var(x), skewness(x), kurtosis(x) all work similarly and return a row vector containing respectively, the minimum, maximum, mean, median, standard deviation, variance, skewness and kurtosis of each column of the matrix x. Help yourself and see help on each. It'll be time well spent, as you ARE going to need them often.
>> log(x)
% returns the natural logarithm of EACH element in x
Functions sqrt(x), log10(x) (base 10 logarithm), log2(x) (base 2 logarithm) and exp(x) work similarly.
>> cov(x)
% returns the covariance matrix with size k X k. If x is a vector the output is same % as var(x)
Function corrcoef(x) works similary (returning correlation coefficients instead of covariances), only that it has several extensions. See help corrcoef for details.
>> sort(x)
% sorts each row of the matrix separately in increasing order
>> sortrows(x, col)
% sorts the matrix as a group w.r.t to the column given by col. equivalent to 'sort
% by-column ...' in Excel
>> unique(x)
% sorts all the elements of a matrix in ascending order, and returns the unique
% entries as a column
There are other "set-theoretic" functions (e.g. union) which work like unique does, but they aren't probably as useful as unique for the purpose at hand. See help unique for examples.
>> diag(x)
% returns vector of diagonal elements if x is a matrix,
% else if x is a vector, it returns a square matrix with elements of the vector as
% diagonal entries and zero elsewhere
Some other often used Functions
1. linspace(x1, x2, N): returns a row vector starting with x1, ending with x2 and having N elements in between, with the common difference between any two elements given by (x2 - x1) / (N - 1)
2. Another use of the colon (:) operator: similar to linspace. used as [x1 : d : x2], where d is the common difference.
3. repmat(x, m, n): stacks copies of a matrix together. m represents the number of copies 'below' (rows), and n represents the number of copies 'side-by-side' (columns). So repmat(x, 3, 2) is equivalent to [x x; x x; x x]
4. reshape(x, m, n): returns a matrix taking elements (column-wise) from x but with changed number of rows (m) and columns (n). product of number of rows and columns of x must be same as the product m*n. The most obvious use of this is to convert a matrix into a row or a column vector, with columns (row) one below (after) the other
These are the main ones I can think of right now. Will add more as I remember them. If any of you stumble across anything useful not included here, please do share.
No comments:
Post a Comment