0
$\begingroup$

I have problem with summation when it is in matrices. I want to make mathematica compute this:

enter image description here

Large X I have added dataset for as well as e. Would anyone be able to show me how to compute this in mathematica.

Due to comments Ill explain a bit more, X' is Transpose[X] so yes it would be a 2 by 2 matrix.

X= {{20.08, 1}, {19.78, 1}, {19.86, 1}, {19.4, 1}, {19.66, 1}, {19.32, 
  1}, {18.99, 1}, {19.29, 1}, {19.43, 1}, {19.52, 1}, {20.07, 
  1}, {20.13, 1}, {20.02, 1}, {19.9, 1}, {20.11, 1}, {20.44, 
  1}, {20.37, 1}, {20.31, 1}, {19.7, 1}, {20.12, 1}, {19.63, 
  1}, {19.76, 1}, {19.34, 1}, {19.66, 1}, {19.85, 1}, {20.32, 
  1}, {20.5, 1}, {20.9, 1}, {21.21, 1}, {21.15, 1}, {21.18, 
  1}, {21.02, 1}, {21.29, 1}, {20.92, 1}, {20.83, 1}, {21.22, 
  1}, {20.73, 1}, {20.76, 1}, {21.16, 1}, {21.31, 1}, {21.94, 
  1}, {22.7, 1}, {22.95, 1}, {22.8, 1}, {22.7, 1}, {22.9, 1}, {22.6, 
  1}, {22.4, 1}, {22.34, 1}, {22.43, 1}, {22.07, 1}, {22.01, 
  1}, {22.17, 1}, {22.1, 1}, {22.86, 1}, {23.26, 1}, {23.38, 
  1}, {23.59, 1}, {23.59, 1}, {23.53, 1}, {23.5, 1}, {23.78, 
  1}, {23.38, 1}, {23.52, 1}, {23.3, 1}, {23.48, 1}, {23.83, 
  1}, {24.2, 1}, {24.49, 1}, {24.69, 1}, {23.98, 1}, {23.79, 
  1}, {23.7, 1}, {23.26, 1}, {23.47, 1}, {23.95, 1}, {24.38, 
  1}, {24.75, 1}, {25.2, 1}, {24.68, 1}, {24.43, 1}, {24.73, 
  1}, {24.3, 1}, {24.97, 1}, {25.07, 1}, {25.17, 1}, {26.07, 
  1}, {26.41, 1}, {26.24, 1}, {26.83, 1}, {26.39, 1}, {26.64, 
  1}, {27.34, 1}, {26.58, 1}, {26.52, 1}, {26.58, 1}, {27., 
  1}, {26.54, 1}, {26.02, 1}, {26.33, 1}, {26.07, 1}, {25.81, 
  1}, {26.33, 1}, {26.3, 1}, {26.39, 1}, {26.26, 1}, {25.75, 
  1}, {26.21, 1}, {27.04, 1}, {26.74, 1}, {26.4, 1}, {25.89, 
  1}, {26.37, 1}, {26.28, 1}, {26.54, 1}, {26.66, 1}, {26.24, 
  1}, {25.35, 1}, {25.19, 1}, {24.07, 1}, {24.96, 1}, {25.29, 
  1}, {25.47, 1}, {25.13, 1}, {25.24, 1}, {24.99, 1}, {25.59, 
  1}, {25.68, 1}, {25.53, 1}, {26.68, 1}, {26.56, 1}, {27.04, 
  1}, {27.23, 1}, {27.34, 1}, {26.88, 1}, {29.66, 1}, {29.66, 
  1}, {29.11, 1}, {27.86, 1}, {27.36, 1}, {27.84, 1}, {28.27, 
  1}, {28.11, 1}, {27.93, 1}, {28.05, 1}, {28.09, 1}, {27.96, 
  1}, {27.65, 1}, {27.67, 1}, {27.32, 1}, {27.39, 1}, {27.48, 
  1}, {27.68, 1}, {28.35, 1}, {28.34, 1}, {28.05, 1}, {27.14, 
  1}, {27.32, 1}, {26.91, 1}, {27.12, 1}, {27.06, 1}, {27.9, 
  1}, {27.99, 1}, {27.7, 1}, {27., 1}, {27.11, 1}, {27.3, 1}, {27.12, 
  1}, {27.78, 1}, {28.07, 1}, {28.23, 1}, {28.17, 1}, {29.24, 
  1}, {29.48, 1}, {29.19, 1}, {29.65, 1}, {29.26, 1}, {29.62, 
  1}, {30., 1}, {30.44, 1}, {31.03, 1}, {30.93, 1}, {30.26, 
  1}, {31.27, 1}, {31.34, 1}, {32.75, 1}, {33.55, 1}, {33.17, 
  1}, {34.31, 1}, {34.14, 1}, {33.88, 1}, {34.89, 1}, {34.14, 
  1}, {32.93, 1}, {33.01, 1}, {33.87, 1}, {34.15, 1}, {34., 
  1}, {33.38, 1}, {33.09, 1}, {32.74, 1}, {33.43, 1}, {34.06, 
  1}, {33.94, 1}, {33.1, 1}, {33.08, 1}, {32.25, 1}, {32.35, 
  1}, {33.17, 1}, {32.57, 1}, {32.94, 1}, {33.18, 1}, {33.19, 
  1}, {32.97, 1}, {32.88, 1}, {32.11, 1}, {33.12, 1}, {33.82, 
  1}, {34.07, 1}, {35.1, 1}, {35.69, 1}, {35.47, 1}, {34.98, 
  1}, {34.63, 1}, {35.62, 1}, {36.3, 1}, {36.49, 1}, {36.29, 
  1}, {36.64, 1}, {36.96, 1}, {36.98, 1}, {37.01, 1}, {36.56, 
  1}, {38.13, 1}, {38.87, 1}, {38.86, 1}, {38.87, 1}, {40.22, 
  1}, {39.16, 1}, {39.35, 1}, {39.73, 1}, {39.73, 1}, {39.51, 
  1}, {40.04, 1}, {40.2, 1}, {40.12, 1}, {40.77, 1}, {40.85, 
  1}, {40.65, 1}}



    e={-39.5086, -34.3476, -21.3119, -17.2624, -22.8212, -12.6281, \
-4.05305, -10.284, -29.2365, -28.9788, -47.4106, -52.2288, -57.3004, \
-57.044, -21.7327, -14.2378, -13.6716, -15.6634, -3.07337, -9.56075, \
0.0428573, 17.8984, 7.95581, 9.61876, 11.0461, 7.3986, 15.974, \
6.7027, 0.0836829, 3.19188, 7.67778, 15.2763, 24.9694, 16.4266, \
20.9189, 18.9357, 17.7393, 16.6252, 19.9538, 19.0034, 14.0373, \
17.3368, 30.486, 25.6365, 28.4368, 24.1961, 32.2371, 28.1878, 26.836, \
21.6437, 20.0929, 14.5311, 15.5325, 20.0288, 4.56828, -2.70303, \
-5.26942, -5.80811, -15.5681, -23.0899, -15.6158, -8.19073, -8.70942, \
-22.0519, -30.5952, -41.6398, -44.3709, -37.6881, -42.0611, -45.5617, \
-42.4614, -28.0188, -37.4065, -47.103, -16.4617, -23.8073, -22.8127, \
-23.1099, -34.5714, -34.0337, -12.4429, -11.6738, -9.01844, -10.4266, \
4.10304, 18.3527, -0.190233, 10.8367, 11.3732, 10.7993, 15.0527, \
20.6719, 38.3996, 38.3901, 44.6483, 43.0501, 34.8527, 24.5722, \
26.1599, 11.7909, 23.8498, 14.9986, 9.23092, 10.165, 5.15272, \
-1.32286, 7.33681, 5.00731, 6.98059, 22.2116, 17.1747, 17.4044, \
14.8388, 14.3011, 21.4022, 33.8758, 40.5732, 38.6981, 37.4166, \
43.9763, 26.3214, 28.5563, 29.1317, 37.8048, 43.5765, 41.9573, \
36.5953, 42.233, 56.2035, 38.1798, 40.8262, 47.4906, 47.248, 47.1996, \
49.3891, 4.39601, -3.47399, -8.86219, 24.9919, 25.9935, 17.508, \
-4.48744, -4.30891, -4.54432, 2.20928, -1.59285, 16.9316, 24.1806, \
22.2945, 19.3957, 12.3694, 12.9571, 7.54649, -7.9417, -12.0437, \
-18.9007, -14.6697, -20.2643, -5.04498, -8.59367, -3.73548, -12.6302, \
-17.5525, -16.7896, -21.9673, -25.3056, -21.4283, -27.1137, -24.0638, \
-17.3968, -11.9953, -11.0271, -19.4666, -22.6394, -10.5365, -20.936, \
-18.7627, -25.7619, -33.4171, -23.1605, -37.4145, -31.1141, -37.136, \
-52.7573, -63.4735, -84.8161, -99.2387, -93.7135, -100.639, -96.9627, \
-104.754, -124.455, -119.213, -112.161, -111.235, -112.446, -113.121, \
-106.63, -90.9322, -70.3292, -74.0381, 37.6777, 19.6116, 25.208, \
62.8927, 57.3488, 60.1155, 58.3352, 66.6165, 70.2785, 64.5913, \
57.2585, 56.1704, 55.0572, 57.7095, 55.078, 47.1967, 30.6944, \
27.9336, 32.3462, 25.7823, 27.589, 33.3226, 32.5037, 13.9685, \
14.9823, 9.79964, 27.0003, 33.9492, 33.5921, 29.756, 24.1719, \
30.0634, 10.1723, -2.36215, 10.3259, 18.4378, 3.62343, 13.0049, \
2.67227, -12.503, -0.312969, 0.043751, 6.55802, 5.49949, 21.1638, \
25.3716, 20.8474, 29.628}

