1
$\begingroup$

Price deviation = financial bubbles.

i try to fit the following stock price index to predict recession. I couldn't fit the model with the data(Data are not available in yahoo finance for the particular period, so I bought the data and typed). Can anyone help me to point out my mistakes??? the codes (R 2.14.2) are as follows:

 library(tseries)
 library(zoo)
 ts<- structure(c(505.47, 528.03, 526.19, 553.65, 543.49, 541.74, 541.01, 535.38, 545.80, 553.46, 559.10, 560.05, 561.83, 562.50, 557.92, 559.16, 552.60, 548.78, 549.41, 544.66, 541.34, 536.32, 529.30, 524.20, 520.57, 521.03,  523.08, 526.70, 530.70, 522.69, 521.81, 521.77, 518.20, 513.11, 508.25, 508.26, 508.22, 507.37, 510.36, 518.80, 514.59, 517.24, 517.59, 516.60, 514.99, 522.36, 525.98, 533.54, 530.44, 535.43, 534.69, 530.30, 531.40, 537.03, 531.09, 531.67, 533.16, 536.19, 554.57, 556.73, 555.85, 553.21, 545.74, 528.60, 535.64, 535.72, 537.59, 539.04, 537.38, 537.07, 536.18, 532.32, 532.09, 528.21, 528.38, 529.41, 529.32, 530.33, 531.42, 531.75, 530.90, 532.69, 537.85, 542.45, 543.30, 540.61, 541.13, 540.64, 545.52, 549.78,  550.48,  551.80, 554.38, 556.22, 552.16, 552.99, 557.34, 555.66, 553.48, 552.37, 551.90, 548.69, 546.63,  552.28,  569.23, 569.65, 572.23, 570.57, 572.27, 571.64, 568.78, 570.55, 569.84, 573.28, 568.75, 570.64, 575.81,  589.84, 603.35, 604.45, 600.76,  594.63, 595.75, 597.78,  607.67, 618.72, 619.06, 615.73,  610.86,  602.29,  603.16,  600.66, 605.06, 601.93, 597.83, 598.64,  593.93, 590.94, 584.01,  586.95, 588.36, 592.42,  595.22,  591.74, 594.49, 600.40, 597.96, 603.19, 609.33, 609.90, 607.40, 604.27, 602.71, 597.69, 593.21, 588.37, 588.12, 586.46, 573.61, 563.69, 571.31, 567.51, 564.51, 568.19, 575.83, 575.75, 572.46, 572.57, 573.95, 582.90, 583.71, 581.02, 583.62, 585.12, 591.63, 597.47, 603.02,  604.46,  603.56, 605.71, 603.97, 603.08,  598.65, 593.03, 590.34, 590.91, 594.09, 592.86, 592.48, 591.93, 592.52, 591.20, 600.17, 589.09, 585.33,  586.74, 588.43, 586.23, 587.16, 585.39, 584.67, 589.52, 590.55, 600.17, 602.15, 603.19, 596.50, 596.58,  594.05, 593.52, 594.00, 598.60, 596.55, 591.14, 592.33, 596.47, 596.08, 595.75, 596.70,  595.97,  603.72,  611.57, 616.08, 617.94, 612.36, 611.74,  605.33, 602.76,  605.16, 608.79, 602.24, 593.53,  589.70,  585.77,  590.71, 598.56,  601.53, 601.39, 603.54, 602.58, 598.27, 597.11, 595.16, 597.80, 594.57, 592.18,  581.94, 568.25, 556.24, 560.01, 564.21, 567.66, 565.56, 561.27, 559.51, 567.56, 574.73, 573.50, 572.92,  577.71, 581.17, 584.84, 586.61, 584.19, 580.79, 583.74, 585.91, 585.15, 587.13, 587.54, 587.99, 588.32, 590.84,  593.07, 602.60, 610.65, 605.93, 602.28, 598.10, 594.59, 594.33, 590.70, 592.04, 593.08, 592.63, 602.72,  605.13, 606.56, 606.17, 608.05, 612.33, 614.96, 615.85, 623.87, 630.07, 633.32, 633.31, 637.13, 641.31,  649.06, 653.07, 659.33, 660.35, 654.60, 648.93, 645.32, 646.80, 652.23, 650.62,  646.25, 641.90, 641.77,  637.39, 642.60, 643.79, 643.02, 641.84, 638.79, 640.48, 641.89, 637.23, 641.51, 636.40,  637.61, 635.56,  635.93, 633.52, 633.94, 635.26,  632.54, 628.88, 632.89, 636.11, 641.08, 643.60, 644.94, 645.04, 646.39,  645.03, 642.95, 642.66, 643.96, 632.83, 630.89, 633.66, 636.26, 629.51, 621.46, 616.46, 614.28,  619.81,  617.49, 624.37, 624.12, 622.61, 625.93, 626.34, 624.89, 618.41, 624.49, 628.38, 639.24, 642.95, 644.90,  641.60, 639.97, 638.62, 635.84, 635.42, 634.91, 635.01, 625.81, 623.11, 629.43, 635.59, 632.28, 634.22,  638.83, 638.19, 634.87, 637.00, 636.44, 636.67, 638.34, 637.93, 631.79, 637.37, 637.25, 638.05, 634.81, 637.79, 640.39, 640.24, 636.33, 634.65, 634.42, 638.87, 638.92, 643.25, 649.56, 651.45, 656.13,  655.89,  661.35, 657.23, 655.24, 657.13, 655.58, 660.60, 660.73, 662.02, 664.95, 672.53, 691.70, 691.45, 691.65, 693.44, 692.57, 695.43, 705.00, 719.61, 717.37, 706.08, 706.46, 712.95, 716.54, 710.99, 712.67,  718.04,  720.00, 720.36, 725.46, 729.43,  735.11, 729.35, 724.48, 731.37, 735.39, 736.07, 735.25, 740.06,  741.27,  735.06, 732.15,  728.16, 736.28, 734.93, 737.53, 746.29, 743.16, 739.42, 736.51, 720.46, 735.25,  724.96, 717.96, 701.66, 708.80, 722.07, 720.58, 721.17, 719.84, 734.73, 732.51, 731.67, 732.66,  733.98, 728.05, 722.82, 718.86, 719.30, 714.23, 735.78, 742.50, 745.90, 761.60, 759.76, 757.65, 758.86, 755.57, 763.97,  764.70, 766.08, 763.46, 771.67, 781.99, 785.49, 778.66, 779.87, 780.60, 780.42, 776.16, 771.91, 772.76,  784.91, 791.02, 798.82, 805.51, 805.26, 805.47, 805.72, 811.78, 808.49, 821.00, 851.10, 851.40, 848.40, 849.70, 842.50, 833.00, 820.70, 820.30, 817.70, 816.40, 819.70, 819.60, 816.40, 818.60, 819.30,  830.30,  837.80, 846.10, 849.90, 846.50, 853.83, 859.87, 852.26, 859.87, 865.77, 874.34, 884.62, 895.15, 905.71,  911.38, 919.03, 927.46, 914.53, 926.16, 922.95, 937.09, 934.11, 930.49, 929.85, 934.53, 946.72, 971.99,  967.05, 972.36, 982.03, 977.06, 944.94, 921.52, 937.66, 950.89, 967.60, 972.47, 961.37, 951.30,  957.81,  956.57, 984.92, 989.40, 966.88, 970.30,  970.31,  976.34, 972.10, 996.45, 1015.42, 1017.51, 1023.92, 1041.11,  1063.25, 1060.79, 1067.34, 1075.85, 1090.35, 1092.66, 1116.09, 1112.59, 1109.53, 1098.08, 1096.61, 1121.38, 1152.24, 1178.46, 1205.28, 1223.05, 1231.86, 1231.73, 1275.32, 1288.85, 1313.35, 1314.46, 1248.69), .Dim = c(595L, 1L), .Dimnames = list(NULL, "Close"), index = structure(c(7900,7901, 7902, 7903, 7904, 7907, 7908, 7909, 7910, 7911, 7914, 7915, 7916, 7917, 7918, 7921, 7922,  7923,7924, 7925, 7928, 7929, 7930, 7931, 7935, 7936, 7937, 7938,7939, 7942, 7943, 7944, 7945,7946,  7949, 7950, 7951, 7952, 7953, 7956, 7957, 7958, 7959, 7960, 7963, 7964, 7965,  7966, 7967, 7970,  7971,   7972, 7973, 7974,  7977, 7979, 7980, 7981, 7984, 7985,7986, 7987, 7988, 7991, 7992, 7993, 7994, 7995,  7998, 7999, 8000, 8001, 8002, 8005, 8006, 8007,8008,  8009, 8012, 8013, 8014, 8015, 8016, 8019, 8020,   8021, 8022,  8023, 8026, 8027, 8029, 8030, 8033, 8034, 8036,8037, 8040, 8041,  8042, 8043, 8044, 8047,  8048, 8049, 8050, 8051, 8054, 8055, 8056, 8057, 8058, 8061, 8062, 8063, 8064, 8065, 8071, 8072, 8075,  8076, 8077, 8078, 8079, 8082, 8083, 8084, 8085, 8086, 8089, 8090, 8091,8092, 8093,8096, 8097, 8098,  8099,8100,8103, 8104, 8105, 8106, 8107, 8110, 8111, 8112, 8113, 8114, 8117, 8118, 8119, 8120,  8121,  8124, 8125, 8126, 8127, 8128, 8133, 8134, 8135,8138, 8139, 8140, 8141, 8142,8145,8146, 8147, 8148,  8149, 8152, 8153,8154, 8155, 8159, 8160,8161, 8162, 8163, 8166, 8167, 8168, 8169, 8170, 8174, 8175,  8176, 8177, 8180, 8181, 8182,  8183, 8184, 8187, 8188, 8189, 8190, 8191, 8194, 8195, 8196, 8198,  8201,  8202, 8203, 8204, 8205, 8208, 8209, 8210, 8211, 8212, 8215, 8216, 8217, 8219, 8222, 8223, 8224,  8225,  8226, 8229, 8230, 8231, 8232, 8233, 8236, 8237, 8238, 8239, 8240, 8243, 8244, 8245, 8246, 8247,  8250, 8251, 8252, 8253, 8254, 8257, 8258,8259, 8260, 8261, 8264, 8265, 8266, 8267, 8268, 8271, 8272,  8273,  8274, 8275, 8279, 8280, 8281, 8282, 8285, 8286,  8288, 8289, 8292, 8293, 8294, 8295, 8296, 8299,  8300,  8301, 8302, 8303, 8306, 8307, 8308, 8309, 8310,8313, 8314, 8315, 8316, 8317, 8320, 8321, 8322,  8323,  8324, 8327, 8328, 8329, 8330, 8331, 8335, 8336, 8337, 8338, 8341, 8342, 8343, 8344, 8345, 8348, 8349,  8350, 8351, 8352, 8355, 8356, 8357, 8358, 8359, 8362, 8363, 8364, 8365, 8366, 8369, 8370, 8371, 8372,  8373, 8376, 8377, 8378, 8379,8380,8383, 8384, 8385, 8386, 8387, 8390,8391,8392, 8393,8397,8398,  8399, 8400, 8404, 8405, 8406,8407,  8408,8411, 8412, 8413, 8414, 8415, 8418, 8419,8420, 8421, 8426,  8427, 8428, 8429, 8433, 8434, 8435, 8436,8439, 8440, 8441, 8442, 8443, 8446, 8447, 8448, 8449, 8450,  8453,  8454, 8455, 8456, 8457, 8460, 8461, 8462,  8463, 8464, 8467, 8468, 8469, 8470, 8471, 8474,  8475, 8476, 8477, 8478, 8481, 8482, 8488, 8489, 8490, 8491,  8492, 8495, 8496,  8497, 8498, 8499, 8502, 8503, 8504, 8505, 8506, 8509, 8510, 8511, 8512, 8513, 8516, 8517, 8518, 8519, 8520, 8523, 8524, 8525, 8527, 8530,  8531, 8532, 8533, 8534, 8537, 8538,  8539, 8540, 8541, 8544, 8545, 8546, 8547, 8548, 8552,  8553, 8554, 8555, 8558, 8559, 8560, 8561, 8562,  8565, 8566, 8567, 8568, 8569, 8573, 8574, 8575, 8576,  8579, 8580, 8581, 8582, 8583,  8586, 8587, 8588, 8589, 8590,8593, 8594, 8595,  8596, 8597, 8600, 8601,  8602, 8603, 8604, 8607, 8608, 8609, 8610, 8611, 8614, 8615, 8616,  8617, 8618,  8621, 8622, 8623,8624, 8625,8628, 8629, 8630, 8631, 8632, 8635, 8636, 8637,  8638, 8639, 8644, 8645, 8646, 8649, 8650, 8651, 8652, 8653, 8656, 8657, 8658, 8659, 8660, 8663,  8664, 8665, 8666, 8667, 8670, 8671, 8672, 8673, 8674,  8677, 8678, 8679, 8680, 8681, 8684, 8685, 8686, 8687, 8688, 8691, 8692, 8693, 8694,8695, 8698, 8699,  8700, 8701,8702, 8705, 8706, 8707, 8708, 8709, 8712, 8713, 8714, 8715, 8716, 8719, 8720, 8721, 8722, 8723, 8726, 8727, 8728, 8729, 8730, 8733,8734, 8735,  8736,  8737, 8740, 8741, 8742, 8743, 8744, 8747, 8748, 8749, 8750, 8751, 8754, 8755, 8756, 8757, 8758, 8761, 8762, 8763, 8764, 8765, 8768, 8769, 8770,  8771), class = "Date"), class = "zoo")

  df<-data.frame(ts)
  df<-data.frame(Date=as.Date(rownames(df)),Y=df$Close)
      df<-df[!is.na(df$Y),]
  library(minpack.lm)
  library(ggplot2)
  df$days<-as.numeric(df$Date-df[1,]$Date)
      f<-function(pars,xx){pars$a + (pars$tc - xx)^pars$m *(pars$b+ pars$c *        cos(pars$omega*log(pars$tc - xx) + pars$phi))}
      resids<-function(p,observed,xx){df$Y-f(p,xx)}

 nls.out <- nls.lm(par=list(a=6,b=-6,tc=3000, m=.4,omega=10,phi=-2,c=-1),fn = resids, observed = df$Y, xx = df$days, control= nls.lm.control (maxiter =1024, ftol=1e-6, maxfev=1e6))
 Warning messages:
