MATLAB Examples

## Fuzzy portfolio optimization models (with and without maximization of result reliability)

We propose two fuzzy portfolio optimization models based on the Markowitz Mean-Variance approach. The first model involves trapezoidal fuzzy numbers to extent statistical data, which are insufficient to describe securities, the model returns fuzzy numbers of portfolio expected return and variance. In this model, we propose a method of results reliability estimation. The second model optimize not only solve the problem of the first model but also improves indicators of reliability. We provide a numerical example to illustrate work of proposed methods, also to compare both methods with each other and with classical Mean-Variance method.

```n=7; % Setting of constraints parametres for optimization A = [1.000 1.000 1.000 1.000 1.000 1.000 1.000]; % x1 b = 1; lb=zeros(1,n); ```

## Generation of initial points for optimization

```%(initial point is required for fmincon, patternsearch, fminimax and other methods presented in optimization toolbox) % Creation of arrays for coordinates of possible initial points. One array(1,n)(where n - number of securities in the portfolio) contins all values of one coordinate of initial points. d=0; for i1=0:0.1:1 for i2=0:0.1:1 for i3=0:0.1:1 for i4=0:0.1:1 for i5=0:0.1:1 for i6=0:0.1:1 for i7=0:0.1:1 if i1+i2+i3+i4+i5+i6+i7==1 d=d+1; k_1(d) = i1; k_2(d) = i2; k_3(d) = i3; k_4(d) = i4; k_5(d) = i5; k_6(d) = i6; k_7(d) = i7; end end end end end end end end % Selection of ten random initial points s1=randi([1 length(k_1)],1,1); s2=randi([1 length(k_1)],1,1); s3=randi([1 length(k_1)],1,1); s4=randi([1 length(k_1)],1,1); s5=randi([1 length(k_1)],1,1); s6=randi([1 length(k_1)],1,1); s7=randi([1 length(k_1)],1,1); s8=randi([1 length(k_1)],1,1); s9=randi([1 length(k_1)],1,1); s10=randi([1 length(k_1)],1,1); % Representation of initial points in the form required by % optimization toolbox m_1 = [k_1(s1) k_2(s1) k_3(s1) k_4(s1) k_5(s1) k_6(s1) k_7(s1)]; m_2 = [k_1(s2) k_2(s2) k_3(s2) k_4(s2) k_5(s2) k_6(s2) k_7(s2)]; m_3 = [k_1(s3) k_2(s3) k_3(s3) k_4(s3) k_5(s3) k_6(s3) k_7(s3)]; m_4 = [k_1(s4) k_2(s4) k_3(s4) k_4(s4) k_5(s4) k_6(s4) k_7(s4)]; m_5 = [k_1(s5) k_2(s5) k_3(s5) k_4(s5) k_5(s5) k_6(s5) k_7(s5)]; m_6 = [k_1(s6) k_2(s6) k_3(s6) k_4(s6) k_5(s6) k_6(s6) k_7(s6)]; m_7 = [k_1(s7) k_2(s7) k_3(s7) k_4(s7) k_5(s7) k_6(s7) k_7(s7)]; m_8 = [k_1(s8) k_2(s8) k_3(s8) k_4(s8) k_5(s8) k_6(s8) k_7(s8)]; m_9 = [k_1(s9) k_2(s9) k_3(s9) k_4(s9) k_5(s9) k_6(s9) k_7(s9)]; m_10 = [k_1(s10) k_2(s10) k_3(s10) k_4(s10) k_5(s10) k_6(s10) k_7(s10)]; ```

## Solving for the first fuzzy optimization problem

```%(Results may be better if you launch each section, which requires an initial % point, separately, to generate different initial points for each one). ```

## fmincon method