I have added the explanation from the book:

enter image description here

$\endgroup$
8
  • $\begingroup$ is $X'$ supposed to be the transpose of $X$? Then $X'X$ is just a 2 by 2 matrix. What is $N$ in there? You can say few words to explain the symbols used. $\endgroup$
    – Nasser
    Dec 27, 2013 at 7:28
  • $\begingroup$ That is correct it is just the transpose. $\endgroup$
    – ALEXANDER
    Dec 27, 2013 at 7:31
  • $\begingroup$ But then, the result is a matrix. Yet, you say there is a linear sum there. This does not add up. How can sum[x x',{1,N}] be a sum over a matrix? a matrix is 2D not 1D. May be someone better than me in math can figure it out. $\endgroup$
    – Nasser
    Dec 27, 2013 at 7:35
  • $\begingroup$ Nope, everything in there is taken straight out of textbook. Should be correct! It is also given proof in the textbook, however did not think it was any reason to post it because I am only interested in how to make mathematica due the calculation. $\endgroup$
    – ALEXANDER
    Dec 27, 2013 at 7:37
  • $\begingroup$ I could add the "proof" if that makes a difference? Could add the explanation, could be that I have misunderstood! $\endgroup$
    – ALEXANDER
    Dec 27, 2013 at 7:39

1 Answer 1

1
$\begingroup$

Please beware that this is NOT optimized. I just wrote down the formulas for easy understanding:

m =  SparseArray[{p_, q_} :> Sum[x[[i, p]] x[[i, q]], {i, Length@x}], {2, 2 }];
me = SparseArray[{p_, q_} :> Sum[e[[i]]^2 x[[i, p]] x[[i, q]], {i, Length@x}], {2, 2}];
mi = Inverse[m];
(v = mi.me.mi) // MatrixForm

$\left( \begin{array}{cc} 0.187057 & -4.58584 \\ -4.58584 & 116.389 \\ \end{array} \right)$

$\endgroup$
8
  • $\begingroup$ Would you be able to optimize it? $\endgroup$
    – ALEXANDER
    Dec 28, 2013 at 1:03
  • $\begingroup$ @ALEXANDER I don't think it will serve any purpose, since Mathematica already has its own least squares algorithms implemented $\endgroup$ Dec 28, 2013 at 1:08
  • $\begingroup$ But will the least square in mathematica be able to create newey west standard errors and white standard errors? $\endgroup$
    – ALEXANDER
    Dec 28, 2013 at 1:13
  • $\begingroup$ @ALEXANDER If you have heteroskedasticity, take a look at ARCH and GARCH in the docs $\endgroup$ Dec 28, 2013 at 1:25
  • $\begingroup$ The problem is that I need to be able to handle it with Newey west standard errors or white standard errors, I am aware of ARCH and GARCH. $\endgroup$
    – ALEXANDER
    Dec 28, 2013 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.