matlab - subscript indices must be real positive in cummswapcfbytrintree.m -
i have copied code matlab example file bermudanswaption.m in effort calibrate hull white 1 factor model given market data. use subset 30 or fewer swaptions calibration set, able arrive @ interior solution. when use larger set of instruments, weird error:
below code:
valuationdate = '10-01-2014'; settle = datenum(valuationdate); % 0 rate data market data, bootstrapped bloomberg , reuters quotes curvedates = [735874; 735882; 735906; 735936; 735950; 736040; 736133; 736224; 736314; 736424; 736606; 736788; 736971; 737153; 737336; 737518; 737701; 737884; 738069; 738251; 738433; 738615; 738797; 738979; 739162; 739345; 739528; 739710; 739893; 740075; 740260; 740442; 740624; 740806; 740989; 741171; 741354; 741536; 741719; 741901; 742084; 742269; 742451; 742633; 742815; 742997; 743180; 743362; 743545; 743728; 743911; 744093; 744278; 744460; 744642; 744824; 745006; 745189; 745372; 745554; 745737; 745919; 746102; 746284; 746469; 746651; 746833; 747015; 747198; 747380; 747563; 747745; 747928; 748111; 748296; 748478; 748660; 748842; 749024; 749206; 749389; 749572; 749755; 749937; 750120; 750302; 750487]; zerorates = 1.0e-03*[0.0172; 0.0188; 0.0191; 0.0221; 0.0249; 0.0244; 0.0269; 0.0333; 0.0423; 0.0571; 0.0789; 0.1021; 0.1253; 0.1435; 0.1617; 0.1749; 0.1881; 0.1973; 0.2064; 0.2158; 0.2253; 0.2311; 0.2370; 0.2429; 0.2488; 0.2547; 0.2607; 0.2640; 0.2672; 0.2706; 0.2738; 0.2772; 0.2807; 0.2842; 0.2877; 0.2913; 0.2948; 0.2964; 0.2979; 0.2995; 0.3011; 0.3026; 0.3043; 0.3060; 0.3077; 0.3095; 0.3112; 0.3118; 0.3125; 0.3132; 0.3138; 0.3146; 0.3152; 0.3160; 0.3167; 0.3175; 0.3183; 0.3186; 0.3189; 0.3192; 0.3196; 0.3199; 0.3202; 0.3206; 0.3209; 0.3213; 0.3217; 0.3217; 0.3216; 0.3216; 0.3216; 0.3216; 0.3216; 0.3216; 0.3216; 0.3216; 0.3216; 0.3217; 0.3217; 0.3218; 0.3218; 0.3219; 0.3219; 0.3220; 0.3220; 0.3221; 0.3221]; compounding = 2; ratespec = intenvset('compounding', 2,'valuationdate', valuationdate,'startdates', valuationdate,'enddates', curvedates,'rates', zerorates); instrumentmaturity = datenum('12-sep-2044'); % swaption vol data bloomberg swaptionblackvol = [ 0.5940 0.5550 0.4450 0.3710 0.3400 0.3110 0.2910 0.2750 0.2630 0.2520 0.2250 0.2140 0.2080 0.2050; 0.5630 0.5470 0.4420 0.3690 0.3360 0.3090 0.2900 0.2740 0.2630 0.2520 0.2260 0.2150 0.2090 0.2060; 0.5760 0.5330 0.4400 0.3730 0.3410 0.3150 0.2970 0.2820 0.2700 0.2590 0.2330 0.2220 0.2170 0.2140; 0.5840 0.5020 0.4240 0.3730 0.3480 0.3240 0.3060 0.2920 0.2810 0.2710 0.2430 0.2300 0.2230 0.2190; 0.5630 0.4750 0.4100 0.3700 0.3450 0.3230 0.3070 0.2940 0.2830 0.2740 0.2470 0.2330 0.2260 0.2210; 0.5510 0.4520 0.3980 0.3660 0.3410 0.3220 0.3070 0.2950 0.2850 0.2760 0.2500 0.2360 0.2290 0.2240; 0.4630 0.4010 0.3660 0.3440 0.3250 0.3100 0.2990 0.2890 0.2790 0.2720 0.2470 0.2320 0.2260 0.2210; 0.4230 0.3750 0.3480 0.3290 0.3140 0.3030 0.2930 0.2840 0.2760 0.2690 0.2420 0.2300 0.2240 0.2190; 0.3700 0.3470 0.3280 0.3110 0.2960 0.2880 0.2800 0.2730 0.2680 0.2620 0.2360 0.2240 0.2190 0.2150; 0.3420 0.3250 0.3100 0.2970 0.2850 0.2770 0.2700 0.2640 0.2590 0.2540 0.2280 0.2180 0.2140 0.2110; 0.3230 0.3010 0.2900 0.2810 0.2720 0.2650 0.2590 0.2540 0.2500 0.2470 0.2230 0.2130 0.2090 0.2060; 0.3010 0.2860 0.2760 0.2670 0.2580 0.2530 0.2480 0.2450 0.2420 0.2390 0.2160 0.2060 0.2030 0.2000; 0.2850 0.2750 0.2650 0.2560 0.2480 0.2440 0.2400 0.2370 0.2350 0.2320 0.2100 0.2000 0.1970 0.1940; 0.2710 0.2600 0.2510 0.2440 0.2380 0.2340 0.2310 0.2290 0.2260 0.2240 0.2040 0.1940 0.1910 0.1890; 0.2580 0.2470 0.2400 0.2350 0.2300 0.2270 0.2240 0.2210 0.2190 0.2170 0.1980 0.1890 0.1860 0.1840; 0.2460 0.2370 0.2320 0.2270 0.2240 0.2210 0.2180 0.2150 0.2130 0.2110 0.1980 0.1840 0.1820 0.1800; 0.2040 0.1980 0.1950 0.1920 0.1900 0.1890 0.1890 0.1880 0.1880 0.1870 0.1720 0.1660 0.1640 0.1620; 0.1790 0.1750 0.1740 0.1730 0.1730 0.1710 0.1710 0.1700 0.1690 0.1690 0.1530 0.1510 0.1500 0.1480; 0.1650 0.1650 0.1660 0.1670 0.1680 0.1670 0.1670 0.1680 0.1680 0.1680 0.1550 0.1580 0.1560 0.1530; 0.1530 0.1570 0.1590 0.1620 0.1640 0.1650 0.1660 0.1670 0.1680 0.1690 0.1560 0.1650 0.1620 0.1590]; % tenors underlying swaps , options on them swaptionexercisedates = cellstr(['1m ';'2m ';'3m '; '6m ';'9m ';'1y ';'18m';'2y ';'3y ';'4y ';'5y ';'6y ';'7y ';'8y ';'9y ';'10y';'15y';'20y';'25y';'30y']); swaptiontenors = cellstr(['1y '; '2y '; '3y '; '4y '; '5y '; '6y '; '7y '; '8y '; '9y '; '10y'; '15y'; '20y'; '25y'; '30y']); testmat = zeros(length(swaptionexercisedates),1); % here construct matrix of exercise dates = 1:length(swaptionexercisedates) if swaptionexercisedates{i}(end)=='y' testmat(i) = addtodate(settle,str2double(swaptionexercisedates{i}(1:end-1)),'year'); elseif swaptionexercisedates{i}(end)=='m' testmat(i)=addtodate(settle,str2double(swaptionexercisedates{i}(1:end-1)),'month'); end end eurexdates= testmat; eurexdatesfull = repmat(testmat,1,length(swaptiontenors)); testmat2 = zeros(length(swaptionexercisedates),length(swaptiontenors)); % here construct matix of maturity dates = 1:size(eurexdatesfull,1) j = 1:size(eurexdatesfull,2) if swaptiontenors{j}(end)=='y' testmat2(i,j) = addtodate(eurexdatesfull(i,j),str2double(swaptiontenors{j}(1:end-1)),'year'); elseif swaptiontenors{j}(end)=='m' testmat2(i,j)= addtodate(eurexdatesfull(i,j),str2double(swaptiontenors{j}(1:end-1)),'month'); end end end eurmatfull = testmat2; % construct index of swaptions intend use calibration relidx = find(eurmatfull <= instrumentmaturity); swaptionblackprices = zeros(size(swaptionblackvol)); swaptionstrike = zeros(size(swaptionblackvol)); % out swaption strikes , prices implied vol data. iswaption=1:length(swaptionexercisedates) itenor=1:length(swaptiontenors) [~,swaptionstrike(iswaption,itenor)] = swapbyzero(ratespec,[nan 0],settle, eurmatfull(iswaption,itenor),... 'startdate',eurexdatesfull(iswaption,itenor),'legreset',[1 2],'basis',2); swaptionblackprices(iswaption,itenor) = swaptionbyblk(ratespec,'call', swaptionstrike(iswaption,itenor),settle, ... eurexdatesfull(iswaption,itenor), eurmatfull(iswaption,itenor),swaptionblackvol(iswaption,itenor)); end end timespec = hwtimespec(settle,daysadd(settle,30*(1:370),6), 12); % construct index of random collection of instruments b = (214:224); hw1fobjfun4 = @(x) swaptionblackprices(relidx(b)) - ... swaptionbyhw(hwtree(hwvolspec(valuationdate,testmat,x(2),testmat,x(1),'spline'), ratespec, timespec), 'call',swaptionstrike(relidx(b)),eurexdatesfull(relidx(b)), 0,eurexdatesfull(relidx(b)), eurmatfull(relidx(b)),'basis',2, 'swapreset',12); options = optimset('disp','iter','maxfunevals',1000,'tolfun',1e-5); x0 = [.1 .01]; lb = [0 0]; ub = [1 1]; hw1fparams = lsqnonlin(hw1fobjfun4,x0,lb,ub,options)
in results, when use instruments numbered 214:224, able find local minima: hw1fparams(1) = 0.0801 , hw1fparams(2) = 0.0002
however when use instruments 150:224 following error:
"subscript indices must real positive integers or logicals" error in cummswapcfbytrintree (line 23)
this matlab created file. know what's going wrong here? how go diagnosing/debugging in error in matlab created file. usual techniques of checking variable or index being given 0 or double value not @ disposal anymore. thanks.
Comments
Post a Comment