(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 13769, 367]*) (*NotebookOutlinePosition[ 14660, 396]*) (* CellTagsIndexPosition[ 14616, 392]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Econometric Functions", "Title"], Cell[TextData[{ "A Warning Note!!!: ", StyleBox["Mathematica", FontSlant->"Italic"], "'s built-in ", StyleBox["Regress[]", FontWeight->"Bold"], " function assumes data in the form ", Cell[BoxData[ \(TraditionalForm \`{{\(x\_1\)\_1, \(x\_2\)\_1, ..., \(x\_k\)\_1, y\_1}, {\(x\_1\)\_2, \(x\_2\)\_2, ..., \(x\_k\)\_2, y\_2}, ..., {\(x\_1\)\_T, \(x\_2\)\_T, ..., \(x\_k\)\_T, y\_T}}\)]], " but the ", StyleBox["simpleLinRegress[ ]", FontWeight->"Bold"], " here and used in some of the other functions assumes data of the form: ", Cell[BoxData[ \(TraditionalForm \`{{y\_1, \(x\_1\)\_1, \(x\_2\)\_1, ..., \(x\_k\)\_1}, {y\_2, \(x\_1\)\_2, \(x\_2\)\_2, ..., \(x\_k\)\_2}, ..., {y\_T, \(x\_1\)\_T, \(x\_2\)\_T, ..., \(x\_k\)\_T}}\)]], ". This seemed to me to make more sense than the other presentation \ because we normally think of equations as reading from left to right with the \ regressand on the left-hand side and the regressors on the right. It is also \ in line with the data ordering used in packages like Shazam." }], "Text"], Cell[CellGroupData[{ Cell["\<\ simpleLinRegress[ ]: A Simple Linear Regression function built for \ speed not features\ \>", "Section"], Cell[CellGroupData[{ Cell[BoxData[ \(simpleLinRegress::usage = "\"\)], "Input"], Cell[BoxData[ \("simpleLinRegress[data_MatrixQ] takes a rectangular matrix of real \ numbers and performs a linear regression or weighted linear regression of the \ first column on the other columns. The output is a non-rectangular list of \ three lists -- the estimated coefficients, the fitted values and the \ residuals."\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(GLSWeights::usage\ = \ "\"\)], "Input"], Cell[BoxData[ \("GLSWeights is an option to simpleLinRegress that takes either the \ value Automatic, corresponding to OLS, or a list of real numbers \ corresponding to the weights to be used in a WLS procedure."\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(IncludeConstant::usage\ = \ "\"\)], "Input"], Cell[BoxData[ \("IncludeConstant is an option to simpleLinRegress that indicates \ whether or not a constant should be added to the data matrix."\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(simpleLinRegress::wtnum = "\"\)], "Input"], Cell[BoxData[ \("Either the vector of weights is not a vector of real numbers with the \ same number of element as the data sample size, or you have tried to apply \ WLS to a model with a single explanator and no constant."\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(simpleLinRegress::notnum\ = \ "\"\)], "Input"], Cell[BoxData[ \("Some of the data supplied is non-numeric."\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Options[simpleLinRegress] = {GLSWeights -> Automatic, IncludeConstant \[Rule] True}\)], "Input"], Cell[BoxData[ \({GLSWeights \[Rule] Automatic, IncludeConstant \[Rule] True}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(SetOptions[simpleLinRegress, GLSWeights -> Automatic, IncludeConstant \[Rule] True]\)], "Input"], Cell[BoxData[ \({GLSWeights \[Rule] Automatic, IncludeConstant \[Rule] True}\)], "Output"] }, Open ]], Cell[BoxData[ \(simpleLinRegress[data_?MatrixQ, \ opts___?OptionQ] := \ If[And\ @@ \ \((NumericQ\ /@ \ Flatten[data])\), \n\t\tModule[{\[ScriptW], \[ScriptC]}, \ \[ScriptW]\ = \ \(GLSWeights\ /. {opts}\)\ /. Options[simpleLinRegress]; \n\t\t\[ScriptC]\ = \ \ \(IncludeConstant\ /. {opts}\)\ /. Options[simpleLinRegress]; \n\t\tWith[{\[ScriptY] = If[Not[\[ScriptC]] && \ \(Dimensions[ data]\)\[LeftDoubleBracket]2\[RightDoubleBracket] == 2, Flatten[First /@ data], First\ /@ \ data], \n\t\t\t\t\t\t\t\ \ \[ScriptX]\ = \ If[\[ScriptC], Transpose[ Join[{Table[1, {Length[data]}]}, Transpose[Rest\ /@ \ data]]], If[\(Dimensions[ Rest\ /@ data]\)\[LeftDoubleBracket]2\[RightDoubleBracket] \ == 1, Flatten[Rest\ /@ \ data], Transpose[Rest\ /@ \ data]]], \[ScriptN] = Length[data]}, \n\t\t\tModule[{betas, yhat, \ resids}, \n\t\t\t\tWhich[\[ScriptW] === Automatic, \(betas\ = \ If[Not[\[ScriptC]] && \(Dimensions[ Rest\ /@ data]\)\[LeftDoubleBracket]2\ \[RightDoubleBracket] == 1, {\((1/\((\[ScriptX] . \[ScriptX])\))\)*\((\ \[ScriptX] . \[ScriptY])\)}, Flatten[ Inverse[\((Transpose[\ \[ScriptX]] . \ \[ScriptX])\)] \ . \((Transpose[\ \[ScriptX]] . \[ScriptY])\)]];\), \n\t\t\t\t\t\((Length[\ \[ScriptW]] == \[ScriptN]\ )\) && \((Length[\[ScriptW]] > 1\ )\) && \ \((And\ @@ \ \((NumericQ\ /@ \ \ \[ScriptW])\))\), \ \(betas\ = Inverse[ Transpose[\((\[ScriptW]*\[ScriptX])\)] . \ \((\[ScriptX])\)] . \((\((Transpose[\[ScriptW]*\[ScriptX]])\) . \ \((\[ScriptY])\))\);\), True, \ Message[ simpleLinRegress::wtnum]; \ $Failed]; \n\t\t\t\tyhat\ = If[Not[\[ScriptC]] && \ \(Dimensions[ data]\)\[LeftDoubleBracket]2\[RightDoubleBracket] == 2, \[ScriptX]* betas\[LeftDoubleBracket]1\[RightDoubleBracket], \ \ \[ScriptX] . betas]; \n\t\t\t\tresids\ = \ \[ScriptY] - yhat; \n\t\t\t\t{betas, yhat, resids}]]], Message[simpleLinRegress::notnum]]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["ARLagOrderSelectionTable[ ]", "Section"], Cell["\<\ This function has been updated thanks to helpful comments by Virgil \ Stokes.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(ARLagOrderSelectionTable::usage = \ "\"\)], "Input"], Cell[BoxData[ \("ARLagOrderSelectionTable[data,maxp] takes a time series (vector of \ real numbers) and generates a table of various Information Criteria used for \ selecting the optimal lag order in an AR model describing the time \ series."\)], "Output"] }, Open ]], Cell[BoxData[ \(ARLagOrderSelectionTable[data : {__Real}, \ maxp_Integer]\ /; \ \((maxp\ < \ Length[data])\)\ := \n\t Module[{\[Sigma]2, \[Beta]s\ , \ T = Length[data]}, \n\t\tTable[ With[{result = simpleLinRegress[ Transpose[ Table[Drop[RotateRight[data, k], j], \ {k, 0, j}]], IncludeConstant \[Rule] True]}, \[Sigma]2[j] = Plus\ @@ \ \((\((result\[LeftDoubleBracket]3\[RightDoubleBracket])\)^2)\); \[Beta]s[j] = First[result];], {j, maxp}]; \ \n\t Join[{{"\", \*"\"\<\!\(\[Sigma]\^2\)\>\"", "\", \ "\", "\", \ "\", "\"}}, Table[{i, \[Sigma]2[i], Log[\((\[Sigma]2[i])\)^2] + \ 2\ i\ /T, \ T*\ Log[\((\[Sigma]2[i])\)^2] + T\ \((1\ + \ i/T)\)/\((1 - \((i + 2)\)\ T)\), Log[\[Sigma]2[i]] + i\ Log[T]/T, \ Log[\[Sigma]2[i]]\ + \ 2\ i\ *Log[Log[T]]/T, {\[Beta]s[i]}}, {i, 1, maxp}]\ ]\ ]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["LjungBoxStatistic[ ]", "Section"], Cell[BoxData[ \(LjungBoxStatistic::usage = "\"\)], "Input"], Cell[BoxData[ \(Needs["\"]\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(ljungboxcompiledteststat = Compile[{{x, _Real, 1}, {k, _Integer}}, \ \[Sum]\+\(i = 1\)\%k\(( \((Plus@@\((Drop[x, \(-i\)]\[Times]Drop[x, i])\)/ \((\(\@Plus@@\((Drop[x, \(-i\)]\^2)\)\) \@Plus@@\((Drop[x, i]\^2)\))\))\)^2\ \(Length[x] \((Length[x] - 2)\)\)\/\(Length[x] - i\))\)]\)], "Input"], Cell[BoxData[ TagBox[ \(CompiledFunction[{x, k}, \[Sum]\+\(i = 1\)\%k \(\((Plus@@ \((Drop[x, \(-i\)]\ Drop[x, i]) \)\/\(\@Plus@@\(Drop[x, \(-i\)]\^2\)\ \@Plus@@\(Drop[x, i]\^2\)\))\)\^2\ \((Length[x]\ \((Length[x] - 2)\))\)\)\/\(Length[x] - i\), "-CompiledCode-"]\), False, Editable->False]], "Output"] }, Open ]], Cell[BoxData[ \(LjungBoxStatistic[data : {__Real}, k_Integer] := \n TableForm[ \t{{"\", "\<5% critical value\>"}, { ljungboxcompiledteststat[data, k], Quantile[ChiSquareDistribution[k], 0.95]}}]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["CochraneOrcutt[ ]", "Section"], Cell[CellGroupData[{ Cell[BoxData[ \(CochraneOrcutt::usage = "\"\)], "Input"], Cell[BoxData[ \("CochraneOrcutt[data] takes a matrix of real numbers and performs the \ Cochrane-Orcutt iterative procedure for estimating linear models in the \ presence of AR(1) serial correlation in the errors."\)], "Output"] }, Open ]], Cell["\<\ Needs[\"Statistics`NormalDistribution`\"]; Needs[\"Statistics`ContinuousDistributions`\"]; Needs[\"Statistics`DataManipulation`\"]; Needs[\"Statistics`DescriptiveStatistics`\"]; Needs[\"Statistics`MultiDescriptiveStatistics`\"]; Needs[\"Statistics`LinearRegression`\"]; Needs[\"Statistics`DataSmoothing`\"];\ \>", "Input", AspectRatioFixed->True], Cell[BoxData[ \(CochraneOrcutt[data_?MatrixQ] := \ FixedPoint[ With[{e = \(simpleLinRegress[#, IncludeConstant \[Rule] True] \)\[LeftDoubleBracket]3\[RightDoubleBracket]}, With[{rhohat = Rest[e].Drop[e, \(-1\)]/Plus@@\((Drop[e, \(-1\)]^2)\)}, Join[{\@\(1 - rhohat\^2\)\[Times]First[#]}, Rest[#] - rhohat\ Drop[#, \(-1\)]]]]&, data]\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"4.0 for Macintosh", ScreenRectangle->{{0, 1152}, {0, 850}}, WindowSize->{862, 789}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, CellLabelAutoDelete->True, Magnification->1.25, MacintoshSystemPageSetup->"\<\ 01h0001804P000000aP2@_ogooP3@09:8085N`?P0000005X0FP000003gP;BP5/ 038;BTLH05000@4100000BL?00400@000000000000000000000000000040I000 00000000000@X@804@000000001C>30`\>" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 38, 0, 122, "Title"], Cell[1780, 53, 1134, 26, 131, "Text"], Cell[CellGroupData[{ Cell[2939, 83, 114, 3, 84, "Section"], Cell[CellGroupData[{ Cell[3078, 90, 371, 5, 119, "Input"], Cell[3452, 97, 342, 5, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[3831, 107, 259, 4, 98, "Input"], Cell[4093, 113, 230, 3, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4360, 121, 197, 3, 77, "Input"], Cell[4560, 126, 163, 2, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4760, 133, 272, 4, 98, "Input"], Cell[5035, 139, 241, 3, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[5313, 147, 113, 2, 35, "Input"], Cell[5429, 151, 77, 1, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[5543, 157, 125, 2, 35, "Input"], Cell[5671, 161, 101, 2, 70, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[5809, 168, 123, 2, 35, "Input"], Cell[5935, 172, 101, 2, 70, "Output"] }, Open ]], Cell[6051, 177, 2583, 47, 392, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[8671, 229, 46, 0, 36, "Section"], Cell[8720, 231, 101, 3, 36, "Text"], Cell[CellGroupData[{ Cell[8846, 238, 458, 7, 147, "Input"], Cell[9307, 247, 260, 4, 71, "Output"] }, Open ]], Cell[9582, 254, 1108, 20, 244, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10727, 279, 39, 0, 62, "Section"], Cell[10769, 281, 311, 5, 109, "Input"], Cell[11083, 288, 76, 1, 33, "Input"], Cell[CellGroupData[{ Cell[11184, 293, 400, 8, 221, "Input"], Cell[11587, 303, 438, 11, 116, "Output"] }, Open ]], Cell[12040, 317, 262, 5, 71, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[12339, 327, 36, 0, 36, "Section"], Cell[CellGroupData[{ Cell[12400, 331, 266, 4, 90, "Input"], Cell[12669, 337, 232, 3, 71, "Output"] }, Open ]], Cell[12916, 343, 358, 9, 134, "Input"], Cell[13277, 354, 464, 9, 138, "Input"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)