``` [x1,fval1] = fmincon(@obj_fun_first,m_1,[],[],A,b,lb); [x2,fval2] = fmincon(@obj_fun_first,m_2,[],[],A,b,lb); [x3,fval3] = fmincon(@obj_fun_first,m_3,[],[],A,b,lb); [x4,fval4] = fmincon(@obj_fun_first,m_4,[],[],A,b,lb); [x5,fval5] = fmincon(@obj_fun_first,m_5,[],[],A,b,lb); [x6,fval6] = fmincon(@obj_fun_first,m_6,[],[],A,b,lb); [x7,fval7] = fmincon(@obj_fun_first,m_7,[],[],A,b,lb); [x8,fval8] = fmincon(@obj_fun_first,m_8,[],[],A,b,lb); [x9,fval9] = fmincon(@obj_fun_first,m_9,[],[],A,b,lb); [x10,fval10] = fmincon(@obj_fun_first,m_10,[],[],A,b,lb); Fval = [fval1 fval2 fval3 fval4 fval5 fval6 fval7 fval8 fval9 fval10]; X_p = x1; X_p(2,:) = x2; X_p(3,:) = x3; X_p(4,:) = x4; X_p(5,:) = x5; X_p(6,:) = x6; X_p(7,:) = x7; X_p(8,:) = x8; X_p(9,:) = x9; X_p(10,:) = x10; % search of the best solution (minimum) from the results of optimization Fval_min = Fval(1); for i=1:length(Fval) if Fval(i) < Fval_min Fval_min=Fval(i); Imin=i; end; end; % result output fprintf('fmincon result for the first fuzzy optimization problem:') fprintf('Fval:') -Fval(Imin) fprintf('k:') X_p(Imin,:) fprintf('E_Er, V_Er, E_V, V_V:') f = obj_fun_val(X_p(Imin,:)) ```
```Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the default value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance. fmincon result for the first fuzzy optimization problem:Fval: ans = 0.2592 k: ans = 0.0000 0.0875 0.0000 0.5521 0.0000 0.0000 0.3605 E_Er, V_Er, E_V, V_V: f = 0.0014 0.0689 0.0056 0.0049 ```

## patternsearch method

``` [x1,fval1] = patternsearch(@obj_fun_first,m_1,[],[],A,b,lb); [x2,fval2] = patternsearch(@obj_fun_first,m_2,[],[],A,b,lb); [x3,fval3] = patternsearch(@obj_fun_first,m_3,[],[],A,b,lb); [x4,fval4] = patternsearch(@obj_fun_first,m_4,[],[],A,b,lb); [x5,fval5] = patternsearch(@obj_fun_first,m_5,[],[],A,b,lb); [x6,fval6] = patternsearch(@obj_fun_first,m_6,[],[],A,b,lb); [x7,fval7] = patternsearch(@obj_fun_first,m_7,[],[],A,b,lb); [x8,fval8] = patternsearch(@obj_fun_first,m_8,[],[],A,b,lb); [x9,fval9] = patternsearch(@obj_fun_first,m_9,[],[],A,b,lb); [x10,fval10] = patternsearch(@obj_fun_first,m_10,[],[],A,b,lb); Fval = [fval1 fval2 fval3 fval4 fval5 fval6 fval7 fval8 fval9 fval10]; X_p = x1; X_p(2,:) = x2; X_p(3,:) = x3; X_p(4,:) = x4; X_p(5,:) = x5; X_p(6,:) = x6; X_p(7,:) = x7; X_p(8,:) = x8; X_p(9,:) = x9; X_p(10,:) = x10; % search of the best solution (minimum) from the results of optimization Fval_min = Fval(1); for i=1:length(Fval) if Fval(i) < Fval_min Fval_min=Fval(i); Imin=i; end; end; % result output fprintf('patternsearch result for the first fuzzy optimization problem:') fprintf('Fval:') -Fval(Imin) fprintf('k:') X_p(Imin,:) fprintf('E_Er, V_Er, E_V, V_V:') f = obj_fun_val(X_p(Imin,:)) ```
```Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. Optimization terminated: mesh size less than options.TolMesh. patternsearch result for the first fuzzy optimization problem:Fval: ans = 0.2546 k: ans = 0 0.1031 0.0000 0.5969 0 0 0.3000 E_Er, V_Er, E_V, V_V: f = 0.0013 0.0664 0.0052 0.0046 ```

## genetic algorithm

