(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of 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[ 10534, 312]*) (*NotebookOutlinePosition[ 11425, 343]*) (* CellTagsIndexPosition[ 11381, 339]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData["About"], "Section", Evaluatable->False], Cell["\<\ A program written for the maximum likelihood estimation of marginal models. Literature: W.P.Bergsma, \"Marginal Models for Categorical Data\", Tilburg University \ Press, Tilburg, Holland. This program was written at the department of methodology of the faculty of \ social and behavioural sciences of Tilburg University. For more information the author can be contacted at: W.P.Bergsma@KUB.NL \ \>", "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell["A maximum likelihood algorithm for marginal models", "Section", Evaluatable->False], Cell["\<\ Input of MLE: n - vector of observations start - starting values for frequencies g - Link function zeta - vector of measures Zt - derivative matrix of zeta Mt - Matrix producing marginals X - Design matrix of marginal model Output of MLE: Maximum likelihood estimates of m \ \>", "Text", Evaluatable->False], Cell[BoxData[{\(Clear[MLE]\), RowBox[{\(MLE[n_, start_, g_, zeta_, Zt_, Mt_, X_]\), ":=", RowBox[{"Module", "[", "\n", "\t\t", RowBox[{ \({MaxStepSize = 1, \n\t\t\tMinStepSize = .1, \n\t\t\t MaxError = 1\/10.\^10, \n\t\t\tMaxIterations = 100, \n\t\t\tdg, G, z, Ut, v, m, step, error, iterate}\), ",", "\n", "\t\t", RowBox[{\(Ut = NullSpace[Transpose[X]]\), ";", "\n", "\t\t", RowBox[{\(v[m_, step_]\), ":=", RowBox[{\(v[m, step]\), "=", RowBox[{"Module", "[", RowBox[{ \({mu, hm, Htm, Hm, lambda}\), ",", "\n", "\t\t\t\t\t", RowBox[{ \(mu = Mt.m\), ";", "\n", "\t\t\t\t\t", \(hm = Ut.g[zeta[mu]]\), ";", "\n", "\t\t\t\t\t", RowBox[{"Htm", "=", RowBox[{\((m\ #1&)\), "/@", RowBox[{"(", RowBox[{"Ut", ".", RowBox[{"(", RowBox[{ RowBox[{ SuperscriptBox["g", "\[Prime]", MultilineFunction->None], "[", \(zeta[mu]\), "]"}], " ", \(Zt[mu].Mt\)}], ")"}]}], ")"}]}]}], ";", "\n", "\t\t\t\t\t", \(Hm = Transpose[Htm]\), ";", "\n", "\t\t\t\t\t", \(lambda = \(-Inverse[Htm.\(Hm\/m\)]. \((Htm.\((n\/m - 1)\) + hm)\)\)\), ";", "\n", "\t\t\t\t\t", \(Log[m] + \(step\ \((n - m + Hm.lambda)\)\)\/m\)}]}], "]"}]}]}], ";", "\n", "\t\t", \(error[m_] := \((v[m, 1] - Log[m])\).\((m\ \((v[m, 1] - Log[m])\))\)\), ";", "\n", "\t\t", \(iterate[m_, step_: MaxStepSize] := Module[{newm}, \n\t\t\t\tnewm = Exp[v[m, step]]; \n\t\t\t\t Print[N[step], "\< \>", error[newm]]; \n\t\t\t\t Which[\n\t\t\t\t\tstep < MinStepSize, Exp[v[m, MaxStepSize]], \n\t\t\t\t\terror[newm] > error[m], iterate[m, step\/2], \n \t\t\t\t\tTrue, newm]]\), ";", "\n", "\t\t", \(Print["\"]\), ";", "\n", "\t\t", \(FixedPoint[iterate, start, MaxIterations, SameTest \[Rule] \((error[#1] < MaxError&)\)]\)}]}], "]"}]}]}], "Input", PageWidth->Infinity, InitializationCell->True, FontFamily->"Courier New", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]] }, Closed]], Cell[CellGroupData[{ Cell["Definition of t-functions", "Section", Evaluatable->False], Cell[BoxData[{ \(\(t[pi_, {a_, c_, 0}] := pi; \)\), \(\(t[pi_, {a_, c_, i_}] := Exp[c[i - 1].Log[a[i - 1].t[pi, {a, c, i - 1}]]]; \)\), \(\(T[pi_, {a_, c_, 0}] := IdentityMatrix[Length[pi]]; \)\), \(T[pi_, {a_, c_, i_}] := t[pi, {a, c, i}]\ c[i - 1]. \(a[i - 1].T[pi, {a, c, i - 1}]\/a[i - 1].t[pi, {a, c, i - 1}] \)\)}], "Input", PageWidth->Infinity, InitializationCell->True, FontFamily->"Courier New", FontSize->12, FontWeight->"Bold", FontColor->GrayLevel[0], Background->GrayLevel[1]] }, Closed]], Cell[CellGroupData[{ Cell["Example: testing gamma=0 for 2x3 table", "Section", Evaluatable->False], Cell[CellGroupData[{ Cell[BoxData[{ \(at[0] = {\n\t\t{1, 0, 0, 0, 0, 0}, \n\t\t{0, 1, 0, 0, 0, 0}, \n \t\t{0, 0, 0, 0, 1, 1}, \n\t\t{0, 0, 0, 0, 0, 1}, \n \t\t{0, 1, 0, 0, 0, 0}, \n\t\t{0, 0, 1, 0, 0, 0}, \n \t\t{0, 0, 0, 1, 0, 0}, \n\t\t{0, 0, 0, 1, 1, 0}}; \n ct[0] = {\n\t\t{1, 0, 1, 0, 0, 0, 0, 0}, \n\t\t{0, 1, 0, 1, 0, 0, 0, 0}, \n\t\t{0, 0, 0, 0, 1, 0, 1, 0}, \n\t\t{0, 0, 0, 0, 0, 1, 0, 1}}; \n \(at[1] = 2\ {{1, 0}, {0, 1}, {1, 1}}.{{1, 1, 0, 0}, {0, 0, 1, 1}}; \)\), \(\(ct[1] = {{1, 0, \(-1\)}, {0, 1, \(-1\)}}; \)\), \(\(et = {{1, \(-1\)}}; \)\), \(\(n = N[{1, 2, 3, 4, 5, 6}]; \)\), \(\(zeta2[n_] := et.t[n, {at, ct, 2}]; \)\), \(\(Zt2[n_] := et.T[n, {at, ct, 2}]; \)\), \(\(loghyperbolic[z_] := Log[\(1 + z\)\/\(1 - z\)]; \)\), \(\(X = {{0}}; \)\), \(\(Mt = IdentityMatrix[6]; \)\), \(MLE[n, n, loghyperbolic, zeta2, Zt2, Mt, X]\)}], "Input", PageWidth->Infinity], Cell[BoxData[ \("stepsize, error :"\)], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]0.00222549857254257155`\), SequenceForm[ 1.0, " ", .0022254985725425713], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]8.09386048128868118`*^-6\), SequenceForm[ 1.0, " ", .80938604812886805*^-5], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]1.56301357381468797`*^-7\), SequenceForm[ 1.0, " ", .15630135738146879*^-6], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]3.60841267813646027`*^-9\), SequenceForm[ 1.0, " ", .36084126781364596*^-8], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]9.3123011693237423`*^-11\), SequenceForm[ 1.0, " ", .93123011693237425*^-10], Editable->False]], "Print"], Cell[BoxData[ InterpretationBox[ \(1.`\[InvisibleSpace]" "\[InvisibleSpace]2.54790586372922778`*^-12\), SequenceForm[ 1.0, " ", .25479058637292278*^-11], Editable->False]], "Print"], Cell[BoxData[ \({1.34086824501585888`, 2.13770084507972057`, 2.52143090994219098`, 3.62284158478072892`, 4.87149462998023086`, 6.50566378524783228`}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Example: Marginal Homogeneity for a 3x3 table", "Section", Evaluatable->False], Cell[CellGroupData[{ Cell[BoxData[{ \(Clear[ct, at, zeta1, Zt1]\), \(\(n = N[{1, 2, 3, 4, 5, 6, 7, 8, 9}]; \)\), \(\(at[0] = IdentityMatrix[6]; \)\), \(ct[0] = {\n\t\t{1, \(-1\), 0, 0, 0, 0}, \n\t\t{0, 1, \(-1\), 0, 0, 0}, \n\t\t{0, 0, 0, 1, \(-1\), 0}, \n\t\t{0, 0, 0, 0, 1, \(-1\)}}; \n Mt = {\n\t\t{1, 1, 1, 0, 0, 0, 0, 0, 0}, \n \t\t{0, 0, 0, 1, 1, 1, 0, 0, 0}, \n\t\t{0, 0, 0, 0, 0, 0, 1, 1, 1}, \n \t\t{1, 0, 0, 1, 0, 0, 1, 0, 0}, \n\t\t{0, 1, 0, 0, 1, 0, 0, 1, 0}, \n \t\t{0, 0, 1, 0, 0, 1, 0, 0, 1}}; \n \(X = {{1, 0}, {0, 1}, {1, 0}, {0, 1}}; \)\), \(\(zeta1[mu_] := t[mu, {at, ct, 1}]; \)\), \(\(Zt1[mu_] := T[mu, {at, ct, 1}]; \)\), \(MLE[n, n, Log, zeta1, Zt1, Mt, X]\)}], "Input", PageWidth->Infinity], Cell[OutputFormData["\<\ No Input Form Generated \ \>", "\<\ stepsize, error : 1. 0.0877674 1. 0.000578635 1. 0.0000150872 -6 1. 1.46749 10 -7 1. 1.42911 10 -8 1. 1.39155 10 -9 1. 1.35488 10 -10 1. 1.31921 10 -11 1. 1.28451 10 -12 1. 1.25076 10\ \>"], "Print", PageWidth->Infinity, Evaluatable->False], Cell[OutputFormData["\<\ {0.999999999998444, 2.838084206072946, 5.177575964664965, 3.088090172375807, 4.999999999999997, 6.859336846116551, 4.927569998357489, 7.109342812421576, \ 8.99999999999865} \ \>", "\<\ {1., 2.83808, 5.17758, 3.08809, 5., 6.85934, 4.92757, 7.10934, 9.}\ \>"], "Output", PageWidth->Infinity, Evaluatable->False] }, Open ]] }, Closed]] }, FrontEndVersion->"Microsoft Windows 3.0", ScreenRectangle->{{0, 1024}, {0, 690}}, AutoGeneratedPackage->None, WindowToolbars->{}, CellGrouping->Manual, WindowSize->{952, 629}, WindowMargins->{{-5, Automatic}, {Automatic, 5}}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False} ] (*********************************************************************** 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[1731, 51, 56, 1, 83, "Section"], Cell[1790, 54, 444, 14, 297, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[2271, 73, 91, 1, 48, "Section"], Cell[2365, 76, 393, 16, 372, "Text"], Cell[2761, 94, 2774, 53, 940, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[5572, 152, 66, 1, 48, "Section"], Cell[5641, 155, 573, 16, 182, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[6251, 176, 79, 1, 48, "Section"], Cell[CellGroupData[{ Cell[6355, 181, 951, 18, 715, "Input"], Cell[7309, 201, 52, 1, 37, "Print"], Cell[7364, 204, 202, 4, 37, "Print"], Cell[7569, 210, 205, 4, 38, "Print"], Cell[7777, 216, 205, 4, 38, "Print"], Cell[7985, 222, 205, 4, 38, "Print"], Cell[8193, 228, 206, 4, 38, "Print"], Cell[8402, 234, 214, 5, 38, "Print"], Cell[8619, 241, 176, 3, 44, "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell[8844, 250, 86, 1, 48, "Section"], Cell[CellGroupData[{ Cell[8955, 255, 770, 14, 549, "Input"], Cell[9728, 271, 430, 24, 306, "Print"], Cell[10161, 297, 345, 11, 41, "Output"] }, Open ]] }, Closed]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)