Repository navigation
feat(engine): draw role-playing links jointly; match affinity per row #80
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
59b45da
chore(bench): baseline from bench run 45965246339
github-actions[bot] 00f9758
feat(bench,engine): drafted bench specs rewritten from documentation;…
claude 4b0f4da
feat(bench): log accepted drift with its reason (AdventureWorks DimPr…
claude ffec5fa
revert: chore(bench): baseline from bench run 45965246339
claude 8e5f79e
fix(engine): SCD2 moved starts stay on the source's later-version starts
claude 844f760
fix(bench): Berka spec from the PKDD'99 data description
claude ec92bd5
feat(bench): IBM AML (HI-Small) as a monthly benchmark dataset
claude 9fb2cb1
fix(bench): AML builder writes through the DuckDB relation API
claude 23fc8b4
feat(bench): IBM AML from the authors' Kaggle dataset; Bitcoin rule; …
claude bcd82a5
fix(bench): the Kaggle credential is the KAGGLE_API_TOKEN secret
claude 03f441b
fix(engine): rule-linked categorical joints, unique keys over stratif…
claude 22618b2
chore(bench): pin the SHA-256 of the Kaggle HI-Small_Trans.csv
claude 8936a48
fix(evaluate): count categorical join columns once in join_metrics
claude 9b9612a
fix(engine): test unique-key nulls with math.isnan (CodeQL comparison…
claude 428191a
fix(marginals): continuous columns with point masses keep their tails
claude 4256f8f
chore(bench): log the owner-accepted ACS privacy-distance and census …
claude 97b6c40
feat(marginals): point-mass knots as the setting marginals.point_mass…
claude 5a29399
fix(quality): fan-out checks of a sampled fact use its exact per-pare…
claude 56fa859
feat(engine): joint role-playing links; link-affinity matches as a ro…
claude 465e68a
fix(engine): exact derives and lookups that read a soft rule's column…
claude 9285927
Merge #79's branch (AML affinity order, soft-derive recompute)
claude ab2b5db
fix(engine): review findings on SCD recent entities, affinity under u…
claude ee32da3
fix(engine): joint links: calibrated dependence test, rare combinatio…
claude 722ab63
Merge branch 'claude/project-thread-ttjix4' of https://github.com/nic…
claude f0323cb
Merge branch 'main' into claude/project-thread-axak0b
claude ddea614
fix(engine): code-review findings on joint links and row-modelled aff…
claude 17cb732
docs: Props 1870-1871, CHANGELOG and spec reference for the code-revi…
claude e6b2923
fix(engine): direct match moves retry draws that would break a rule
claude d845917
fix(engine): CodeRabbit review of joint links and row-modelled affinity
claude 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
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,23 @@ | ||
| \section{Role-playing links and match-aware link affinity}\label{sec:v75rolelinks} | ||
| \newcounter{savedproprole}\setcounter{savedproprole}{\value{proposition}} | ||
| A fact can link to one parent table several times: a payment and a receiving currency, an origin and a destination airport, a ship-from and a ship-to warehouse (a \emph{role-playing} dimension). Two links of a row can also point at parents that share a value more often than chance (\texttt{link\_affinity}): the same account (a self-loop), the same bank, the same region. Both cases appeared on the IBM AML benchmark (71\,\% cross-currency rows against 1.4\,\% in the data; no reinvestments, which are always self-loops) and were reproduced on two more data classes before the change (synthetic currency transfers and regional shipments, \texttt{tests/unit/test\_joint\_links\_match\_affinity.py}). The code is \texttt{Synthesizer.\_fit\_joint\_extras}, \texttt{\_joint\_extra\_rows}, \texttt{\_match\_flag\_layout} and \texttt{\_match\_links} in \texttt{relsynth/engine.py}. | ||
|
|
||
| \setcounter{proposition}{1869} | ||
| \begin{proposition}[Role-playing extra links drawn jointly]\label{prop:rolelinks} | ||
| Let a fact have extra links $x_1,\dots,x_m$ ($m\ge2$) to one parent table, none set by an offset, with segment labels $s_j$ (level $K_j$ for a null key, when present) and block $b=(z_1,z_2)$. (a) Drawn one by one given the block (the model before this change), $P(s_1=c_1,\dots,s_m=c_m\mid b)=\prod_j P_j(c_j\mid b)$; in particular for a parent with one segment per row, the share of rows whose links agree is $\sum_c\prod_j P_j(c\mid b)$, which the margins fix whatever the source's agreement. (b) The joint model draws the level $S=(s_1,\dots,s_m)$ from $P(S\mid b)$, a sparse Dirichlet over the $L=\prod_j d_j$ joint levels with a back-off prior towards $P(S\mid\text{the more informative block dimension})$, exactly as \texttt{\_extra\_dist} does for one link, and then each link's row within its segment by its degree weight, independently. For a parent with one segment per row the law of the rows given $b$ is $P(S\mid b)$, whose posterior mean converges to the source's conditional frequency of each combination in blocks with growing counts. (c) A combination held by fewer than $k$ source rows ($k=$ \texttt{privacy.suppress\_small\_counts}) is not a level of the joint law: its rows are counted at one extra level, ``other'', and a row drawn there takes each link from its own law given the block reweighted by $r_j(c)=q_j(c)/p_j(c)$, where $p_j$ is link $j$'s margin over all source rows and $q_j=(n^{o}_j+d_jp_j)/(n^{o}+d_j)$ its margin over the ``other'' rows smoothed towards $p_j$. So each link's margin keeps the floored rows' mass where it was, and a rare combination appears only at the rate independent links give it (times the share of the ``other'' level), never from its own count, as a rare level of one column is not reproduced. (d) The joint law is used only when the Gunel--Dickey Bayes factor of the joint table (every row) against independent margins is at least $B=$ \texttt{facts.joint\_links\_bf}, and $L\le$ \texttt{facts.joint\_links\_max\_levels}. The joint prior is a symmetric Dirichlet with total concentration $c=\sum_j d_j$ and the margin priors are its margins, $\mathrm{Dir}(c/d_j)$, so both models have the same prior mean. Under independence $\log\mathrm{BF}\to-\infty$ at rate $\tfrac12\big(L-1-\sum_j(d_j-1)\big)\log n$; under dependence it grows linearly in $n$. (e) A fact without such a group consumes the same random numbers in the same order as before, so its output is byte-identical. | ||
| \end{proposition} | ||
| \begin{proof} | ||
| (a) The single-link draws use separate calls of \texttt{rng.choice} with the block's probabilities and no shared variable, so they are independent given $b$; the agreement share of per-row segments is the sum over rows of the product of the margins. (b) \texttt{\_level\_dist} performs the operations of \texttt{\_extra\_dist} on the joint labels $S=\mathrm{ravel}(s_1,\dots,s_m)$; the level is decoded by \texttt{unravel\_index} with the same dimensions, and the rows of distinct links are drawn by separate weighted choices within their segments. With one segment per row a level is a tuple of rows, so the law of the rows is $P(S\mid b)$. The Dirichlet posterior mean $(n_{b,S}+\alpha_S)/(n_b+\sum\alpha)$ converges to $n_{b,S}/n_b$ as $n_b\to\infty$. (c) Rows of a floored combination carry the label $L$ (``other'') in \texttt{\_level\_dist}, so no joint level has their combination's count. A row drawn at level $L$ takes each link's segment from \texttt{extra\_p}, the single-link law given its block, times $r_j$, independently, so the expected number of rows on a given rare combination is the share of level $L$ times the product of the reweighted single-link probabilities. Averaged over blocks, $\sum_bP(b)P_j(c\mid b)\approx p_j(c)$, so the reweighted law has margin $\approx q_j$ at level $L$, and link $j$'s margin over all rows is $(1-\phi)p^{k}_j+\phi q_j\approx p_j$, with $\phi$ the floored share and $p^k_j$ the margin of the kept rows; without $r_j$ it would be $(1-\phi)p^k_j+\phi p_j$, which moves mass from the levels of rare combinations to common ones. $q_j$ reads per-level counts of one link, as the single-link law already does, and no combination's count. (d) The marginal likelihood of a table under a Dirichlet prior is the Dirichlet-multinomial; aggregating $\mathrm{Dir}(c/L)$ over the other links gives $\mathrm{Dir}(c/d_j)$ on link $j$'s margin, so both models have the same prior mean, and the multinomial coefficient cancels (Good 1976; Gunel and Dickey 1974). With $c=L$ (one per cell) the margin priors concentrate near uniform when $L\gg n$, and skewed independent margins then read as dependence (three Zipf links of 13 levels and 400 rows gave $\log\mathrm{BF}=+91$; the PR bench's stress schema hit this); $c=\sum_j d_j$ gives negative values on such tables at every size tried and keeps strong dependence decisive (\texttt{test\_log\_bf\_joint\_detects\_dependence\_only}). The rates are the usual Laplace (BIC) rates of nested multinomial models: the saturated model has $L-1$ free parameters and the independence model $\sum_j(d_j-1)$. (e) \texttt{\_extra\_dist} calls \texttt{\_level\_dist} with the labels and level count it computed before, which runs the same draws in the same order; \texttt{\_fit\_joint\_extras} registers nothing and \texttt{\_fact\_rows} skips no link when no group passes (d). The CI identity job checks this on every fake scenario and the quickstart. | ||
| \end{proof} | ||
| \noindent Links whose parent is synthesised with few segments (a warehouse table whose segments do not follow its regions) gain little from (b): the dependence runs through an attribute of the parent, which is what \texttt{link\_affinity} declares (Prop.~\ref{prop:matchrows}). | ||
|
|
||
| \setcounter{proposition}{1870} | ||
| \begin{proposition}[Link-affinity matches as a row attribute]\label{prop:matchrows} | ||
| Let a fact declare a match $(a,b)$ whose moved link $\ell$ (the link of $b$) is the partner or an extra link, with no fixed share, no unique key over $\ell$, no nesting, $\ell$ not the partner of an occupancy fact (whose kept pairs a moved partner would break), and $a$ a path through a link, a lookup column (read through its path) or a column the attribute model draws (a column drawn after the links keeps the share model), and let $M=\mathbf 1[a=b]$ on each source row. $M$ is added to the fact's attribute model as a binary column, joined with the categoricals of every check rule that reads both links (under the floor of the rule-linked joints). At sampling, the attributes and $M$ come from the generator, $\ell$ from the link model $Q(\ell\mid\text{row})$; a row whose link disagrees with $M$ is redrawn as follows. (a) If both paths read through links of a role-playing group (Prop.~\ref{prop:rolelinks}), the group's level is drawn from $P(S\mid b)\,p_M(S)$ with $p_1(S)=\sum_{i\in s_a,\,j\in s_b}w_iw_j\mathbf 1[v_a(i)=v_b(j)]/(W_aW_b)$ and $p_0=1-p_1$, the first row $i$ with probability proportional to $w_i\,c_M(i)$, where $c_1(i)=\sum_{j\in s_b}w_j\mathbf 1[v_a(i)=v_b(j)]$ and $c_0(i)=W_b-c_1(i)$, and the second among its segment's rows that agree with $M$ by weight. Then the final keys have the law $Q(\cdot\mid M)$ of the joint model. (b) Otherwise the row's link is moved to a parent drawn by degree weight among the parents with (if $M=1$) or without (if $M=0$) the row's value of $a$; a move is kept only when the row then agrees with $M$ and (d) holds, and is otherwise drawn again, up to \texttt{MATCH\_REDRAWS} times. For a successful move the law of the moved link given $M$ is the weight law restricted to the agreeing parents. A row with no successful move keeps its link and may disagree with $M$; it is checked by the rule redraws like any other row. (c) In both cases the joint law of the attributes and $M$ on the released rows is the generator's, so a rule that ties an attribute to the match on every source row (``a reinvestment is a self-loop'') holds on every synthetic row whose link agrees with its match column (all rows but those left by the fallback of (b)) and whose match column the generator draws within the rule's support (always, when the rule-linked joint is formed), without rejecting rows. (d) Entries are applied in the declared order, and a redraw that would break an earlier entry's agreement, or a key or attribute rule that held on the row, is not made, so a later entry never undoes an earlier one and a move never makes a valid row invalid; a moved partner moves the row's block to its segment. (e) $M$ is a function of columns the model already reads; it adds one binary column and no new source value, and under DP mode it is part of the table the DP mechanism covers. | ||
| \end{proposition} | ||
| \begin{proof} | ||
| (a) Write $A$ for agreement with $M$. The kept row has the law $Q(x)\mathbf 1[A(x)]$; a disagreeing row (probability $Q(\neg A)$) is replaced by a draw from $Q(\cdot\mid A)$. So $P(x)=Q(x)\mathbf 1[A(x)]+Q(\neg A)\,Q(x\mid A)=Q(x\mid A)\big(Q(A)+Q(\neg A)\big)=Q(x\mid A)$. The redraw is $Q(\cdot\mid A)$: the level's probability of agreeing is $p_M(S)$ because the rows are drawn independently by weight within their segments, so $P(S\mid b,A)\propto P(S\mid b)p_M(S)$, and given $S$ the pair $(i,j)$ agreeing with $M$ has probability $w_iw_j\mathbf 1[A(i,j)]/\sum_{i',j'}w_{i'}w_{j'}\mathbf 1[A(i',j')]$, whose margin on $i$ is proportional to $w_ic_M(i)$ and whose conditional on $j$ given $i$ is the weight law on the rows agreeing with $i$; floored levels are masked in this draw as in Prop.~\ref{prop:rolelinks}. (b) A kept move is a draw from the weights restricted to the agreeing set, by construction and by the agreement check on the moved row; rows with no kept move after \texttt{MATCH\_REDRAWS} rounds keep their link and are left to the rule checks. (c) $M$ and the attributes are drawn together by the generator before the links are adjusted, and the adjustment reads $M$ but changes neither; a categorical joint over observed combinations has the source's support. (d) \texttt{\_keeps\_done} compares each earlier entry's agreement before and after the redraw and keeps only rows that do not lose it. (e) The column is computed from the source's keys and their parents' values, which the link model and the rules already read; it enters \texttt{TableModel} like any categorical column, so every generator and DP mechanism that covers the table covers it. | ||
| \end{proof} | ||
| \noindent The share model (Goodman's diagonal inflation, \texttt{\_apply\_affinity}) remains for the cases listed and with \texttt{model: share} or \texttt{structure.link\_affinity: share}. On the test fact with reinvestments it keeps the self-loop share (10.3\,\% against 10.4\,\%) but loses the reinvestments (0.2\,\% against 8.0\,\%), because the self-loops are placed after the rule checks; with the match as a row attribute the reinvestments are 7.7\,\% and the other self-loops 2.7\,\% (source 2.6\,\%). | ||
|
|
||
| \setcounter{proposition}{\value{savedproprole}} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Draw the Bitcoin currency rules on the same rows.
The payment-currency rule (line 128) and the receiving-currency rule (line 135) are both soft, with
holds: 0.99998. The engine draws a separate row indicator for each rule, so one rule can fire on a Bitcoin-format row while the other does not. The row then has Bitcoin on one side and a fiat currency on the other. The spec's own check at line 150 rejects this pair, because Bitcoin payments are neither ACH nor Wire.The two rules must switch on the same rows. A Bitcoin-format row must end with both currencies set to
'Bitcoin', or with both keeping their modelled values.