``` [x,fval] = ga(@obj_fun_first,n,[],[],A,b,lb); % result output fprintf('genetic algorithm result for the first fuzzy optimization problem:') fprintf('Fval:') -fval fprintf('k:') x fprintf('E_Er, V_Er, E_V, V_V:') f = obj_fun_val(x) ```
```Optimization terminated: average change in the fitness value less than options.TolFun. genetic algorithm result for the first fuzzy optimization problem:Fval: ans = 0.2384 k: x = 0.0301 0.0640 0.0188 0.4477 0.0551 0.0598 0.3254 E_Er, V_Er, E_V, V_V: f = 0.0012 0.0721 0.0052 0.0054 ```

## fminimax method

``` [x1,fval1] = fminimax(@obj_fun_second,m_1,[],[],A,b,lb); [x2,fval2] = fminimax(@obj_fun_second,m_2,[],[],A,b,lb); [x3,fval3] = fminimax(@obj_fun_second,m_3,[],[],A,b,lb); [x4,fval4] = fminimax(@obj_fun_second,m_4,[],[],A,b,lb); [x5,fval5] = fminimax(@obj_fun_second,m_5,[],[],A,b,lb); [x6,fval6] = fminimax(@obj_fun_second,m_6,[],[],A,b,lb); [x7,fval7] = fminimax(@obj_fun_second,m_7,[],[],A,b,lb); [x8,fval8] = fminimax(@obj_fun_second,m_8,[],[],A,b,lb); [x9,fval9] = fminimax(@obj_fun_second,m_9,[],[],A,b,lb); [x10,fval10] = fminimax(@obj_fun_second,m_10,[],[],A,b,lb); Fval = [fval1 fval2 fval3 fval4 fval5 fval6 fval7 fval8 fval9 fval10]; fval_sum = [sum(fval1) sum(fval2) sum(fval3) sum(fval4) sum(fval5) sum(fval6) sum(fval7) sum(fval8) sum(fval9) sum(fval10)]; X_p = x1; X_p(2,:) = x2; X_p(3,:) = x3; X_p(4,:) = x4; X_p(5,:) = x5; X_p(6,:) = x6; X_p(7,:) = x7; X_p(8,:) = x8; X_p(9,:) = x9; X_p(10,:) = x10; % search of the best solution (minimum) from the results of optimization fval_sum_min = fval_sum(1); for i=1:length(fval_sum) if fval_sum(i) < fval_sum_min fval_sum_min=fval_sum(i); Imin=i; end; end; % result output fprintf('fminimax result for the second fuzzy optimization problem:') fprintf('Fval:') -Fval(Imin) fprintf('k:') X_p(Imin,:) fprintf('E_Er, V_Er, E_V, V_V:') f = obj_fun_val(X_p(Imin,:)) ```
```Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. Local minimum possible. Constraints satisfied. fminimax stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance. fminimax result for the second fuzzy optimization problem:Fval: ans = -0.0484 k: ans = 0.0000 0 0.4309 0.4314 0.1377 -0.0000 0 E_Er, V_Er, E_V, V_V: f = 0.0003 0.0489 0.0051 0.0027 ```

## genetic algorithm

