{
  "schemaVersion": 1,
  "query": "21_segment_themes",
  "queryId": "2cbb5a71-fef2-41fd-8b33-6b030a101ee9",
  "sql": "-- Fit one query-local KMeans model, then rank each fitted group's mean category shares.\nWITH assignments AS (\n    SELECT id, cluster_id\n    FROM cuvs_kmeans(\n        input => (\n            SELECT\n                id,\n                COALESCE(women, CAST(0 AS REAL)) AS women,\n                COALESCE(toys, CAST(0 AS REAL)) AS toys,\n                COALESCE(kids, CAST(0 AS REAL)) AS kids,\n                COALESCE(men, CAST(0 AS REAL)) AS men,\n                COALESCE(home, CAST(0 AS REAL)) AS home,\n                COALESCE(electronics, CAST(0 AS REAL)) AS electronics,\n                COALESCE(beauty, CAST(0 AS REAL)) AS beauty,\n                COALESCE(vintage, CAST(0 AS REAL)) AS vintage,\n                COALESCE(books, CAST(0 AS REAL)) AS books,\n                COALESCE(sports, CAST(0 AS REAL)) AS sports,\n                COALESCE(other, CAST(0 AS REAL)) AS other,\n                COALESCE(handmade, CAST(0 AS REAL)) AS handmade,\n                COALESCE(garden, CAST(0 AS REAL)) AS garden,\n                COALESCE(arts, CAST(0 AS REAL)) AS arts,\n                COALESCE(pets, CAST(0 AS REAL)) AS pets,\n                COALESCE(office, CAST(0 AS REAL)) AS office,\n                COALESCE(tools, CAST(0 AS REAL)) AS tools\n            FROM mr_tastes\n            ORDER BY id\n        ),\n        n_clusters => 8,\n        n_init => 5\n    )\n), profiles AS (\n    SELECT\n        a.cluster_id AS id,\n        COALESCE(CAST(AVG(COALESCE(t.women, CAST(0 AS REAL)) * COALESCE(t.women, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS women,\n        COALESCE(CAST(AVG(COALESCE(t.toys, CAST(0 AS REAL)) * COALESCE(t.toys, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS toys,\n        COALESCE(CAST(AVG(COALESCE(t.kids, CAST(0 AS REAL)) * COALESCE(t.kids, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS kids,\n        COALESCE(CAST(AVG(COALESCE(t.men, CAST(0 AS REAL)) * COALESCE(t.men, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS men,\n        COALESCE(CAST(AVG(COALESCE(t.home, CAST(0 AS REAL)) * COALESCE(t.home, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS home,\n        COALESCE(CAST(AVG(COALESCE(t.electronics, CAST(0 AS REAL)) * COALESCE(t.electronics, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS electronics,\n        COALESCE(CAST(AVG(COALESCE(t.beauty, CAST(0 AS REAL)) * COALESCE(t.beauty, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS beauty,\n        COALESCE(CAST(AVG(COALESCE(t.vintage, CAST(0 AS REAL)) * COALESCE(t.vintage, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS vintage,\n        COALESCE(CAST(AVG(COALESCE(t.books, CAST(0 AS REAL)) * COALESCE(t.books, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS books,\n        COALESCE(CAST(AVG(COALESCE(t.sports, CAST(0 AS REAL)) * COALESCE(t.sports, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS sports,\n        COALESCE(CAST(AVG(COALESCE(t.other, CAST(0 AS REAL)) * COALESCE(t.other, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS other,\n        COALESCE(CAST(AVG(COALESCE(t.handmade, CAST(0 AS REAL)) * COALESCE(t.handmade, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS handmade,\n        COALESCE(CAST(AVG(COALESCE(t.garden, CAST(0 AS REAL)) * COALESCE(t.garden, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS garden,\n        COALESCE(CAST(AVG(COALESCE(t.arts, CAST(0 AS REAL)) * COALESCE(t.arts, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS arts,\n        COALESCE(CAST(AVG(COALESCE(t.pets, CAST(0 AS REAL)) * COALESCE(t.pets, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS pets,\n        COALESCE(CAST(AVG(COALESCE(t.office, CAST(0 AS REAL)) * COALESCE(t.office, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS office,\n        COALESCE(CAST(AVG(COALESCE(t.tools, CAST(0 AS REAL)) * COALESCE(t.tools, CAST(0 AS REAL))) AS REAL), CAST(0 AS REAL)) AS tools\n    FROM assignments a\n    JOIN mr_tastes t ON t.id = a.id\n    GROUP BY a.cluster_id\n)\nSELECT\n    ranked.id AS cluster_id,\n    ranked.rank,\n    CASE ranked.position\n        WHEN 0 THEN 'Women'\n        WHEN 1 THEN 'Toys & Collectibles'\n        WHEN 2 THEN 'Kids'\n        WHEN 3 THEN 'Men'\n        WHEN 4 THEN 'Home'\n        WHEN 5 THEN 'Electronics'\n        WHEN 6 THEN 'Beauty'\n        WHEN 7 THEN 'Vintage & collectibles'\n        WHEN 8 THEN 'Books'\n        WHEN 9 THEN 'Sports & outdoors'\n        WHEN 10 THEN 'Other'\n        WHEN 11 THEN 'Handmade'\n        WHEN 12 THEN 'Garden & Outdoor'\n        WHEN 13 THEN 'Arts & Crafts'\n        WHEN 14 THEN 'Pet Supplies'\n        WHEN 15 THEN 'Office'\n        WHEN 16 THEN 'Tools'\n    END AS category,\n    ranked.value AS mean_view_share\nFROM cuvs_top_k(\n    input => (\n        SELECT id, women, toys, kids, men, home, electronics, beauty, vintage,\n               books, sports, other, handmade, garden, arts, pets, office, tools\n        FROM profiles\n        ORDER BY id\n    ),\n    k => 3,\n    select => 'max'\n) ranked\nORDER BY cluster_id, rank;\n",
  "schema": {
    "text": "cluster_id: int32 not null\nrank: uint32 not null\ncategory: string\nmean_view_share: float not null",
    "fields": [
      {
        "name": "cluster_id",
        "type": "int32",
        "nullable": false
      },
      {
        "name": "rank",
        "type": "uint32",
        "nullable": false
      },
      {
        "name": "category",
        "type": "string",
        "nullable": true
      },
      {
        "name": "mean_view_share",
        "type": "float",
        "nullable": false
      }
    ]
  },
  "population": {
    "users": 1919,
    "minimumItemViewEvents": 20,
    "eventType": "item_view",
    "cutoffExclusive": "2023-10-21",
    "featureTransform": "sqrt(view count by category / total views)",
    "featureColumns": [
      "women",
      "toys",
      "kids",
      "men",
      "home",
      "electronics",
      "beauty",
      "vintage",
      "books",
      "sports",
      "other",
      "handmade",
      "garden",
      "arts",
      "pets",
      "office",
      "tools"
    ],
    "categories": [
      "Women",
      "Toys & Collectibles",
      "Kids",
      "Men",
      "Home",
      "Electronics",
      "Beauty",
      "Vintage & collectibles",
      "Books",
      "Sports & outdoors",
      "Other",
      "Handmade",
      "Garden & Outdoor",
      "Arts & Crafts",
      "Pet Supplies",
      "Office",
      "Tools"
    ]
  },
  "method": {
    "algorithm": "cuvs_kmeans",
    "clusters": 8,
    "nInit": 5,
    "profileAggregation": "SQL squares each stored square-root share, then averages across users in each query-local cluster",
    "selection": "cuvs_top_k with k=3 and select=max",
    "shareMeaning": "Each mean_view_share is an equal-user mean of that user's share of item_view events in the category. It is neither a share of all cluster events nor a proportion of users."
  },
  "execution": {
    "outcome": "succeeded",
    "disposition": "native",
    "serverReportedElapsedMs": 246,
    "serverReportedElapsedBoundary": "Server query statistics field elapsed_ms.",
    "clientFlightElapsedSeconds": 0.25340579298790544,
    "clientFlightElapsedBoundary": "Client Flight elapsed_s includes planning and receipt of the full result; preparation is excluded.",
    "timingContext": "Both values are single observations from a shared server. Concurrent server activity was not controlled; neither value is an isolated benchmark.",
    "nativePlanFragments": 5,
    "hostDataFusionBoundaries": 0,
    "postNativeExecutionPath": "native"
  },
  "crossFitConsistency": {
    "status": "passed",
    "outputClusterToSavedCluster": {
      "0": 0,
      "1": 1,
      "2": 2,
      "3": 3,
      "4": 4,
      "5": 5,
      "6": 6,
      "7": 7
    },
    "maxTopThreeMeanAbsoluteError": 0.0,
    "comparisonScope": "cross-fit consistency against saved cuvs_kmeans assignments, invariant to cluster-label permutation",
    "doesNotAssertPerUserAssignmentIdentity": true,
    "interpretation": "a match supports the captured profile table only"
  },
  "rows": [
    {
      "cluster_id": 0,
      "rank": 1,
      "category": "Men",
      "mean_view_share": 0.6230103373527527
    },
    {
      "cluster_id": 0,
      "rank": 2,
      "category": "Women",
      "mean_view_share": 0.15120576322078705
    },
    {
      "cluster_id": 0,
      "rank": 3,
      "category": "Sports & outdoors",
      "mean_view_share": 0.050094716250896454
    },
    {
      "cluster_id": 1,
      "rank": 1,
      "category": "Toys & Collectibles",
      "mean_view_share": 0.7978272438049316
    },
    {
      "cluster_id": 1,
      "rank": 2,
      "category": "Electronics",
      "mean_view_share": 0.04546361416578293
    },
    {
      "cluster_id": 1,
      "rank": 3,
      "category": "Women",
      "mean_view_share": 0.0269844401627779
    },
    {
      "cluster_id": 2,
      "rank": 1,
      "category": "Women",
      "mean_view_share": 0.7703091502189636
    },
    {
      "cluster_id": 2,
      "rank": 2,
      "category": "Men",
      "mean_view_share": 0.051171671599149704
    },
    {
      "cluster_id": 2,
      "rank": 3,
      "category": "Home",
      "mean_view_share": 0.028670791536569595
    },
    {
      "cluster_id": 3,
      "rank": 1,
      "category": "Electronics",
      "mean_view_share": 0.676618754863739
    },
    {
      "cluster_id": 3,
      "rank": 2,
      "category": "Toys & Collectibles",
      "mean_view_share": 0.06404487788677216
    },
    {
      "cluster_id": 3,
      "rank": 3,
      "category": "Other",
      "mean_view_share": 0.04465782642364502
    },
    {
      "cluster_id": 4,
      "rank": 1,
      "category": "Toys & Collectibles",
      "mean_view_share": 0.22422800958156586
    },
    {
      "cluster_id": 4,
      "rank": 2,
      "category": "Women",
      "mean_view_share": 0.18196500837802887
    },
    {
      "cluster_id": 4,
      "rank": 3,
      "category": "Books",
      "mean_view_share": 0.12039686739444733
    },
    {
      "cluster_id": 5,
      "rank": 1,
      "category": "Kids",
      "mean_view_share": 0.6969141960144043
    },
    {
      "cluster_id": 5,
      "rank": 2,
      "category": "Women",
      "mean_view_share": 0.1354818493127823
    },
    {
      "cluster_id": 5,
      "rank": 3,
      "category": "Toys & Collectibles",
      "mean_view_share": 0.05455411225557327
    },
    {
      "cluster_id": 6,
      "rank": 1,
      "category": "Home",
      "mean_view_share": 0.4627987742424011
    },
    {
      "cluster_id": 6,
      "rank": 2,
      "category": "Women",
      "mean_view_share": 0.15464423596858978
    },
    {
      "cluster_id": 6,
      "rank": 3,
      "category": "Vintage & collectibles",
      "mean_view_share": 0.13185392320156097
    },
    {
      "cluster_id": 7,
      "rank": 1,
      "category": "Beauty",
      "mean_view_share": 0.5550774335861206
    },
    {
      "cluster_id": 7,
      "rank": 2,
      "category": "Women",
      "mean_view_share": 0.22349148988723755
    },
    {
      "cluster_id": 7,
      "rank": 3,
      "category": "Home",
      "mean_view_share": 0.05518219619989395
    }
  ],
  "sourceSha256": {
    "docs/research/merrec/sql/21_segment_themes.sql": "c6deed773892ba4f683555ebd0e083913a7923ab63c9791d26d23a8b89d7bf99",
    "docs/research/merrec/results/21_segment_themes.json": "47d24248aa5ce67a4faf4fb61b5d544d01b82331ff77fdda09682a2d5fc33e25",
    "target/merrec-exploration/results/21_segment_themes.parquet": "5d42f0c065990bf65b03c1979d4515ef100b4bd3441234251f0b86ea25bbec99",
    "docs/research/merrec/results/21_segment_themes.stats.txt.gz": "6e4ba86d33eaed0795157eadf382bf75902528df4929c4947a9cabda57b7c23f",
    "docs/research/merrec/features.json": "1a072620e3229e76c0c5066e429354ffa81a947136bbb90fa59d0e92759ca33d",
    "docs/research/merrec/composition_validation.json": "43c76c4e7bea7700a28b628d4daa913074ed2dc59f6b2fbc452a28373c87d611",
    "target/merrec-exploration/results/prepare_tastes.parquet": "9d22936806817d9ced10dfb8f08433d0197334d3a0a7aec5eb3e6c7343647b18",
    "target/merrec-exploration/results/cuvs_kmeans.parquet": "5b3ee7f231f77e41428439d46394fba83d0796baa307f3e13c92cf5be39167cc"
  }
}
