MATLAB program optimization (option calculation)

Source: Internet
Author: User

Recently, someone asked me what to pay attention to when writing a MATLAB program ...?
Below I will write some
First of all, I didn't pay much attention to the efficiency of the MATLAB program when I started writing it. I just wanted to get the result.
Not for yourself. However, as problems become more and more complex, Matlab computing takes longer and longer time and is sometimes unbearable,
C/C ++ seems to be much more complex than MATLAB, but it is very efficient. Later I came into contact with some program optimization and Algorithm Optimization
The problem is really complicated. MATLAB (kernel) should be written in C, and I feel a lot about C ++ new delet...
Programming has some insights.
How can we make your MATLAB more efficient?
MATLAB is a scripting language. It basically explains how to execute a row in a row. The maximum size of the variable depends on the size of your memory.
I don't know how to use the virtual memory (maybe not)
Mainly from two aspects, program structure and algorithm selection
1. You must initialize the variables before using them.
For...
X [I] = ..
End
If you do not initialize the data, the interpreter does not know the size of X array when x (1) is given.
What Will x become when X (2? Maybe you don't care about this, but here the interpreter will waste a lot
Time.
Therefore, it is generally initialized;
X = zeros (n, m );
2. Use matrix computing as much as possible, and use less loops such as for... While,
Do not use if or I/O operations within the loop.
3. algorithm selection: This is the main aspect to improve efficiency, but it has the highest requirements for you,
Be familiar with MATLAB commands, for example
Temp1 = min (ABS (temp (:, 3 )));
N = find (temp (:, 3) = temp1 );
Find the location with the smallest ABS (temp), you can directly use
[Temp, Index] = min (ABS (temp (:, 3 ));
Returns the location of the smallest element.
4. algorithm usage example
% Find min Result start
W = 0000* 0.8: 0.001: 0000* 1;
W_num = size (W, 2 );
Temp_num = zeros (1, w_num );
For k = 1: w_num
Temp_temperature (K) = blsprice (stk_temp (I) + (M/N) * w (K), strick_price, rate, m_time, vol_temp (j )) * n/(m + n );
% TEMP = [temp; [W, temp_1_abs (w-temp_bls)];
End
% Sort (temp, 3) Sort has a high computational complexity. It is meaningless to use sort only at the maximum here;
[Temp, Index] = min (ABS (w-temp_bls ));
Result (I, j) = W (INDEX );
% Find min Result end
X = f (x) function solution, the program uses a method to traverse a feasible region of X | X-f (x) | the smallest X (I) is the solution of X.
The binary search method can be used to increase the efficiency by N times ....
Result (I, j) = fzero (@ (w) OBJ (W, stk_temp (I), m, n, strick_price, rate, m_time, vol_temp (j )), token );
F = x-f (x) Zero Point solution.

It's easy to say, so let's get started with it ........
Reading books from less to more and then from more to less... now, I am talking about less... how to say more? Of course, the specific problem must be solved ....
MSN: ariszheng@gmail.com
Email: ariszheng@gmail.com
The following is a black shols calculation. (1)... (2)... (3) the computing efficiency is improved. Of course, (1) It is not written by me.
(2), (3) I modified it... the result is okay ..
/// // (1) ////////////////////////////
% N indicates the total share capital, and M indicates the number of warrants
N = 64.553;
M = 12.65;

Stk_price = 45; % ('Stock current price ');
Strick_price = 40; % ('execution price ');
M_time = 5; % ('yearly duration ');
Vol = 0.30; % ('volatility ');
Pchange = 0.05; % ('price variation margin in sensitivity analytics ');
Vchange = 0.05; % ('implied fluctuation variation margin in sensitivity analytics ');

Result = [0 vol-1 * vchange: Vol + 10 * vchange];
For stk_temp = stk_price-5 * pchange: stk_price + 5 * pchange
Result_temp = [stk_temp];
For vol_temp = vol-1 * vchange: Vol + 10 * vchange
[Stk_temp, vol_temp]
Temp = [];
Keys = blsprice (stk_temp, strick_price, 0.0252, m_time, vol_temp );
For W = drawing * 0.8: 0.001: Drawing * 1
Temp_temperature = blsprice (stk_temp + (M/N) * w, strick_price, 0.0252, m_time, vol_temp) * n/(m + n );
Temp = [temp; [W, temp_1_abs (w-temp_bls)];
End
% Sort (temp, 3)
Temp1 = min (ABS (temp (:, 3 )));
N = find (temp (:, 3) = temp1 );
Result_temp = [result_temp temp (n, 1)];
End
Result = [result; result_temp];
End

Result
/// // (2) //////////////////////////
% [Call, put] = blsprice (price, strike, rate, time, volatility, yield)
% N indicates the total share capital, and M indicates the number of warrants
N = 64.553;
M = 12.65;

Stk_price = 45; % ('Stock current price ');
Strick_price = 40; % ('execution price ');
M_time = 5; % ('yearly duration ');
Vol = 0.30; % ('volatility ');
Rate = 0.0252; % annualized, continuously compounded risk-free rate
% Return over the life of the option, expressed as a positive decimal number.
Pchange = 0.05; % ('price variation margin in sensitivity analytics ');
Vchange = 0.05; % ('implied fluctuation variation margin in sensitivity analytics ');
% Analysis Interval
Stk_temp = stk_price-5 * pchange: stk_price + 5 * pchange;
Stk_temp_num = size (stk_temp, 2 );

Vol_temp = vol-1 * vchange: Vol + 10 * vchange;
Vol_temp_num = size (vol_temp, 2 );

% The result is a table of stk_temp_num x vol_temp_num;
Result = zeros (stk_temp_num, vol_temp_num );

For I = 1: stk_temp_num % stk_temp = stk_price-5 * pchange: stk_price + 5 * pchange
% Result_temp = [stk_temp];
For j = 1: vol_temp_num % vol_temp = vol-1 * vchange: Vol + 10 * vchange
% TEMP = [];
[I, j]
Keys = blsprice (stk_temp (I), strick_price, rate, m_time, vol_temp (j ));

% Find min Result start
W = 0000* 0.8: 0.001: 0000* 1;
W_num = size (W, 2 );
Temp_num = zeros (1, w_num );
For k = 1: w_num
Temp_temperature (K) = blsprice (stk_temp (I) + (M/N) * w (K), strick_price, rate, m_time, vol_temp (j )) * n/(m + n );
% TEMP = [temp; [W, temp_1_abs (w-temp_bls)];
End
% Sort (temp, 3) Sort has a high computational complexity. It is meaningless to use sort only at the maximum here;
[Temp, Index] = min (ABS (w-temp_bls ));
Result (I, j) = W (INDEX );
% Find min Result end
End
End
Result

/// // (3) /////////////////////////////////////
% [Call, put] = blsprice (price, strike, rate, time, volatility, yield)
% N indicates the total share capital, and M indicates the number of warrants
N = 64.553;
M = 12.65;

Stk_price = 45; % ('Stock current price ');
Strick_price = 40; % ('execution price ');
M_time = 5; % ('yearly duration ');
Vol = 0.30; % ('volatility ');
Rate = 0.0252; % annualized, continuously compounded risk-free rate
% Return over the life of the option, expressed as a positive decimal number.
Pchange = 0.05; % ('price variation margin in sensitivity analytics ');
Vchange = 0.05; % ('implied fluctuation variation margin in sensitivity analytics ');
% Analysis Interval
Stk_temp = stk_price-5 * pchange: stk_price + 5 * pchange;
Stk_temp_num = size (stk_temp, 2 );

Vol_temp = vol-1 * vchange: Vol + 10 * vchange;
Vol_temp_num = size (vol_temp, 2 );

% The result is a table of stk_temp_num x vol_temp_num;
Result = zeros (stk_temp_num, vol_temp_num );

For I = 1: stk_temp_num % stk_temp = stk_price-5 * pchange: stk_price + 5 * pchange
% Result_temp = [stk_temp];
For j = 1: vol_temp_num % vol_temp = vol-1 * vchange: Vol + 10 * vchange
% TEMP = [];
[I, j]
Keys = blsprice (stk_temp (I), strick_price, rate, m_time, vol_temp (j ));
Result (I, j) = fzero (@ (w) OBJ (W, stk_temp (I), m, n, strick_price, rate, m_time, vol_temp (j )), token );
End
End
Result

/// OBJ
Function f = OBJ (W, stk_temp, M, N, strick_price, rate, m_time, vol_temp)
F = blsprice (stk_temp + (M/N) * w, strick_price, rate, m_time, vol_temp) * n/(m + n)-W;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.