1: In log(pars$tc - xx) : NaNs produced
    2: In log(pars$tc - xx) : NaNs produced
3: In log(pars$tc - xx) : NaNs produced
    4: In log(pars$tc - xx) : NaNs produced
5: In log(pars$tc - xx) : NaNs produced
nls.out
Nonlinear regression via the Levenberg-Marquardt algorithm
parameter estimates: 1292.85251942153, -14.1403675964815, 871.068726297531,   0.682341485852459, 0.742986030555774, 88.600462385114, 8.15118775146334 
residual sum-of-squares: 247649
reason terminated: Relative error in the sum of squares is at most `ftol'.


nls.out <- nls.lm(par=list(a=6,b=-6,tc=3000, m=.4,omega=10,phi=-2,c=-1),fn = resids, observed = df$Y, xx = df$days, control= nls.lm.control (maxiter =1024, ftol=1e-9, maxfev=1e6))
Warning messages:
1: In log(pars$tc - xx) : NaNs produced
    2: In log(pars$tc - xx) : NaNs produced
3: In log(pars$tc - xx) : NaNs produced
    4: In log(pars$tc - xx) : NaNs produced
5: In log(pars$tc - xx) : NaNs produced


nls.out <- nls.lm(par=list(a=6,b=-6,tc=3000, m=.5,omega=10,phi=-2,c=-1),fn = resids, observed = df$Y, xx = df$days, control= nls.lm.control (maxiter =1024, ftol=1e-9, ma xfev=1e6))
 Warning messages:
 1: In log(pars$tc - xx) : NaNs produced
     2: In log(pars$tc - xx) : NaNs produced
 3: In log(pars$tc - xx) : NaNs produced
     4: In log(pars$tc - xx) : NaNs produced
 5: In log(pars$tc - xx) : NaNs produced
     6: In log(pars$tc - xx) : NaNs produced


nls.out <- nls.lm(par=list(a=6,b=-6,tc=3000, m=.2,omega=10,phi=-2,c=-1),fn = resids, observed = df$Y, xx = df$days, control= nls.lm.control (maxiter =1024, ftol=1e-9, maxfev=1e6))
  Warning messages:
 1: In log(pars$tc - xx) : NaNs produced
     2: In log(pars$tc - xx) : NaNs produced
 3: In log(pars$tc - xx) : NaNs produced
     4: In log(pars$tc - xx) : NaNs produced
 5: In log(pars$tc - xx) : NaNs produced
     6: In log(pars$tc - xx) : NaNs produced
 7: In log(pars$tc - xx) : NaNs produced
     8: In log(pars$tc - xx) : NaNs produced
 9: In log(pars$tc - xx) : NaNs produced
     10: In log(pars$tc - xx) : NaNs produced

I tried many difference initial values but i couldn't set anything. Your help will be highly appreciated. You may refer the model in the following paper screen 33 (eq.2.9).

$\endgroup$
5
  • 2
    $\begingroup$ I'm not clear on what you're asking here. What's the relation between the title of your question and your code? Could you rephrase the first part of your question to explain exactly what it is your trying to achieve? Also, what is the time series in the ts variable? $\endgroup$ – Bob Jansen Jul 16 '14 at 14:51
  • $\begingroup$ Hello @BobJansen. Actually the title is very general to this question. Financial bubbles form when the stock price deviates from its intrinsic value. The formed bubble will expand if investors keep trading that particular stock. The bubble will start to shrink or burst if the investors start to sell the stocks. When the investors all sell the stock in short period the bubble will burst (economic recession). $\endgroup$ – user3610659 Jul 17 '14 at 0:26
  • $\begingroup$ CONTINUE.......By using the model that found in this link [er.ethz.ch/publications/PHD_YanWanfeng_Final_thesis_911.pdf] screen 33 (eq.2.9) we can predict when the bubble will burst or stock price stop increasing (critical point). This fitting method I found in this [link] (stackoverflow.com/questions/21804609/…). ts is time series of KLSE Index from (1991-1994) to predict economic recession during the year 1998. If my explanation is not clear enough pls let me knw. tq @BobJansen. $\endgroup$ – user3610659 Jul 17 '14 at 0:31
  • $\begingroup$ The less general the title, the better. I believe your equation has some errors. Can you take a careful look at it? For instance, I believe that $B$ should depend on the parameter $m$ and can't be seen as a independent input to the optimization. $\endgroup$ – Bob Jansen Jul 17 '14 at 6:08
  • $\begingroup$ tq @BobJansen. i will check again and let u knw about it... $\endgroup$ – user3610659 Jul 17 '14 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.