-
Notifications
You must be signed in to change notification settings - Fork 261
python-math #26: feat(math): Q11 — replicate vectorz distance cancellation in legacy kmeans #2654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jucor
wants to merge
1
commit into
spr/edge/00d8bc52
Choose a base branch
from
spr/edge/7ae9c688
base: spr/edge/00d8bc52
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| { | ||
| "dataset": "vw", | ||
| "schedule_id": "every-vote-56", | ||
| "cuts": { | ||
| "mode": "explicit-event-index", | ||
| "at": [ | ||
| 1, | ||
| 2, | ||
| 3, | ||
| 4, | ||
| 5, | ||
| 6, | ||
| 7, | ||
| 8, | ||
| 9, | ||
| 10, | ||
| 11, | ||
| 12, | ||
| 13, | ||
| 14, | ||
| 15, | ||
| 16, | ||
| 17, | ||
| 18, | ||
| 19, | ||
| 20, | ||
| 21, | ||
| 22, | ||
| 23, | ||
| 24, | ||
| 25, | ||
| 26, | ||
| 27, | ||
| 28, | ||
| 29, | ||
| 30, | ||
| 31, | ||
| 32, | ||
| 33, | ||
| 34, | ||
| 35, | ||
| 36, | ||
| 37, | ||
| 38, | ||
| 39, | ||
| 40, | ||
| 41, | ||
| 42, | ||
| 43, | ||
| 44, | ||
| 45, | ||
| 46, | ||
| 47, | ||
| 48, | ||
| 49, | ||
| 50, | ||
| 51, | ||
| 52, | ||
| 53, | ||
| 54, | ||
| 55, | ||
| 56 | ||
| ] | ||
| }, | ||
| "moderation": "none", | ||
| "notes": "every-vote PREFIX (56 single-vote steps): maximal-density warm-start edge case, truncated BEFORE the first Q11 knife-edge (step 57's near-coincident pair, whose merge/no-merge decision is bit-chaotic and irreducible cross-language \u2014 CLOJURE_QUIRKS.md Q11, journal 2026-07-22)" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| ;; Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| (ns proj-probe | ||
| "Diagnostic probe (R1 goal, every-vote step-57 uniqify edge): replay the | ||
| first N votes of a dataset through conv-update and print the FULL-PRECISION | ||
| projection rows for selected pids at the final step. | ||
|
|
||
| DELIBERATELY a separate file from replay.clj: the certify clj-recording | ||
| cache manifests hash dev/replay.clj, so touching that file forces a full | ||
| battery re-record. This probe reuses replay's own loaders via `load-file` | ||
| and changes nothing. | ||
|
|
||
| Usage (from math/): | ||
| clojure -M dev/proj_probe.clj <votes-csv> <n-votes> <pid> [<pid> ...]" | ||
| (:require [polismath.math.conversation :as conv] | ||
| [polismath.math.clusters :as clusters] | ||
| [polismath.math.named-matrix :as nm] | ||
| [clojure.core.matrix :as matrix])) | ||
|
|
||
| (load-file "dev/replay.clj") | ||
|
|
||
| (defn -main [& args] | ||
| (let [[csv-path n-votes & pids] args | ||
| n-votes (Long/parseLong n-votes) | ||
| pids (set (map #(Long/parseLong %) pids)) | ||
| votes (->> (replay/read-votes-csv csv-path) | ||
| replay/build-dataset | ||
| (take n-votes)) | ||
| seed (-> (conv/new-conv) (assoc :zid 99999 :meta-tids #{})) | ||
| ;; Replay ONE VOTE PER UPDATE — the warm-start chain is path-dependent | ||
| ;; (PCA start-vectors thread tick to tick), so a cold single batch | ||
| ;; does NOT reproduce the every-vote recording's state. | ||
| conv' (reduce | ||
| (fn [c v] | ||
| (conv/conv-update c (replay/->conv-votes [v]) | ||
| replay/certify-conv-opts)) | ||
| seed | ||
| votes) | ||
| rownames (nm/rownames (:rating-mat conv')) | ||
| proj (:proj conv')] | ||
| (doseq [[pid row] (map vector rownames proj) | ||
| :when (contains? pids pid)] | ||
| (println (format "pid=%d proj=[%.17g %.17g]" | ||
| (long pid) | ||
| (double (first row)) | ||
| (double (second row))))) | ||
| (doseq [c (:base-clusters conv')] | ||
| (println (format "cluster id=%d members=%s center=[%.17g %.17g]" | ||
| (long (:id c)) | ||
| (pr-str (:members c)) | ||
| (double (first (:center c))) | ||
| (double (second (:center c)))))) | ||
| ;; Phase-by-phase clean-start replay: chain to n-1 votes (prev state), | ||
| ;; apply the final vote, then walk clean-start-clusters manually on the | ||
| ;; new projection nmat with the PREV clusters, printing each phase. | ||
| (let [prev (reduce | ||
| (fn [c v] | ||
| (conv/conv-update c (replay/->conv-votes [v]) | ||
| replay/certify-conv-opts)) | ||
| (-> (conv/new-conv) (assoc :zid 99999 :meta-tids #{})) | ||
| (butlast votes)) | ||
| cur (conv/conv-update prev (replay/->conv-votes [(last votes)]) | ||
| replay/certify-conv-opts) | ||
| pnmat (nm/named-matrix (nm/rownames (:rating-mat cur)) ["x" "y"] | ||
| (:proj cur)) | ||
| inmat (nm/rowname-subset pnmat (:in-conv cur)) | ||
| rec (clusters/safe-recenter-clusters inmat (:base-clusters prev)) | ||
| uniq (clusters/uniqify-clusters rec) | ||
| possible (min 100 (count (distinct (into [] (matrix/rows (nm/get-matrix inmat))))))] | ||
| (println "PHASE safe-recenter:") | ||
| (doseq [c rec] | ||
| (println (format " id=%d members=%s center=[%.17g %.17g]" | ||
| (long (:id c)) (pr-str (:members c)) | ||
| (double (first (:center c))) (double (second (:center c)))))) | ||
| (println (format "PHASE uniqify: %d clusters (ids %s)" | ||
| (count uniq) (pr-str (mapv :id uniq)))) | ||
| (println (format "PHASE possible-clusters: %d (rows %d)" | ||
| possible (count (nm/rownames inmat)))) | ||
| (let [km (clusters/kmeans inmat 100 | ||
| :last-clusters (:base-clusters prev) | ||
| :max-iters 100)] | ||
| (println "PHASE full-kmeans:") | ||
| (doseq [c (sort-by :id km)] | ||
| (println (format " id=%d members=%s" | ||
| (long (:id c)) (pr-str (:members c)))))) | ||
| (let [cs (clusters/clean-start-clusters inmat (:base-clusters prev) 100) | ||
| data-iter (map vector (nm/rownames inmat) | ||
| (matrix/rows (matrix/matrix (nm/get-matrix inmat)))) | ||
| s1 (clusters/cluster-step data-iter 100 cs)] | ||
| (println "PHASE clean-start-direct:" (pr-str (mapv (juxt :id :members) cs))) | ||
| (println "PHASE cluster-step-1:" (pr-str (mapv (juxt :id :members) s1))) | ||
| (let [c6 (first (filter #(= 6 (:id %)) cs)) | ||
| c8 (first (filter #(= 8 (:id %)) cs)) | ||
| row5 (nm/get-row-by-name inmat 5)] | ||
| (println (format "PHASE centers: c6=[%.17g %.17g] c8=[%.17g %.17g]" | ||
| (double (first (:center c6))) (double (second (:center c6))) | ||
| (double (first (:center c8))) (double (second (:center c8))))) | ||
| (println (format "PHASE dist: d(p5,c6)=%.20g d(p5,c8)=%.20g equal=%s" | ||
| (double (matrix/distance row5 (:center c6))) | ||
| (double (matrix/distance row5 (:center c8))) | ||
| (= (matrix/distance row5 (:center c6)) | ||
| (matrix/distance row5 (:center c8))))) | ||
| (let [cleared (clusters/cleared-clusters cs) | ||
| after (clusters/add-to-closest cleared [5 row5]) | ||
| winner (first (filter (fn [[_ c]] (seq (:members c))) after))] | ||
| (println "PHASE single-assign: pid 5 ->" (pr-str (key winner)) | ||
| "map-type:" (str (type cleared)) | ||
| "entry-order:" (pr-str (keys cleared))) | ||
| (doseq [[cid c] cleared] | ||
| (println (format " d(p5, c%s)=%.20g" | ||
| (str cid) | ||
| (double (matrix/distance row5 (:center c)))))) | ||
| (let [names (nm/rownames inmat) | ||
| rows (into [] (matrix/rows (matrix/matrix (nm/get-matrix inmat)))) | ||
| mism (for [[n r] (map vector names rows) | ||
| :when (not= (into [] r) | ||
| (into [] (nm/get-row-by-name inmat n)))] | ||
| n)] | ||
| (println "PHASE alignment: rownames=" (pr-str names) | ||
| "misaligned-names=" (pr-str (vec mism))) | ||
| (let [view5 (nth rows 4)] | ||
| (println (format "PHASE view-dist: d(view5,c6)=%.20g d(view5,c8)=%.20g types=%s/%s" | ||
| (double (matrix/distance view5 (:center c6))) | ||
| (double (matrix/distance view5 (:center c8))) | ||
| (str (type view5)) (str (type (:center c6))))) | ||
| (let [after (clusters/add-to-closest | ||
| (clusters/cleared-clusters cs) [5 view5]) | ||
| winner (first (filter (fn [[_ c]] (seq (:members c))) after))] | ||
| (println "PHASE view-assign: pid 5 ->" (pr-str (key winner)))))))))))) | ||
|
|
||
| (apply -main *command-line-args*) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.