``` [x, fval, E_V, V_V] = gamultiobj(@obj_fun_second,n,[],[],A,b,lb); % result output fprintf('genetic algorithm result for the second fuzzy optimization problem:') fprintf('Fval:') -fval fprintf('k:') x fprintf('E_Er, V_Er, E_V, V_V:') f = obj_fun_val(x) ```
```Optimization terminated: average change in the spread of Pareto solutions less than options.TolFun. genetic algorithm result for the second fuzzy optimization problem:Fval: ans = 0.1344 -0.0537 -0.0031 0.1363 -0.0538 -0.0031 0.2335 -0.0657 -0.0045 0.1796 -0.0575 -0.0035 0.1285 -0.0537 -0.0031 0.1986 -0.0596 -0.0037 0.1073 -0.0535 -0.0031 0.1402 -0.0541 -0.0031 0.1504 -0.0546 -0.0032 0.1885 -0.0584 -0.0036 0.1729 -0.0569 -0.0034 0.1578 -0.0554 -0.0032 0.1189 -0.0536 -0.0031 0.1430 -0.0541 -0.0031 0.1618 -0.0557 -0.0033 0.1083 -0.0535 -0.0031 0.1307 -0.0537 -0.0031 0.2181 -0.0623 -0.0040 0.2044 -0.0605 -0.0038 0.1339 -0.0537 -0.0031 0.1449 -0.0542 -0.0031 0.2352 -0.0666 -0.0046 0.1314 -0.0537 -0.0031 0.1597 -0.0555 -0.0032 0.2217 -0.0630 -0.0041 0.1701 -0.0567 -0.0034 0.2015 -0.0601 -0.0038 0.1382 -0.0540 -0.0031 0.1961 -0.0594 -0.0037 0.2164 -0.0621 -0.0040 0.1743 -0.0570 -0.0034 0.1567 -0.0552 -0.0032 0.2379 -0.0684 -0.0049 0.2155 -0.0619 -0.0040 0.2382 -0.0689 -0.0049 0.1543 -0.0550 -0.0032 0.1904 -0.0587 -0.0036 0.2130 -0.0615 -0.0039 0.2206 -0.0627 -0.0041 0.2000 -0.0599 -0.0038 0.1811 -0.0577 -0.0035 0.1708 -0.0567 -0.0034 0.2301 -0.0647 -0.0044 0.1686 -0.0564 -0.0034 0.1449 -0.0544 -0.0031 0.1488 -0.0546 -0.0032 0.2097 -0.0611 -0.0039 0.1159 -0.0536 -0.0031 0.1396 -0.0540 -0.0031 0.2232 -0.0632 -0.0042 0.1630 -0.0559 -0.0033 0.1834 -0.0579 -0.0035 0.1854 -0.0581 -0.0035 0.2320 -0.0652 -0.0044 0.2317 -0.0651 -0.0044 0.1784 -0.0574 -0.0035 0.1926 -0.0591 -0.0037 0.1523 -0.0548 -0.0032 0.1175 -0.0536 -0.0031 0.2346 -0.0663 -0.0046 0.1206 -0.0536 -0.0031 0.1669 -0.0562 -0.0033 0.2119 -0.0614 -0.0039 0.1650 -0.0560 -0.0033 0.1367 -0.0540 -0.0031 0.2063 -0.0606 -0.0038 0.2382 -0.0689 -0.0049 0.1250 -0.0536 -0.0031 0.1471 -0.0545 -0.0031 0.2365 -0.0670 -0.0047 k: x = 0.0007 0.0100 0.1828 0.6856 0.0346 0.0441 0.0413 0.0007 0.0100 0.1757 0.6901 0.0343 0.0446 0.0436 0.0006 0.0126 0.0572 0.5642 0.0384 0.0511 0.2761 0.0001 0.0108 0.0719 0.7202 0.0343 0.0470 0.1155 0.0008 0.0099 0.2286 0.6403 0.0350 0.0442 0.0402 0.0003 0.0112 0.0657 0.6827 0.0346 0.0482 0.1573 0.0013 0.0097 0.3550 0.5156 0.0364 0.0442 0.0370 0.0005 0.0102 0.1590 0.7030 0.0337 0.0446 0.0480 0.0001 0.0106 0.0939 0.7596 0.0323 0.0459 0.0571 0.0003 0.0108 0.0720 0.7011 0.0337 0.0472 0.1348 0.0003 0.0107 0.0780 0.7278 0.0337 0.0472 0.1021 0.0002 0.0108 0.0946 0.7424 0.0328 0.0463 0.0728 0.0008 0.0097 0.2925 0.5773 0.0355 0.0442 0.0391 0.0005 0.0103 0.1358 0.7251 0.0334 0.0452 0.0491 0.0002 0.0106 0.0760 0.7553 0.0329 0.0465 0.0784 0.0013 0.0097 0.3499 0.5207 0.0364 0.0442 0.0371 0.0008 0.0099 0.2124 0.6564 0.0348 0.0442 0.0405 0.0005 0.0120 0.0646 0.6273 0.0360 0.0493 0.2103 0.0005 0.0108 0.0807 0.6499 0.0347 0.0481 0.1751 0.0007 0.0100 0.1867 0.6818 0.0346 0.0441 0.0412 0.0003 0.0104 0.1249 0.7345 0.0329 0.0453 0.0511 0.0007 0.0129 0.0587 0.5439 0.0389 0.0517 0.2936 0.0008 0.0100 0.2074 0.6613 0.0347 0.0441 0.0408 0.0003 0.0106 0.0810 0.7542 0.0329 0.0464 0.0747 0.0006 0.0118 0.0659 0.6116 0.0367 0.0499 0.2236 0.0004 0.0109 0.0911 0.7195 0.0334 0.0466 0.0981 0.0004 0.0113 0.0707 0.6682 0.0354 0.0486 0.1654 0.0007 0.0100 0.1721 0.6906 0.0345 0.0444 0.0469 0.0002 0.0112 0.0692 0.6839 0.0352 0.0480 0.1522 0.0005 0.0118 0.0673 0.6298 0.0358 0.0493 0.2058 0.0003 0.0108 0.0771 0.7265 0.0335 0.0470 0.1047 0.0003 0.0107 0.0849 0.7558 0.0330 0.0464 0.0692 0.0009 0.0133 0.0531 0.5115 0.0398 0.0526 0.3290 0.0004 0.0117 0.0647 0.6358 0.0360 0.0491 0.2024 0.0009 0.0134 0.0514 0.5019 0.0400 0.0526 0.3400 0.0003 0.0107 0.0858 0.7603 0.0329 0.0462 0.0642 0.0003 0.0110 0.0686 0.6993 0.0344 0.0477 0.1386 0.0004 0.0117 0.0662 0.6418 0.0356 0.0489 0.1952 0.0005 0.0120 0.0641 0.6183 0.0366 0.0497 0.2188 0.0004 0.0118 0.0729 0.6690 0.0354 0.0484 0.1623 0.0001 0.0109 0.0716 0.7171 0.0343 0.0471 0.1186 0.0002 0.0107 0.0930 0.7151 0.0341 0.0465 0.1000 0.0006 0.0124 0.0597 0.5818 0.0378 0.0507 0.2569 0.0002 0.0112 0.0808 0.7342 0.0335 0.0466 0.0935 0.0002 0.0105 0.1354 0.7218 0.0330 0.0456 0.0531 0.0004 0.0107 0.1074 0.7477 0.0327 0.0455 0.0561 0.0004 0.0116 0.0670 0.6507 0.0354 0.0487 0.1860 0.0010 0.0099 0.3056 0.5651 0.0362 0.0443 0.0373 0.0005 0.0102 0.1635 0.6988 0.0338 0.0446 0.0477 0.0005 0.0120 0.0626 0.6109 0.0367 0.0498 0.2274 0.0002 0.0108 0.0892 0.7369 0.0331 0.0466 0.0830 0.0002 0.0109 0.0715 0.7119 0.0344 0.0473 0.1237 0.0003 0.0107 0.0766 0.7028 0.0337 0.0470 0.1286 0.0006 0.0125 0.0585 0.5721 0.0381 0.0510 0.2672 0.0007 0.0125 0.0593 0.5732 0.0378 0.0509 0.2657 0.0001 0.0108 0.0729 0.7216 0.0342 0.0471 0.1131 0.0006 0.0111 0.0835 0.6748 0.0351 0.0482 0.1469 0.0001 0.0107 0.0936 0.7530 0.0339 0.0464 0.0614 0.0008 0.0099 0.2998 0.5699 0.0356 0.0443 0.0387 0.0007 0.0129 0.0601 0.5469 0.0388 0.0513 0.2893 0.0007 0.0100 0.2820 0.5875 0.0354 0.0442 0.0393 0.0003 0.0107 0.0792 0.7403 0.0332 0.0467 0.0895 0.0004 0.0116 0.0678 0.6433 0.0355 0.0489 0.1923 0.0002 0.0106 0.0820 0.7411 0.0334 0.0465 0.0861 0.0006 0.0102 0.1842 0.6794 0.0339 0.0449 0.0462 0.0004 0.0115 0.0655 0.6621 0.0355 0.0486 0.1765 0.0009 0.0134 0.0514 0.5019 0.0400 0.0526 0.3400 0.0009 0.0099 0.2521 0.6170 0.0352 0.0443 0.0396 0.0004 0.0105 0.1176 0.7393 0.0328 0.0454 0.0543 0.0007 0.0130 0.0553 0.5389 0.0388 0.0517 0.3019 E_Er, V_Er, E_V, V_V: f = 1.0e-03 * 0.0026 0.3864 0.0250 0.0002 ```