{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# The textbook tables of a factorial: Yates's algorithm and three-level components\n", "\n", "A textbook that analyzes a factorial by hand prints its working as tables: the columns of Yates's\n", "algorithm with the checks on them, and the components of an interaction of three-level factors.\n", "The functions on this page return those tables, so each step can be set beside the book and\n", "checked. For an analysis of variance with p-values, use `analyze_experiment`." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import itertools\n", "\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "from process_improve.experiments import interaction_components, reverse_yates, yates_table, yates_table_3k" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Yates's algorithm on a two-level factorial\n", "\n", "Box, Hunter and Hunter (1978, Table 10.5) work the algorithm on a pilot plant: a $2^3$ factorial in\n", "temperature T, concentration C and catalyst K, each response the average of two runs. Each pass\n", "writes the sums of successive pairs of the column before, then their differences, the second of\n", "each pair less the first. After three passes, column (3) holds the total and each effect's\n", "contrast; the effect is its contrast divided by 4 (by 8 for the mean), and its sum of squares the\n", "contrast squared divided by 8. The runs may come in any order: the table puts them in standard\n", "order, T changing fastest." ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "runs = pd.DataFrame(\n", " {\n", " \"T\": [-1, 1, -1, 1, -1, 1, -1, 1],\n", " \"C\": [-1, -1, 1, 1, -1, -1, 1, 1],\n", " \"K\": [-1, -1, -1, -1, 1, 1, 1, 1],\n", " \"y\": [60, 72, 54, 68, 52, 83, 45, 80],\n", " }\n", ")\n", "table = yates_table(runs, \"y\")\n", "table" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "Each pass doubles the sum of squares of the column before it. The book uses this to check the\n", "arithmetic:" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "table.attrs[\"sum_of_squares\"]" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "The table's column (3) and its effects are those of the book's Table 10.5:" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "book_column_3 = [514, 92, -20, 6, 6, 40, 0, 2]\n", "book_effects = [64.25, 23.0, -5.0, 1.5, 1.5, 10.0, 0.0, 0.5]\n", "table[\"(3)\"].tolist() == book_column_3, table[\"estimate\"].tolist() == book_effects" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "## Fitted values by the algorithm run in reverse\n", "\n", "Only T, C and the TK interaction stand out, and Box, Hunter and Hunter (Appendix 10A) fit the model\n", "that keeps those three effects. With the other contrasts set to zero, the same passes, run on the\n", "column written from the bottom up, give eight times the fitted values in standard order:" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "fit = reverse_yates(table, keep=[\"T\", \"C\", \"T:K\"])\n", "fit[[\"T\", \"C\", \"K\", \"y\", \"effect\", \"kept\", \"fitted\", \"residual\"]]" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "The crude sum of squares of the fitted values and the sum of squares of the residuals add up to\n", "that of the responses:" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "squares = fit.attrs[\"sum_of_squares\"]\n", "squares[\"fitted\"], squares[\"residual\"], squares[\"fitted\"] + squares[\"residual\"]" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "## Yates's algorithm on a three-level factorial\n", "\n", "For a $3^k$ factorial each pass takes successive triples $(w_1, w_2, w_3)$ and writes their sums,\n", "their linear contrasts $w_3 - w_1$, then their quadratic contrasts $w_1 - 2w_2 + w_3$. After $k$\n", "passes each entry is $z'y$ for one product of the factors' orthogonal polynomials, the linear $x$\n", "and the quadratic $3x^2 - 2$, so each of the $3^k - 1$ effects has one degree of freedom. The\n", "effect's `estimate` is $z'y / z'z$, its coefficient in the least-squares fit, and `divisor` is\n", "$z'z$.\n", "\n", "Box and Draper (2007, Appendix 7G) work it on a textile experiment: the cycles to failure Y of\n", "worsted yarn in a $3^3$ factorial in the length of the test specimen x1, the amplitude of the load\n", "cycle x2 and the load x3. In the effect names, `x1^2` stands for the quadratic polynomial of x1." ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "cycles = [674, 1414, 3636, 338, 1022, 1568, 170, 442, 1140, 370, 1198, 3184, 266, 620]\n", "cycles += [1070, 118, 332, 884, 292, 634, 2000, 210, 438, 566, 90, 220, 360]\n", "settings = [(x1, x2, x3) for x3, x2, x1 in itertools.product((-1, 0, 1), repeat=3)]\n", "textile = pd.DataFrame(settings, columns=[\"x1\", \"x2\", \"x3\"]).assign(Y=cycles)\n", "table_3k = yates_table_3k(textile, \"Y\")\n", "table_3k.round(2)" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "Box and Draper number the response column (1), so their columns (2) to (4) of Table 7G.1 are the\n", "columns (1) to (3) here, and their columns (6), (7) and (8) are `divisor`, `estimate` and `sum_sq`.\n", "\n", "`standardized` is $z'y / (z'z)^{1/2}$, the signed square root of the sum of squares, and\n", "`normal_score` is the standard normal quantile at $(i - 1/2)/26$ of its rank $i$. Plotted against\n", "each other they make the normal plot of the effects (Box and Draper, Figure 7F.1): the effects that\n", "lie off the line through the small ones are those to look at. The largest, labeled, are the main\n", "effects of the three factors and the interaction of length and amplitude." ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "effects = table_3k.iloc[1:]\n", "fig, ax = plt.subplots(figsize=(6, 4))\n", "ax.scatter(effects[\"standardized\"], effects[\"normal_score\"], s=20, color=\"tab:blue\")\n", "for _, row in effects[effects[\"standardized\"].abs() > 1000].iterrows():\n", " left = row[\"standardized\"] > 0 # the label on the side away from the edge of the plot\n", " ax.annotate(\n", " row[\"effect\"],\n", " (row[\"standardized\"], row[\"normal_score\"]),\n", " xytext=(-6 if left else 6, -3),\n", " textcoords=\"offset points\",\n", " ha=\"right\" if left else \"left\",\n", " fontsize=8,\n", " )\n", "ax.set_xlabel(\"Standardized effect, $z'y / (z'z)^{1/2}$\")\n", "ax.set_ylabel(\"Normal score\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## The components of an interaction of three-level factors\n", "\n", "In a $3^k$ factorial the interaction of two factors has four degrees of freedom. Write each factor's\n", "levels 0, 1 and 2, and group the runs by $x_A + x_B$ and by $x_A + 2x_B$, modulo 3: the sums of\n", "squares of the three group totals are the interaction's two orthogonal components, AB and $AB^2$,\n", "of two degrees of freedom each (Yates's J and I). An interaction of three factors splits into four\n", "components, ABC, $ABC^2$, $AB^2C$ and $AB^2C^2$ (Yates's Z, Y, X and W).\n", "\n", "Montgomery (2013, section 9.2) splits the interaction of the tool angle A and the cutting speed B\n", "in his tool-life experiment (Example 5.5), a $3^2$ factorial run twice. A factor's levels are its\n", "three settings, lowest first:" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "tool_life = pd.DataFrame(\n", " {\n", " \"A\": [15] * 6 + [20] * 6 + [25] * 6,\n", " \"B\": [125, 125, 150, 150, 175, 175] * 3,\n", " \"y\": [-2, -1, -3, 0, 2, 3, 0, 2, 1, 3, 4, 6, -1, 0, 5, 6, 0, -1],\n", " }\n", ")\n", "components = interaction_components(tool_life, \"y\", [\"A\", \"B\"])\n", "components.round(2)" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "Montgomery prints 28.00 for $AB^2$ and 33.34 for AB, where the table gives 33.33: his group totals\n", "give $392/6 - 24^2/18 = 33.333$. The two components add up to the sum of squares of the\n", "interaction, on its four degrees of freedom:" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "interaction = components[components[\"effect\"] == \"AB\"]\n", "int(interaction[\"df\"].sum()), round(float(interaction[\"sum_of_squares\"].sum()), 2)" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "## The same tables as agent tools\n", "\n", "Both tables are tools an agent can call through the MCP server or `skills/doe-designer/`:\n", "`yates_table` takes a $2^k$ or a $3^k$ factorial, and with `keep` runs a $2^k$ in reverse;\n", "`interaction_components` takes the runs and the three-level factors. Each takes the runs as a list\n", "of records and returns the tables as records:" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "from process_improve.tool_spec import execute_tool_call\n", "\n", "request = {\"design_matrix\": runs.to_dict(\"records\"), \"response_column\": \"y\", \"keep\": [\"T\", \"C\", \"T:K\"]}\n", "result = execute_tool_call(\"yates_table\", request)\n", "[row[\"fitted\"] for row in result[\"reverse\"][\"table\"]]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }