@lucern/graph-primitives 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/dist/beliefDecay-Q_26RTc-.d.ts +72 -0
- package/dist/beliefDecay.d.ts +2 -0
- package/dist/beliefDecay.js +1628 -0
- package/dist/beliefDecay.js.map +1 -0
- package/dist/beliefEvidenceLinks-42FlR48t.d.ts +77 -0
- package/dist/beliefEvidenceLinks.d.ts +1 -0
- package/dist/beliefEvidenceLinks.js +1978 -0
- package/dist/beliefEvidenceLinks.js.map +1 -0
- package/dist/beliefLifecycle-C-AehZgF.d.ts +43 -0
- package/dist/beliefLifecycle.d.ts +1 -0
- package/dist/beliefLifecycle.js +98 -0
- package/dist/beliefLifecycle.js.map +1 -0
- package/dist/confidencePropagationDispatch.d.ts +46 -0
- package/dist/confidencePropagationDispatch.js +744 -0
- package/dist/confidencePropagationDispatch.js.map +1 -0
- package/dist/contradictions-Hdwl7zid.d.ts +71 -0
- package/dist/contradictions.d.ts +1 -0
- package/dist/contradictions.js +1557 -0
- package/dist/contradictions.js.map +1 -0
- package/dist/convex.d.ts +23 -0
- package/dist/convex.js +17 -0
- package/dist/convex.js.map +1 -0
- package/dist/edgeValidation-CeI0wc0r.d.ts +35 -0
- package/dist/edgeValidation.d.ts +2 -0
- package/dist/edgeValidation.js +307 -0
- package/dist/edgeValidation.js.map +1 -0
- package/dist/edges/contains.d.ts +6 -0
- package/dist/edges/contains.js +14 -0
- package/dist/edges/contains.js.map +1 -0
- package/dist/edges/contradicts.d.ts +6 -0
- package/dist/edges/contradicts.js +183 -0
- package/dist/edges/contradicts.js.map +1 -0
- package/dist/edges/dependsOn.d.ts +6 -0
- package/dist/edges/dependsOn.js +240 -0
- package/dist/edges/dependsOn.js.map +1 -0
- package/dist/edges/derivedFrom.d.ts +6 -0
- package/dist/edges/derivedFrom.js +14 -0
- package/dist/edges/derivedFrom.js.map +1 -0
- package/dist/edges/elaborates.d.ts +6 -0
- package/dist/edges/elaborates.js +100 -0
- package/dist/edges/elaborates.js.map +1 -0
- package/dist/edges/index.d.ts +3 -0
- package/dist/edges/index.js +556 -0
- package/dist/edges/index.js.map +1 -0
- package/dist/edges/informs.d.ts +6 -0
- package/dist/edges/informs.js +112 -0
- package/dist/edges/informs.js.map +1 -0
- package/dist/edges/propagationTypes.d.ts +39 -0
- package/dist/edges/propagationTypes.js +17 -0
- package/dist/edges/propagationTypes.js.map +1 -0
- package/dist/edges/refutes.d.ts +6 -0
- package/dist/edges/refutes.js +108 -0
- package/dist/edges/refutes.js.map +1 -0
- package/dist/edges/supports.d.ts +6 -0
- package/dist/edges/supports.js +193 -0
- package/dist/edges/supports.js.map +1 -0
- package/dist/edges/tests.d.ts +6 -0
- package/dist/edges/tests.js +14 -0
- package/dist/edges/tests.js.map +1 -0
- package/dist/edges/utils.d.ts +12 -0
- package/dist/edges/utils.js +188 -0
- package/dist/edges/utils.js.map +1 -0
- package/dist/embeddingTrigger.d.ts +24 -0
- package/dist/embeddingTrigger.js +24 -0
- package/dist/embeddingTrigger.js.map +1 -0
- package/dist/entityBridge-DMaKooYn.d.ts +59 -0
- package/dist/entityBridge.d.ts +1 -0
- package/dist/entityBridge.js +663 -0
- package/dist/entityBridge.js.map +1 -0
- package/dist/entityLifecycle-BkhRJ-XI.d.ts +69 -0
- package/dist/entityLifecycle.d.ts +1 -0
- package/dist/entityLifecycle.js +2083 -0
- package/dist/entityLifecycle.js.map +1 -0
- package/dist/entityValidation-KLZ_Xl2D.d.ts +50 -0
- package/dist/entityValidation.d.ts +3 -0
- package/dist/entityValidation.js +71 -0
- package/dist/entityValidation.js.map +1 -0
- package/dist/epistemicAnswers-DSP1slZ9.d.ts +67 -0
- package/dist/epistemicAnswers.d.ts +1 -0
- package/dist/epistemicAnswers.js +1650 -0
- package/dist/epistemicAnswers.js.map +1 -0
- package/dist/epistemicBeliefs-DtFVTp-k.d.ts +377 -0
- package/dist/epistemicBeliefs.d.ts +5 -0
- package/dist/epistemicBeliefs.js +6386 -0
- package/dist/epistemicBeliefs.js.map +1 -0
- package/dist/epistemicContractHelpers.d.ts +1 -0
- package/dist/epistemicContractHelpers.js +320 -0
- package/dist/epistemicContractHelpers.js.map +1 -0
- package/dist/epistemicContracts.d.ts +77 -0
- package/dist/epistemicContracts.js +8436 -0
- package/dist/epistemicContracts.js.map +1 -0
- package/dist/epistemicEdges-DcA8ErUG.d.ts +191 -0
- package/dist/epistemicEdges.d.ts +2 -0
- package/dist/epistemicEdges.js +2749 -0
- package/dist/epistemicEdges.js.map +1 -0
- package/dist/epistemicEvidence-Bo638XDP.d.ts +128 -0
- package/dist/epistemicEvidence.d.ts +3 -0
- package/dist/epistemicEvidence.js +3282 -0
- package/dist/epistemicEvidence.js.map +1 -0
- package/dist/epistemicHelpers-Bd9xbaib.d.ts +329 -0
- package/dist/epistemicHelpers.d.ts +4 -0
- package/dist/epistemicHelpers.js +999 -0
- package/dist/epistemicHelpers.js.map +1 -0
- package/dist/epistemicLinking-CyeLOIzN.d.ts +35 -0
- package/dist/epistemicLinking.d.ts +1 -0
- package/dist/epistemicLinking.js +1391 -0
- package/dist/epistemicLinking.js.map +1 -0
- package/dist/epistemicNodes-BpD6Koud.d.ts +167 -0
- package/dist/epistemicNodes.d.ts +2 -0
- package/dist/epistemicNodes.js +2942 -0
- package/dist/epistemicNodes.js.map +1 -0
- package/dist/epistemicQuestions-CmEeY6zQ.d.ts +214 -0
- package/dist/epistemicQuestions.d.ts +3 -0
- package/dist/epistemicQuestions.js +4993 -0
- package/dist/epistemicQuestions.js.map +1 -0
- package/dist/epistemicSources-ZazxHOK1.d.ts +25 -0
- package/dist/epistemicSources.d.ts +1 -0
- package/dist/epistemicSources.js +2025 -0
- package/dist/epistemicSources.js.map +1 -0
- package/dist/evaluators/index.d.ts +9 -0
- package/dist/evaluators/index.js +8440 -0
- package/dist/evaluators/index.js.map +1 -0
- package/dist/evaluators/lintCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/lintCheckerEvaluator.js +155 -0
- package/dist/evaluators/lintCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/sentryCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/sentryCheckerEvaluator.js +126 -0
- package/dist/evaluators/sentryCheckerEvaluator.js.map +1 -0
- package/dist/evaluators/shared.d.ts +27 -0
- package/dist/evaluators/shared.js +92 -0
- package/dist/evaluators/shared.js.map +1 -0
- package/dist/evaluators/testRunnerEvaluator.d.ts +17 -0
- package/dist/evaluators/testRunnerEvaluator.js +232 -0
- package/dist/evaluators/testRunnerEvaluator.js.map +1 -0
- package/dist/evaluators/tscCheckerEvaluator.d.ts +11 -0
- package/dist/evaluators/tscCheckerEvaluator.js +189 -0
- package/dist/evaluators/tscCheckerEvaluator.js.map +1 -0
- package/dist/globalId-DKh9d_uD.d.ts +20 -0
- package/dist/globalId.d.ts +1 -0
- package/dist/globalId.js +15 -0
- package/dist/globalId.js.map +1 -0
- package/dist/graphTypes-CpgIuCdo.d.ts +52 -0
- package/dist/graphTypes.d.ts +1 -0
- package/dist/graphTypes.js +120 -0
- package/dist/graphTypes.js.map +1 -0
- package/dist/helpers-BYHIk5vU.d.ts +27 -0
- package/dist/helpers.d.ts +4 -0
- package/dist/helpers.js +313 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index-Dct1T70K.d.ts +25 -0
- package/dist/index-Dq-7R-gi.d.ts +31 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +22294 -0
- package/dist/index.js.map +1 -0
- package/dist/invariantEnforcement.d.ts +52 -0
- package/dist/invariantEnforcement.js +231 -0
- package/dist/invariantEnforcement.js.map +1 -0
- package/dist/logicalRoleInference-CJxqWi3u.d.ts +16 -0
- package/dist/logicalRoleInference.d.ts +3 -0
- package/dist/logicalRoleInference.js +64 -0
- package/dist/logicalRoleInference.js.map +1 -0
- package/dist/matcherFeedbackUtils.d.ts +33 -0
- package/dist/matcherFeedbackUtils.js +95 -0
- package/dist/matcherFeedbackUtils.js.map +1 -0
- package/dist/ontology-matching-Buhu23ss.d.ts +48 -0
- package/dist/ontology-matching.d.ts +2 -0
- package/dist/ontology-matching.js +346 -0
- package/dist/ontology-matching.js.map +1 -0
- package/dist/ontologyApproval-Ba0Jjk1k.d.ts +26 -0
- package/dist/ontologyApproval.d.ts +1 -0
- package/dist/ontologyApproval.js +78 -0
- package/dist/ontologyApproval.js.map +1 -0
- package/dist/ontologyDefinitions.d.ts +72 -0
- package/dist/ontologyDefinitions.js +635 -0
- package/dist/ontologyDefinitions.js.map +1 -0
- package/dist/ontologyHelpers.d.ts +79 -0
- package/dist/ontologyHelpers.js +81 -0
- package/dist/ontologyHelpers.js.map +1 -0
- package/dist/ontologyRegistry-B67rPJ16.d.ts +31 -0
- package/dist/ontologyRegistry.d.ts +1 -0
- package/dist/ontologyRegistry.js +296 -0
- package/dist/ontologyRegistry.js.map +1 -0
- package/dist/projectionReconciliation-CxrXYGaB.d.ts +20 -0
- package/dist/projectionReconciliation.d.ts +1 -0
- package/dist/projectionReconciliation.js +261 -0
- package/dist/projectionReconciliation.js.map +1 -0
- package/dist/projectionStaleness-CAdpIsaW.d.ts +51 -0
- package/dist/projectionStaleness.d.ts +1 -0
- package/dist/projectionStaleness.js +57 -0
- package/dist/projectionStaleness.js.map +1 -0
- package/dist/questionEvidenceLinks-BdQD0TkM.d.ts +34 -0
- package/dist/questionEvidenceLinks.d.ts +1 -0
- package/dist/questionEvidenceLinks.js +1690 -0
- package/dist/questionEvidenceLinks.js.map +1 -0
- package/dist/resolverTypes-CC8Ea2E2.d.ts +20 -0
- package/dist/resolverTypes.d.ts +4 -0
- package/dist/resolverTypes.js +3 -0
- package/dist/resolverTypes.js.map +1 -0
- package/dist/resolvers-Br1a6eLV.d.ts +14 -0
- package/dist/resolvers.d.ts +5 -0
- package/dist/resolvers.js +308 -0
- package/dist/resolvers.js.map +1 -0
- package/dist/scopeResolverCompat.d.ts +26 -0
- package/dist/scopeResolverCompat.js +242 -0
- package/dist/scopeResolverCompat.js.map +1 -0
- package/dist/text-matching-CMn2WnVD.d.ts +40 -0
- package/dist/text-matching.d.ts +2 -0
- package/dist/text-matching.js +246 -0
- package/dist/text-matching.js.map +1 -0
- package/dist/topicOntologyResolver.d.ts +80 -0
- package/dist/topicOntologyResolver.js +67 -0
- package/dist/topicOntologyResolver.js.map +1 -0
- package/dist/topicProjectOverlay.d.ts +92 -0
- package/dist/topicProjectOverlay.js +249 -0
- package/dist/topicProjectOverlay.js.map +1 -0
- package/dist/topicScope-By_zp4tt.d.ts +34 -0
- package/dist/topicScope.d.ts +3 -0
- package/dist/topicScope.js +206 -0
- package/dist/topicScope.js.map +1 -0
- package/dist/workspaceIsolation.d.ts +44 -0
- package/dist/workspaceIsolation.js +950 -0
- package/dist/workspaceIsolation.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// ../confidence/src/v1/operations/subjectiveLogic/index.ts
|
|
2
|
+
function opinion(belief, disbelief, uncertainty, baseRate = 0.5) {
|
|
3
|
+
const b = Math.max(0, Math.min(1, belief));
|
|
4
|
+
const d = Math.max(0, Math.min(1, disbelief));
|
|
5
|
+
const u = Math.max(0, Math.min(1, uncertainty));
|
|
6
|
+
const a = Math.max(0, Math.min(1, baseRate));
|
|
7
|
+
const sum = b + d + u;
|
|
8
|
+
if (sum === 0) {
|
|
9
|
+
return { b: 0, d: 0, u: 1, a };
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
b: b / sum,
|
|
13
|
+
d: d / sum,
|
|
14
|
+
u: u / sum,
|
|
15
|
+
a
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function vacuous(baseRate = 0.5) {
|
|
19
|
+
return { b: 0, d: 0, u: 1, a: baseRate };
|
|
20
|
+
}
|
|
21
|
+
function cumulativeFusion(left, right) {
|
|
22
|
+
if (left.u === 0 && right.u === 0) {
|
|
23
|
+
return opinion(
|
|
24
|
+
(left.b + right.b) / 2,
|
|
25
|
+
(left.d + right.d) / 2,
|
|
26
|
+
0,
|
|
27
|
+
(left.a + right.a) / 2
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
const k = left.u + right.u - left.u * right.u;
|
|
31
|
+
if (k === 0) {
|
|
32
|
+
return vacuous((left.a + right.a) / 2);
|
|
33
|
+
}
|
|
34
|
+
return opinion(
|
|
35
|
+
(left.b * right.u + right.b * left.u) / k,
|
|
36
|
+
(left.d * right.u + right.d * left.u) / k,
|
|
37
|
+
left.u * right.u / k,
|
|
38
|
+
(left.a + right.a) / 2
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function trustDiscount(sourceOpinion, trust) {
|
|
42
|
+
const weight = Math.max(0, Math.min(1, Math.abs(trust)));
|
|
43
|
+
return opinion(
|
|
44
|
+
weight * sourceOpinion.b,
|
|
45
|
+
weight * sourceOpinion.d,
|
|
46
|
+
1 - weight * (sourceOpinion.b + sourceOpinion.d),
|
|
47
|
+
sourceOpinion.a
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function negate(o) {
|
|
51
|
+
return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ../confidence/src/v1/operations/dynamics/defeat.ts
|
|
55
|
+
function applyNegativeEvidence(source, target, weight) {
|
|
56
|
+
const discounted = trustDiscount(negate(source), Math.abs(weight));
|
|
57
|
+
return {
|
|
58
|
+
opinion: cumulativeFusion(target, discounted),
|
|
59
|
+
operator: "cumulative_fusion",
|
|
60
|
+
rationale: `Contradicting evidence (weight=${weight.toFixed(2)})`
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/edges/utils.ts
|
|
65
|
+
function applyPerHopDamping(sourceOpinion, damping) {
|
|
66
|
+
if (damping >= 1) {
|
|
67
|
+
return sourceOpinion;
|
|
68
|
+
}
|
|
69
|
+
return trustDiscount(sourceOpinion, Math.max(0, damping));
|
|
70
|
+
}
|
|
71
|
+
function annotateRationale(result, spec, hop) {
|
|
72
|
+
return {
|
|
73
|
+
...result,
|
|
74
|
+
rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(
|
|
75
|
+
2
|
|
76
|
+
)} :: ${result.rationale}`
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function propagatePositiveInform(sourceOpinion, targetOpinion, edgeWeight) {
|
|
80
|
+
const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));
|
|
81
|
+
return {
|
|
82
|
+
opinion: cumulativeFusion(targetOpinion, discounted),
|
|
83
|
+
operator: "cumulative_fusion",
|
|
84
|
+
rationale: `Supporting evidence (weight=${edgeWeight.toFixed(2)})`
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function propagateNegativeInform(sourceOpinion, targetOpinion, edgeWeight) {
|
|
88
|
+
return applyNegativeEvidence(sourceOpinion, targetOpinion, edgeWeight);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/edges/informs.ts
|
|
92
|
+
var informsPropagationSpec = {
|
|
93
|
+
edgeType: "informs",
|
|
94
|
+
direction: "outgoing",
|
|
95
|
+
transitivity: "full",
|
|
96
|
+
damping: 0.92,
|
|
97
|
+
maxHops: "unbounded",
|
|
98
|
+
operator: (sourceOpinion, targetOpinion, edge, context) => {
|
|
99
|
+
const dampedSource = applyPerHopDamping(
|
|
100
|
+
sourceOpinion,
|
|
101
|
+
context.spec.damping
|
|
102
|
+
);
|
|
103
|
+
const weight = edge.weight ?? 1;
|
|
104
|
+
const result = weight < 0 ? propagateNegativeInform(dampedSource, targetOpinion, weight) : propagatePositiveInform(dampedSource, targetOpinion, weight);
|
|
105
|
+
return annotateRationale(result, context.spec, context.hop);
|
|
106
|
+
},
|
|
107
|
+
description: "Evidence-bearing influence. Informs can chain through the graph with light per-hop damping."
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export { informsPropagationSpec };
|
|
111
|
+
//# sourceMappingURL=informs.js.map
|
|
112
|
+
//# sourceMappingURL=informs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../confidence/src/v1/operations/subjectiveLogic/index.ts","../../../confidence/src/v1/operations/dynamics/defeat.ts","../../src/edges/utils.ts","../../src/edges/informs.ts"],"names":[],"mappings":";AAEO,SAAS,OAAA,CACd,MAAA,EACA,SAAA,EACA,WAAA,EACA,WAAmB,GAAA,EACV;AACT,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,MAAM,CAAC,CAAA;AACzC,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,SAAS,CAAC,CAAA;AAC5C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,WAAW,CAAC,CAAA;AAC9C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,QAAQ,CAAC,CAAA;AAC3C,EAAA,MAAM,GAAA,GAAM,IAAI,CAAA,GAAI,CAAA;AAEpB,EAAA,IAAI,QAAQ,CAAA,EAAG;AACb,IAAA,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAE;AAAA,EAC/B;AAEA,EAAA,OAAO;AAAA,IACL,GAAG,CAAA,GAAI,GAAA;AAAA,IACP,GAAG,CAAA,GAAI,GAAA;AAAA,IACP,GAAG,CAAA,GAAI,GAAA;AAAA,IACP;AAAA,GACF;AACF;AAEO,SAAS,OAAA,CAAQ,WAAmB,GAAA,EAAc;AACvD,EAAA,OAAO,EAAE,GAAG,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,GAAG,QAAA,EAAS;AACzC;AAyBO,SAAS,gBAAA,CAAiB,MAA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,WAAA,EACE;AACJ","file":"informs.js","sourcesContent":["import type { Opinion } from \"../../types\";\n\nexport function opinion(\n belief: number,\n disbelief: number,\n uncertainty: number,\n baseRate: number = 0.5\n): Opinion {\n const b = Math.max(0, Math.min(1, belief));\n const d = Math.max(0, Math.min(1, disbelief));\n const u = Math.max(0, Math.min(1, uncertainty));\n const a = Math.max(0, Math.min(1, baseRate));\n const sum = b + d + u;\n\n if (sum === 0) {\n return { b: 0, d: 0, u: 1, a };\n }\n\n return {\n b: b / sum,\n d: d / sum,\n u: u / sum,\n a,\n };\n}\n\nexport function vacuous(baseRate: number = 0.5): Opinion {\n return { b: 0, d: 0, u: 1, a: baseRate };\n}\n\nexport function dogmatic(probability: number, baseRate: number = 0.5): Opinion {\n const p = Math.max(0, Math.min(1, probability));\n return { b: p, d: 1 - p, u: 0, a: baseRate };\n}\n\nexport function project(o: Opinion): number {\n return o.b + o.a * o.u;\n}\n\nexport function confidenceLevel(o: Opinion): \"high\" | \"medium\" | \"low\" {\n const projected = project(o);\n if (o.u > 0.5) {\n return \"low\";\n }\n if (projected >= 0.8 && o.u < 0.2) {\n return \"high\";\n }\n if (projected >= 0.6) {\n return \"medium\";\n }\n return \"low\";\n}\n\nexport function cumulativeFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u - left.u * right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function averagingFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (2 * left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function trustDiscount(sourceOpinion: Opinion, trust: number): Opinion {\n const weight = Math.max(0, Math.min(1, Math.abs(trust)));\n return opinion(\n weight * sourceOpinion.b,\n weight * sourceOpinion.d,\n 1 - weight * (sourceOpinion.b + sourceOpinion.d),\n sourceOpinion.a\n );\n}\n\nconst EPSILON = 1e-9;\n\nfunction childBaseRateFallback(\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number | undefined\n): number {\n if (fallbackBaseRate !== undefined) {\n return Math.max(0, Math.min(1, fallbackBaseRate));\n }\n\n if (Math.abs(ifTrue.a - ifFalse.a) <= EPSILON) {\n return ifTrue.a;\n }\n\n return (ifTrue.a + ifFalse.a) / 2;\n}\n\nfunction computeConditionalDeductionBaseRate(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number\n): number {\n const denominator =\n 1 - opinionA.a * ifTrue.u - (1 - opinionA.a) * ifFalse.u;\n\n if (ifTrue.u + ifFalse.u < 2 - EPSILON && Math.abs(denominator) > EPSILON) {\n const baseRate =\n (opinionA.a * ifTrue.b + (1 - opinionA.a) * ifFalse.b) / denominator;\n if (baseRate >= -EPSILON && baseRate <= 1 + EPSILON) {\n return Math.max(0, Math.min(1, baseRate));\n }\n }\n\n return fallbackBaseRate;\n}\n\nfunction safeCorrectionTerm(\n numerator: number,\n denominator: number\n): number | undefined {\n if (Math.abs(denominator) <= EPSILON) {\n return undefined;\n }\n\n const value = numerator / denominator;\n if (!Number.isFinite(value)) {\n return undefined;\n }\n\n return Math.max(0, value);\n}\n\nexport function conditionalDeduction(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate?: number\n): Opinion {\n const fallbackChildBaseRate = childBaseRateFallback(\n ifTrue,\n ifFalse,\n fallbackBaseRate\n );\n const childBaseRate = computeConditionalDeductionBaseRate(\n opinionA,\n ifTrue,\n ifFalse,\n fallbackChildBaseRate\n );\n const projectedAntecedent = project(opinionA);\n const projectedAntecedentComplement = 1 - projectedAntecedent;\n const intermediateBelief =\n opinionA.b * ifTrue.b +\n opinionA.d * ifFalse.b +\n opinionA.u * (ifTrue.b * opinionA.a + ifFalse.b * (1 - opinionA.a));\n const intermediateDisbelief =\n opinionA.b * ifTrue.d +\n opinionA.d * ifFalse.d +\n opinionA.u * (ifTrue.d * opinionA.a + ifFalse.d * (1 - opinionA.a));\n const intermediateUncertainty =\n opinionA.b * ifTrue.u +\n opinionA.d * ifFalse.u +\n opinionA.u * (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedVacuousDeduction =\n ifTrue.b * opinionA.a +\n ifFalse.b * (1 - opinionA.a) +\n childBaseRate *\n (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedConditionalA =\n ifTrue.b + childBaseRate * (1 - ifTrue.b - ifTrue.d);\n let correction = 0;\n\n if (\n (ifTrue.b > ifFalse.b && ifTrue.d > ifFalse.d) ||\n (ifTrue.b <= ifFalse.b && ifTrue.d <= ifFalse.d)\n ) {\n correction = 0;\n } else if (ifTrue.b > ifFalse.b && ifTrue.d <= ifFalse.d) {\n const beliefGap = ifTrue.b - ifFalse.b;\n const disbeliefGap = ifFalse.d - ifTrue.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedent * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d) * beliefGap,\n projectedAntecedentComplement * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedent * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedentComplement * (1 - childBaseRate)\n ) ?? 0;\n }\n } else {\n const beliefGap = ifFalse.b - ifTrue.b;\n const disbeliefGap = ifTrue.d - ifFalse.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateDisbelief - ifTrue.d) *\n beliefGap,\n projectedAntecedent * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedentComplement * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedent * (1 - childBaseRate)\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n opinionA.a *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedentComplement * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n }\n }\n\n return opinion(\n intermediateBelief - childBaseRate * correction,\n intermediateDisbelief - (1 - childBaseRate) * correction,\n intermediateUncertainty + correction,\n childBaseRate\n );\n}\n\n/**\n * Abductive inference over a conditional.\n * Given an opinion on Y and conditionals P(Y|X), P(Y|~X), infer an opinion on X.\n */\nexport function conditionalAbduction(\n opinionY: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n baseRateX: number\n): Opinion {\n const priorX = Math.max(0, Math.min(1, baseRateX));\n const probabilityY = project(opinionY);\n const probabilityGivenTrue = project(ifTrue);\n const probabilityGivenFalse = project(ifFalse);\n\n const posteriorIfYDenominator =\n probabilityGivenTrue * priorX +\n probabilityGivenFalse * (1 - priorX);\n const posteriorIfY =\n posteriorIfYDenominator === 0\n ? priorX\n : (probabilityGivenTrue * priorX) / posteriorIfYDenominator;\n\n const posteriorIfNotYDenominator =\n (1 - probabilityGivenTrue) * priorX +\n (1 - probabilityGivenFalse) * (1 - priorX);\n const posteriorIfNotY =\n posteriorIfNotYDenominator === 0\n ? priorX\n : ((1 - probabilityGivenTrue) * priorX) / posteriorIfNotYDenominator;\n\n const projectedX =\n probabilityY * posteriorIfY + (1 - probabilityY) * posteriorIfNotY;\n const uncertainty = Math.max(\n opinionY.u * 0.5,\n probabilityY * ifTrue.u + (1 - probabilityY) * ifFalse.u\n );\n\n return opinion(\n projectedX * (1 - uncertainty),\n (1 - projectedX) * (1 - uncertainty),\n uncertainty,\n priorX\n );\n}\n\nexport function negate(o: Opinion): Opinion {\n return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };\n}\n\nexport function constraintFusion(\n left: Opinion,\n right: Opinion,\n mode: \"pressure\" | \"redistribute\" = \"pressure\"\n): { o1: Opinion; o2: Opinion } {\n if (mode === \"redistribute\") {\n const leftProjected = project(left);\n const rightProjected = project(right);\n const total = leftProjected + rightProjected;\n\n if (total <= 1) {\n return { o1: left, o2: right };\n }\n\n const scale = 1 / total;\n return {\n o1: opinion(\n left.b * scale,\n left.d + left.b * (1 - scale),\n left.u,\n left.a\n ),\n o2: opinion(\n right.b * scale,\n right.d + right.b * (1 - scale),\n right.u,\n right.a\n ),\n };\n }\n\n const pressureLeft = right.b * 0.5;\n const pressureRight = left.b * 0.5;\n\n return {\n o1: opinion(\n left.b - pressureLeft * 0.3,\n left.d + pressureLeft * 0.3,\n left.u,\n left.a\n ),\n o2: opinion(\n right.b - pressureRight * 0.3,\n right.d + pressureRight * 0.3,\n right.u,\n right.a\n ),\n };\n}\n\nexport function evidenceBalance(o: Opinion): number {\n const total = o.b + o.d;\n if (total === 0) {\n return 0;\n }\n return (o.b - o.d) / total;\n}\n\nexport function areTensioned(left: Opinion, right: Opinion): boolean {\n return (\n project(left) > 0.5 &&\n project(right) > 0.5 &&\n (left.d > 0.2 || right.d > 0.2)\n );\n}\n\nexport function informationGain(o: Opinion): number {\n if (o.u === 0) {\n return 0;\n }\n if (o.u === 1) {\n return 1;\n }\n return o.u * (1 - Math.abs(o.b - o.d));\n}\n","import type { EdgeMetadata, Opinion, PropagationResult } from \"../../types\";\nimport {\n constraintFusion,\n cumulativeFusion,\n negate,\n project,\n trustDiscount,\n} from \"../subjectiveLogic\";\n\nexport function applyNegativeSupport(\n source: Opinion,\n target: Opinion,\n weight: number,\n metadata: EdgeMetadata = {}\n): PropagationResult {\n if (metadata.constraint === \"xor\") {\n const result = constraintFusion(\n source,\n target,\n metadata.normalization ?? \"pressure\"\n );\n return {\n opinion: result.o2,\n operator: \"constraint_fusion\",\n rationale: `XOR constraint: source belief at ${project(source).toFixed(\n 2\n )} pressures target`,\n };\n }\n\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(\n 2\n )}) from source at ${project(source).toFixed(2)}`,\n };\n}\n\nexport function applyNegativeEvidence(\n source: Opinion,\n target: Opinion,\n weight: number\n): PropagationResult {\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(2)})`,\n };\n}\n","import {\n applyNegativeEvidence,\n applyNegativeSupport,\n cumulativeFusion,\n project,\n trustDiscount,\n type EdgeMetadata,\n type Opinion,\n type PropagationResult,\n} from \"@lucern/confidence\";\nimport type {\n EdgePropagationSpec,\n PropagationEdgeRecord,\n} from \"./propagationTypes\";\n\nexport function readEdgeMetadata<TNodeId extends string>(\n edge: PropagationEdgeRecord<TNodeId>\n): EdgeMetadata {\n return {\n constraint: edge.constraint ?? undefined,\n normalization: edge.normalization ?? undefined,\n propagation: edge.propagation ?? undefined,\n conditionalA: edge.conditionalA ?? undefined,\n conditionalNotA: edge.conditionalNotA ?? undefined,\n };\n}\n\nexport function applyPerHopDamping(\n sourceOpinion: Opinion,\n damping: number\n): Opinion {\n if (damping >= 1) {\n return sourceOpinion;\n }\n\n return trustDiscount(sourceOpinion, Math.max(0, damping));\n}\n\nexport function annotateRationale(\n result: PropagationResult,\n spec: EdgePropagationSpec,\n hop: number\n): PropagationResult {\n return {\n ...result,\n rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(\n 2\n )} :: ${result.rationale}`,\n };\n}\n\nexport function propagatePositiveSupport(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(\n 2\n )}) from source at ${project(sourceOpinion).toFixed(2)}`,\n };\n}\n\nexport function propagatePositiveInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(2)})`,\n };\n}\n\nexport function propagateNegativeSupportWithMetadata(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number,\n edge: PropagationEdgeRecord\n): PropagationResult {\n return applyNegativeSupport(\n sourceOpinion,\n targetOpinion,\n edgeWeight,\n readEdgeMetadata(edge)\n );\n}\n\nexport function propagateNegativeInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n return applyNegativeEvidence(sourceOpinion, targetOpinion, edgeWeight);\n}\n\n","import type { EdgePropagationSpec } from \"./propagationTypes\";\nimport {\n annotateRationale,\n applyPerHopDamping,\n propagateNegativeInform,\n propagatePositiveInform,\n} from \"./utils\";\n\nexport const informsPropagationSpec: EdgePropagationSpec = {\n edgeType: \"informs\",\n direction: \"outgoing\",\n transitivity: \"full\",\n damping: 0.92,\n maxHops: \"unbounded\",\n operator: (sourceOpinion, targetOpinion, edge, context) => {\n const dampedSource = applyPerHopDamping(\n sourceOpinion,\n context.spec.damping\n );\n const weight = edge.weight ?? 1;\n const result =\n weight < 0\n ? propagateNegativeInform(dampedSource, targetOpinion, weight)\n : propagatePositiveInform(dampedSource, targetOpinion, weight);\n\n return annotateRationale(result, context.spec, context.hop);\n },\n description:\n \"Evidence-bearing influence. Informs can chain through the graph with light per-hop damping.\",\n};\n\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { EdgeMetadata, Opinion, PropagationResult } from '@lucern/confidence';
|
|
2
|
+
|
|
3
|
+
type PropagationTraversalDirection = "outgoing" | "incoming";
|
|
4
|
+
type PropagationSpecDirection = PropagationTraversalDirection | "bidirectional";
|
|
5
|
+
type PropagationTransitivity = "none" | "damped" | "full";
|
|
6
|
+
type PropagationEdgeType = "supports" | "depends_on" | "informs" | "contains" | "tests" | "derived_from" | "contradicts" | "refutes" | "elaborates";
|
|
7
|
+
type PropagationEdgeRecord<TNodeId extends string = string> = {
|
|
8
|
+
fromNodeId?: TNodeId | null;
|
|
9
|
+
toNodeId?: TNodeId | null;
|
|
10
|
+
edgeType: string;
|
|
11
|
+
weight?: number | null;
|
|
12
|
+
constraint?: EdgeMetadata["constraint"] | null;
|
|
13
|
+
normalization?: EdgeMetadata["normalization"] | null;
|
|
14
|
+
propagation?: EdgeMetadata["propagation"] | null;
|
|
15
|
+
conditionalA?: EdgeMetadata["conditionalA"] | null;
|
|
16
|
+
conditionalNotA?: EdgeMetadata["conditionalNotA"] | null;
|
|
17
|
+
tenantId?: string;
|
|
18
|
+
workspaceId?: string;
|
|
19
|
+
};
|
|
20
|
+
type EdgePropagationSpec<TNodeId extends string = string> = {
|
|
21
|
+
edgeType: PropagationEdgeType;
|
|
22
|
+
direction: PropagationSpecDirection;
|
|
23
|
+
transitivity: PropagationTransitivity;
|
|
24
|
+
damping: number;
|
|
25
|
+
maxHops: number | "unbounded";
|
|
26
|
+
operator: (sourceOpinion: Opinion, targetOpinion: Opinion, edge: PropagationEdgeRecord<TNodeId>, context: {
|
|
27
|
+
hop: number;
|
|
28
|
+
sourceNodeId: TNodeId;
|
|
29
|
+
targetNodeId: TNodeId;
|
|
30
|
+
traversedDirection: PropagationTraversalDirection;
|
|
31
|
+
spec: EdgePropagationSpec<TNodeId>;
|
|
32
|
+
}) => PropagationResult | null;
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
declare function isPropagationTraversalDirection(direction: string): direction is PropagationTraversalDirection;
|
|
36
|
+
declare function canTraverseHop<TNodeId extends string>(spec: EdgePropagationSpec<TNodeId>, nextHop: number): boolean;
|
|
37
|
+
declare function canContinueTransitively<TNodeId extends string>(spec: EdgePropagationSpec<TNodeId>, currentHop: number): boolean;
|
|
38
|
+
|
|
39
|
+
export { type EdgePropagationSpec, type PropagationEdgeRecord, type PropagationEdgeType, type PropagationSpecDirection, type PropagationTransitivity, type PropagationTraversalDirection, canContinueTransitively, canTraverseHop, isPropagationTraversalDirection };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/edges/propagationTypes.ts
|
|
2
|
+
function isPropagationTraversalDirection(direction) {
|
|
3
|
+
return direction === "outgoing" || direction === "incoming";
|
|
4
|
+
}
|
|
5
|
+
function canTraverseHop(spec, nextHop) {
|
|
6
|
+
return spec.maxHops === "unbounded" || nextHop <= spec.maxHops;
|
|
7
|
+
}
|
|
8
|
+
function canContinueTransitively(spec, currentHop) {
|
|
9
|
+
if (spec.transitivity === "none") {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return spec.maxHops === "unbounded" || currentHop < spec.maxHops;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { canContinueTransitively, canTraverseHop, isPropagationTraversalDirection };
|
|
16
|
+
//# sourceMappingURL=propagationTypes.js.map
|
|
17
|
+
//# sourceMappingURL=propagationTypes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/edges/propagationTypes.ts"],"names":[],"mappings":";AA4DO,SAAS,gCACd,SAAA,EAC4C;AAC5C,EAAA,OAAO,SAAA,KAAc,cAAc,SAAA,KAAc,UAAA;AACnD;AAEO,SAAS,cAAA,CACd,MACA,OAAA,EACS;AACT,EAAA,OAAO,IAAA,CAAK,OAAA,KAAY,WAAA,IAAe,OAAA,IAAW,IAAA,CAAK,OAAA;AACzD;AAEO,SAAS,uBAAA,CACd,MACA,UAAA,EACS;AACT,EAAA,IAAI,IAAA,CAAK,iBAAiB,MAAA,EAAQ;AAChC,IAAA,OAAO,KAAA;AAAA,EACT;AAEA,EAAA,OAAO,IAAA,CAAK,OAAA,KAAY,WAAA,IAAe,UAAA,GAAa,IAAA,CAAK,OAAA;AAC3D","file":"propagationTypes.js","sourcesContent":["import type {\n EdgeMetadata,\n Opinion,\n PropagationResult,\n} from \"@lucern/confidence\";\n\nexport type PropagationTraversalDirection = \"outgoing\" | \"incoming\";\n\nexport type PropagationSpecDirection =\n | PropagationTraversalDirection\n | \"bidirectional\";\n\nexport type PropagationTransitivity = \"none\" | \"damped\" | \"full\";\n\nexport type PropagationEdgeType =\n | \"supports\"\n | \"depends_on\"\n | \"informs\"\n | \"contains\"\n | \"tests\"\n | \"derived_from\"\n | \"contradicts\"\n | \"refutes\"\n | \"elaborates\";\n\nexport type PropagationEdgeRecord<TNodeId extends string = string> = {\n fromNodeId?: TNodeId | null;\n toNodeId?: TNodeId | null;\n edgeType: string;\n weight?: number | null;\n constraint?: EdgeMetadata[\"constraint\"] | null;\n normalization?: EdgeMetadata[\"normalization\"] | null;\n propagation?: EdgeMetadata[\"propagation\"] | null;\n conditionalA?: EdgeMetadata[\"conditionalA\"] | null;\n conditionalNotA?: EdgeMetadata[\"conditionalNotA\"] | null;\n tenantId?: string;\n workspaceId?: string;\n};\n\nexport type EdgePropagationSpec<TNodeId extends string = string> = {\n edgeType: PropagationEdgeType;\n direction: PropagationSpecDirection;\n transitivity: PropagationTransitivity;\n damping: number;\n maxHops: number | \"unbounded\";\n operator: (\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edge: PropagationEdgeRecord<TNodeId>,\n context: {\n hop: number;\n sourceNodeId: TNodeId;\n targetNodeId: TNodeId;\n traversedDirection: PropagationTraversalDirection;\n spec: EdgePropagationSpec<TNodeId>;\n }\n ) => PropagationResult | null;\n description: string;\n};\n\nexport function isPropagationTraversalDirection(\n direction: string\n): direction is PropagationTraversalDirection {\n return direction === \"outgoing\" || direction === \"incoming\";\n}\n\nexport function canTraverseHop<TNodeId extends string>(\n spec: EdgePropagationSpec<TNodeId>,\n nextHop: number\n): boolean {\n return spec.maxHops === \"unbounded\" || nextHop <= spec.maxHops;\n}\n\nexport function canContinueTransitively<TNodeId extends string>(\n spec: EdgePropagationSpec<TNodeId>,\n currentHop: number\n): boolean {\n if (spec.transitivity === \"none\") {\n return false;\n }\n\n return spec.maxHops === \"unbounded\" || currentHop < spec.maxHops;\n}\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// ../confidence/src/v1/operations/subjectiveLogic/index.ts
|
|
2
|
+
function opinion(belief, disbelief, uncertainty, baseRate = 0.5) {
|
|
3
|
+
const b = Math.max(0, Math.min(1, belief));
|
|
4
|
+
const d = Math.max(0, Math.min(1, disbelief));
|
|
5
|
+
const u = Math.max(0, Math.min(1, uncertainty));
|
|
6
|
+
const a = Math.max(0, Math.min(1, baseRate));
|
|
7
|
+
const sum = b + d + u;
|
|
8
|
+
if (sum === 0) {
|
|
9
|
+
return { b: 0, d: 0, u: 1, a };
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
b: b / sum,
|
|
13
|
+
d: d / sum,
|
|
14
|
+
u: u / sum,
|
|
15
|
+
a
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function vacuous(baseRate = 0.5) {
|
|
19
|
+
return { b: 0, d: 0, u: 1, a: baseRate };
|
|
20
|
+
}
|
|
21
|
+
function cumulativeFusion(left, right) {
|
|
22
|
+
if (left.u === 0 && right.u === 0) {
|
|
23
|
+
return opinion(
|
|
24
|
+
(left.b + right.b) / 2,
|
|
25
|
+
(left.d + right.d) / 2,
|
|
26
|
+
0,
|
|
27
|
+
(left.a + right.a) / 2
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
const k = left.u + right.u - left.u * right.u;
|
|
31
|
+
if (k === 0) {
|
|
32
|
+
return vacuous((left.a + right.a) / 2);
|
|
33
|
+
}
|
|
34
|
+
return opinion(
|
|
35
|
+
(left.b * right.u + right.b * left.u) / k,
|
|
36
|
+
(left.d * right.u + right.d * left.u) / k,
|
|
37
|
+
left.u * right.u / k,
|
|
38
|
+
(left.a + right.a) / 2
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function trustDiscount(sourceOpinion, trust) {
|
|
42
|
+
const weight = Math.max(0, Math.min(1, Math.abs(trust)));
|
|
43
|
+
return opinion(
|
|
44
|
+
weight * sourceOpinion.b,
|
|
45
|
+
weight * sourceOpinion.d,
|
|
46
|
+
1 - weight * (sourceOpinion.b + sourceOpinion.d),
|
|
47
|
+
sourceOpinion.a
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function negate(o) {
|
|
51
|
+
return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ../confidence/src/v1/operations/dynamics/defeat.ts
|
|
55
|
+
function applyNegativeEvidence(source, target, weight) {
|
|
56
|
+
const discounted = trustDiscount(negate(source), Math.abs(weight));
|
|
57
|
+
return {
|
|
58
|
+
opinion: cumulativeFusion(target, discounted),
|
|
59
|
+
operator: "cumulative_fusion",
|
|
60
|
+
rationale: `Contradicting evidence (weight=${weight.toFixed(2)})`
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/edges/utils.ts
|
|
65
|
+
function applyPerHopDamping(sourceOpinion, damping) {
|
|
66
|
+
if (damping >= 1) {
|
|
67
|
+
return sourceOpinion;
|
|
68
|
+
}
|
|
69
|
+
return trustDiscount(sourceOpinion, Math.max(0, damping));
|
|
70
|
+
}
|
|
71
|
+
function annotateRationale(result, spec, hop) {
|
|
72
|
+
return {
|
|
73
|
+
...result,
|
|
74
|
+
rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(
|
|
75
|
+
2
|
|
76
|
+
)} :: ${result.rationale}`
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function propagateNegativeInform(sourceOpinion, targetOpinion, edgeWeight) {
|
|
80
|
+
return applyNegativeEvidence(sourceOpinion, targetOpinion, edgeWeight);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/edges/refutes.ts
|
|
84
|
+
var refutesPropagationSpec = {
|
|
85
|
+
edgeType: "refutes",
|
|
86
|
+
direction: "outgoing",
|
|
87
|
+
transitivity: "none",
|
|
88
|
+
damping: 0.9,
|
|
89
|
+
maxHops: 1,
|
|
90
|
+
operator: (sourceOpinion, targetOpinion, edge, context) => {
|
|
91
|
+
const dampedSource = applyPerHopDamping(
|
|
92
|
+
sourceOpinion,
|
|
93
|
+
context.spec.damping
|
|
94
|
+
);
|
|
95
|
+
const negativeWeight = -Math.abs(edge.weight ?? 1);
|
|
96
|
+
const result = propagateNegativeInform(
|
|
97
|
+
dampedSource,
|
|
98
|
+
targetOpinion,
|
|
99
|
+
negativeWeight
|
|
100
|
+
);
|
|
101
|
+
return annotateRationale(result, context.spec, context.hop);
|
|
102
|
+
},
|
|
103
|
+
description: "Explicit negative evidence semantics. Refutes is treated as strong one-hop counter-evidence."
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export { refutesPropagationSpec };
|
|
107
|
+
//# sourceMappingURL=refutes.js.map
|
|
108
|
+
//# sourceMappingURL=refutes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../confidence/src/v1/operations/subjectiveLogic/index.ts","../../../confidence/src/v1/operations/dynamics/defeat.ts","../../src/edges/utils.ts","../../src/edges/refutes.ts"],"names":[],"mappings":";AAEO,SAAS,OAAA,CACd,MAAA,EACA,SAAA,EACA,WAAA,EACA,WAAmB,GAAA,EACV;AACT,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,MAAM,CAAC,CAAA;AACzC,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,SAAS,CAAC,CAAA;AAC5C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,WAAW,CAAC,CAAA;AAC9C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,QAAQ,CAAC,CAAA;AAC3C,EAAA,MAAM,GAAA,GAAM,IAAI,CAAA,GAAI,CAAA;AAEpB,EAAA,IAAI,QAAQ,CAAA,EAAG;AACb,IAAA,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAE;AAAA,EAC/B;AAEA,EAAA,OAAO;AAAA,IACL,GAAG,CAAA,GAAI,GAAA;AAAA,IACP,GAAG,CAAA,GAAI,GAAA;AAAA,IAC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O,SAAS,iBAAA,CACd,MAAA,EACA,IAAA,EACA,GAAA,EACmB;AACnB,EAAA,OAAO;AAAA,IACL,GAAG,MAAA;AAAA,IACH,SAAA,EAAW,OAAO,GAAG,CAAA,MAAA,EAAS,KAAK,QAAQ,CAAA,SAAA,EAAY,KAAK,OAAA,CAAQ,OAAA;AAAA,MAClE;AAAA,KACD,CAAA,IAAA,EAAO,MAAA,CAAO,SAAS,CAAA;AAAA,GAC1B;AACF;AA4CO,SAAS,uBAAA,CACd,aAAA,EACA,aAAA,EACA,UAAA,EACmB;AACnB,EAAA,OAAO,qBAAA,CAAsB,aAAA,EAAe,aAAA,EAAe,UAAU,CAAA;AACvE;;;AC5FO,IAAM,sBAAA,GAA8C;AAAA,EACzD,QAAA,EAAU,SAAA;AAAA,EACV,SAAA,EAAW,UAAA;AAAA,EACX,YAAA,EAAc,MAAA;AAAA,EACd,OAAA,EAAS,GAAA;AAAA,EACT,OAAA,EAAS,CAAA;AAAA,EACT,QAAA,EAAU,CAAC,aAAA,EAAe,aAAA,EAAe,MAAM,OAAA,KAAY;AACzD,IAAA,MAAM,YAAA,GAAe,kBAAA;AAAA,MACnB,aAAA;AAAA,MACA,QAAQ,IAAA,CAAK;AAAA,KACf;AACA,IAAA,MAAM,iBAAiB,CAAC,IAAA,CAAK,GAAA,CAAI,IAAA,CAAK,UAAU,CAAC,CAAA;AACjD,IAAA,MAAM,MAAA,GAAS,uBAAA;AAAA,MACb,YAAA;AAAA,MACA,aAAA;AAAA,MACA;AAAA,KACF;AAEA,IAAA,OAAO,iBAAA,CAAkB,MAAA,EAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,GAAG,CAAA;AAAA,EAC5D,CAAA;AAAA,EACA,WAAA,EACE;AACJ","file":"refutes.js","sourcesContent":["import type { Opinion } from \"../../types\";\n\nexport function opinion(\n belief: number,\n disbelief: number,\n uncertainty: number,\n baseRate: number = 0.5\n): Opinion {\n const b = Math.max(0, Math.min(1, belief));\n const d = Math.max(0, Math.min(1, disbelief));\n const u = Math.max(0, Math.min(1, uncertainty));\n const a = Math.max(0, Math.min(1, baseRate));\n const sum = b + d + u;\n\n if (sum === 0) {\n return { b: 0, d: 0, u: 1, a };\n }\n\n return {\n b: b / sum,\n d: d / sum,\n u: u / sum,\n a,\n };\n}\n\nexport function vacuous(baseRate: number = 0.5): Opinion {\n return { b: 0, d: 0, u: 1, a: baseRate };\n}\n\nexport function dogmatic(probability: number, baseRate: number = 0.5): Opinion {\n const p = Math.max(0, Math.min(1, probability));\n return { b: p, d: 1 - p, u: 0, a: baseRate };\n}\n\nexport function project(o: Opinion): number {\n return o.b + o.a * o.u;\n}\n\nexport function confidenceLevel(o: Opinion): \"high\" | \"medium\" | \"low\" {\n const projected = project(o);\n if (o.u > 0.5) {\n return \"low\";\n }\n if (projected >= 0.8 && o.u < 0.2) {\n return \"high\";\n }\n if (projected >= 0.6) {\n return \"medium\";\n }\n return \"low\";\n}\n\nexport function cumulativeFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u - left.u * right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function averagingFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (2 * left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function trustDiscount(sourceOpinion: Opinion, trust: number): Opinion {\n const weight = Math.max(0, Math.min(1, Math.abs(trust)));\n return opinion(\n weight * sourceOpinion.b,\n weight * sourceOpinion.d,\n 1 - weight * (sourceOpinion.b + sourceOpinion.d),\n sourceOpinion.a\n );\n}\n\nconst EPSILON = 1e-9;\n\nfunction childBaseRateFallback(\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number | undefined\n): number {\n if (fallbackBaseRate !== undefined) {\n return Math.max(0, Math.min(1, fallbackBaseRate));\n }\n\n if (Math.abs(ifTrue.a - ifFalse.a) <= EPSILON) {\n return ifTrue.a;\n }\n\n return (ifTrue.a + ifFalse.a) / 2;\n}\n\nfunction computeConditionalDeductionBaseRate(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number\n): number {\n const denominator =\n 1 - opinionA.a * ifTrue.u - (1 - opinionA.a) * ifFalse.u;\n\n if (ifTrue.u + ifFalse.u < 2 - EPSILON && Math.abs(denominator) > EPSILON) {\n const baseRate =\n (opinionA.a * ifTrue.b + (1 - opinionA.a) * ifFalse.b) / denominator;\n if (baseRate >= -EPSILON && baseRate <= 1 + EPSILON) {\n return Math.max(0, Math.min(1, baseRate));\n }\n }\n\n return fallbackBaseRate;\n}\n\nfunction safeCorrectionTerm(\n numerator: number,\n denominator: number\n): number | undefined {\n if (Math.abs(denominator) <= EPSILON) {\n return undefined;\n }\n\n const value = numerator / denominator;\n if (!Number.isFinite(value)) {\n return undefined;\n }\n\n return Math.max(0, value);\n}\n\nexport function conditionalDeduction(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate?: number\n): Opinion {\n const fallbackChildBaseRate = childBaseRateFallback(\n ifTrue,\n ifFalse,\n fallbackBaseRate\n );\n const childBaseRate = computeConditionalDeductionBaseRate(\n opinionA,\n ifTrue,\n ifFalse,\n fallbackChildBaseRate\n );\n const projectedAntecedent = project(opinionA);\n const projectedAntecedentComplement = 1 - projectedAntecedent;\n const intermediateBelief =\n opinionA.b * ifTrue.b +\n opinionA.d * ifFalse.b +\n opinionA.u * (ifTrue.b * opinionA.a + ifFalse.b * (1 - opinionA.a));\n const intermediateDisbelief =\n opinionA.b * ifTrue.d +\n opinionA.d * ifFalse.d +\n opinionA.u * (ifTrue.d * opinionA.a + ifFalse.d * (1 - opinionA.a));\n const intermediateUncertainty =\n opinionA.b * ifTrue.u +\n opinionA.d * ifFalse.u +\n opinionA.u * (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedVacuousDeduction =\n ifTrue.b * opinionA.a +\n ifFalse.b * (1 - opinionA.a) +\n childBaseRate *\n (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedConditionalA =\n ifTrue.b + childBaseRate * (1 - ifTrue.b - ifTrue.d);\n let correction = 0;\n\n if (\n (ifTrue.b > ifFalse.b && ifTrue.d > ifFalse.d) ||\n (ifTrue.b <= ifFalse.b && ifTrue.d <= ifFalse.d)\n ) {\n correction = 0;\n } else if (ifTrue.b > ifFalse.b && ifTrue.d <= ifFalse.d) {\n const beliefGap = ifTrue.b - ifFalse.b;\n const disbeliefGap = ifFalse.d - ifTrue.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedent * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d) * beliefGap,\n projectedAntecedentComplement * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedent * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedentComplement * (1 - childBaseRate)\n ) ?? 0;\n }\n } else {\n const beliefGap = ifFalse.b - ifTrue.b;\n const disbeliefGap = ifTrue.d - ifFalse.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateDisbelief - ifTrue.d) *\n beliefGap,\n projectedAntecedent * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedentComplement * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedent * (1 - childBaseRate)\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n opinionA.a *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedentComplement * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n }\n }\n\n return opinion(\n intermediateBelief - childBaseRate * correction,\n intermediateDisbelief - (1 - childBaseRate) * correction,\n intermediateUncertainty + correction,\n childBaseRate\n );\n}\n\n/**\n * Abductive inference over a conditional.\n * Given an opinion on Y and conditionals P(Y|X), P(Y|~X), infer an opinion on X.\n */\nexport function conditionalAbduction(\n opinionY: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n baseRateX: number\n): Opinion {\n const priorX = Math.max(0, Math.min(1, baseRateX));\n const probabilityY = project(opinionY);\n const probabilityGivenTrue = project(ifTrue);\n const probabilityGivenFalse = project(ifFalse);\n\n const posteriorIfYDenominator =\n probabilityGivenTrue * priorX +\n probabilityGivenFalse * (1 - priorX);\n const posteriorIfY =\n posteriorIfYDenominator === 0\n ? priorX\n : (probabilityGivenTrue * priorX) / posteriorIfYDenominator;\n\n const posteriorIfNotYDenominator =\n (1 - probabilityGivenTrue) * priorX +\n (1 - probabilityGivenFalse) * (1 - priorX);\n const posteriorIfNotY =\n posteriorIfNotYDenominator === 0\n ? priorX\n : ((1 - probabilityGivenTrue) * priorX) / posteriorIfNotYDenominator;\n\n const projectedX =\n probabilityY * posteriorIfY + (1 - probabilityY) * posteriorIfNotY;\n const uncertainty = Math.max(\n opinionY.u * 0.5,\n probabilityY * ifTrue.u + (1 - probabilityY) * ifFalse.u\n );\n\n return opinion(\n projectedX * (1 - uncertainty),\n (1 - projectedX) * (1 - uncertainty),\n uncertainty,\n priorX\n );\n}\n\nexport function negate(o: Opinion): Opinion {\n return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };\n}\n\nexport function constraintFusion(\n left: Opinion,\n right: Opinion,\n mode: \"pressure\" | \"redistribute\" = \"pressure\"\n): { o1: Opinion; o2: Opinion } {\n if (mode === \"redistribute\") {\n const leftProjected = project(left);\n const rightProjected = project(right);\n const total = leftProjected + rightProjected;\n\n if (total <= 1) {\n return { o1: left, o2: right };\n }\n\n const scale = 1 / total;\n return {\n o1: opinion(\n left.b * scale,\n left.d + left.b * (1 - scale),\n left.u,\n left.a\n ),\n o2: opinion(\n right.b * scale,\n right.d + right.b * (1 - scale),\n right.u,\n right.a\n ),\n };\n }\n\n const pressureLeft = right.b * 0.5;\n const pressureRight = left.b * 0.5;\n\n return {\n o1: opinion(\n left.b - pressureLeft * 0.3,\n left.d + pressureLeft * 0.3,\n left.u,\n left.a\n ),\n o2: opinion(\n right.b - pressureRight * 0.3,\n right.d + pressureRight * 0.3,\n right.u,\n right.a\n ),\n };\n}\n\nexport function evidenceBalance(o: Opinion): number {\n const total = o.b + o.d;\n if (total === 0) {\n return 0;\n }\n return (o.b - o.d) / total;\n}\n\nexport function areTensioned(left: Opinion, right: Opinion): boolean {\n return (\n project(left) > 0.5 &&\n project(right) > 0.5 &&\n (left.d > 0.2 || right.d > 0.2)\n );\n}\n\nexport function informationGain(o: Opinion): number {\n if (o.u === 0) {\n return 0;\n }\n if (o.u === 1) {\n return 1;\n }\n return o.u * (1 - Math.abs(o.b - o.d));\n}\n","import type { EdgeMetadata, Opinion, PropagationResult } from \"../../types\";\nimport {\n constraintFusion,\n cumulativeFusion,\n negate,\n project,\n trustDiscount,\n} from \"../subjectiveLogic\";\n\nexport function applyNegativeSupport(\n source: Opinion,\n target: Opinion,\n weight: number,\n metadata: EdgeMetadata = {}\n): PropagationResult {\n if (metadata.constraint === \"xor\") {\n const result = constraintFusion(\n source,\n target,\n metadata.normalization ?? \"pressure\"\n );\n return {\n opinion: result.o2,\n operator: \"constraint_fusion\",\n rationale: `XOR constraint: source belief at ${project(source).toFixed(\n 2\n )} pressures target`,\n };\n }\n\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(\n 2\n )}) from source at ${project(source).toFixed(2)}`,\n };\n}\n\nexport function applyNegativeEvidence(\n source: Opinion,\n target: Opinion,\n weight: number\n): PropagationResult {\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(2)})`,\n };\n}\n","import {\n applyNegativeEvidence,\n applyNegativeSupport,\n cumulativeFusion,\n project,\n trustDiscount,\n type EdgeMetadata,\n type Opinion,\n type PropagationResult,\n} from \"@lucern/confidence\";\nimport type {\n EdgePropagationSpec,\n PropagationEdgeRecord,\n} from \"./propagationTypes\";\n\nexport function readEdgeMetadata<TNodeId extends string>(\n edge: PropagationEdgeRecord<TNodeId>\n): EdgeMetadata {\n return {\n constraint: edge.constraint ?? undefined,\n normalization: edge.normalization ?? undefined,\n propagation: edge.propagation ?? undefined,\n conditionalA: edge.conditionalA ?? undefined,\n conditionalNotA: edge.conditionalNotA ?? undefined,\n };\n}\n\nexport function applyPerHopDamping(\n sourceOpinion: Opinion,\n damping: number\n): Opinion {\n if (damping >= 1) {\n return sourceOpinion;\n }\n\n return trustDiscount(sourceOpinion, Math.max(0, damping));\n}\n\nexport function annotateRationale(\n result: PropagationResult,\n spec: EdgePropagationSpec,\n hop: number\n): PropagationResult {\n return {\n ...result,\n rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(\n 2\n )} :: ${result.rationale}`,\n };\n}\n\nexport function propagatePositiveSupport(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(\n 2\n )}) from source at ${project(sourceOpinion).toFixed(2)}`,\n };\n}\n\nexport function propagatePositiveInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(2)})`,\n };\n}\n\nexport function propagateNegativeSupportWithMetadata(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number,\n edge: PropagationEdgeRecord\n): PropagationResult {\n return applyNegativeSupport(\n sourceOpinion,\n targetOpinion,\n edgeWeight,\n readEdgeMetadata(edge)\n );\n}\n\nexport function propagateNegativeInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n return applyNegativeEvidence(sourceOpinion, targetOpinion, edgeWeight);\n}\n\n","import type { EdgePropagationSpec } from \"./propagationTypes\";\nimport {\n annotateRationale,\n applyPerHopDamping,\n propagateNegativeInform,\n} from \"./utils\";\n\nexport const refutesPropagationSpec: EdgePropagationSpec = {\n edgeType: \"refutes\",\n direction: \"outgoing\",\n transitivity: \"none\",\n damping: 0.9,\n maxHops: 1,\n operator: (sourceOpinion, targetOpinion, edge, context) => {\n const dampedSource = applyPerHopDamping(\n sourceOpinion,\n context.spec.damping\n );\n const negativeWeight = -Math.abs(edge.weight ?? 1);\n const result = propagateNegativeInform(\n dampedSource,\n targetOpinion,\n negativeWeight\n );\n\n return annotateRationale(result, context.spec, context.hop);\n },\n description:\n \"Explicit negative evidence semantics. Refutes is treated as strong one-hop counter-evidence.\",\n};\n\n"]}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// ../confidence/src/v1/operations/subjectiveLogic/index.ts
|
|
2
|
+
function opinion(belief, disbelief, uncertainty, baseRate = 0.5) {
|
|
3
|
+
const b = Math.max(0, Math.min(1, belief));
|
|
4
|
+
const d = Math.max(0, Math.min(1, disbelief));
|
|
5
|
+
const u = Math.max(0, Math.min(1, uncertainty));
|
|
6
|
+
const a = Math.max(0, Math.min(1, baseRate));
|
|
7
|
+
const sum = b + d + u;
|
|
8
|
+
if (sum === 0) {
|
|
9
|
+
return { b: 0, d: 0, u: 1, a };
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
b: b / sum,
|
|
13
|
+
d: d / sum,
|
|
14
|
+
u: u / sum,
|
|
15
|
+
a
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function vacuous(baseRate = 0.5) {
|
|
19
|
+
return { b: 0, d: 0, u: 1, a: baseRate };
|
|
20
|
+
}
|
|
21
|
+
function project(o) {
|
|
22
|
+
return o.b + o.a * o.u;
|
|
23
|
+
}
|
|
24
|
+
function cumulativeFusion(left, right) {
|
|
25
|
+
if (left.u === 0 && right.u === 0) {
|
|
26
|
+
return opinion(
|
|
27
|
+
(left.b + right.b) / 2,
|
|
28
|
+
(left.d + right.d) / 2,
|
|
29
|
+
0,
|
|
30
|
+
(left.a + right.a) / 2
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
const k = left.u + right.u - left.u * right.u;
|
|
34
|
+
if (k === 0) {
|
|
35
|
+
return vacuous((left.a + right.a) / 2);
|
|
36
|
+
}
|
|
37
|
+
return opinion(
|
|
38
|
+
(left.b * right.u + right.b * left.u) / k,
|
|
39
|
+
(left.d * right.u + right.d * left.u) / k,
|
|
40
|
+
left.u * right.u / k,
|
|
41
|
+
(left.a + right.a) / 2
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function trustDiscount(sourceOpinion, trust) {
|
|
45
|
+
const weight = Math.max(0, Math.min(1, Math.abs(trust)));
|
|
46
|
+
return opinion(
|
|
47
|
+
weight * sourceOpinion.b,
|
|
48
|
+
weight * sourceOpinion.d,
|
|
49
|
+
1 - weight * (sourceOpinion.b + sourceOpinion.d),
|
|
50
|
+
sourceOpinion.a
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
function negate(o) {
|
|
54
|
+
return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };
|
|
55
|
+
}
|
|
56
|
+
function constraintFusion(left, right, mode = "pressure") {
|
|
57
|
+
if (mode === "redistribute") {
|
|
58
|
+
const leftProjected = project(left);
|
|
59
|
+
const rightProjected = project(right);
|
|
60
|
+
const total = leftProjected + rightProjected;
|
|
61
|
+
if (total <= 1) {
|
|
62
|
+
return { o1: left, o2: right };
|
|
63
|
+
}
|
|
64
|
+
const scale = 1 / total;
|
|
65
|
+
return {
|
|
66
|
+
o1: opinion(
|
|
67
|
+
left.b * scale,
|
|
68
|
+
left.d + left.b * (1 - scale),
|
|
69
|
+
left.u,
|
|
70
|
+
left.a
|
|
71
|
+
),
|
|
72
|
+
o2: opinion(
|
|
73
|
+
right.b * scale,
|
|
74
|
+
right.d + right.b * (1 - scale),
|
|
75
|
+
right.u,
|
|
76
|
+
right.a
|
|
77
|
+
)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const pressureLeft = right.b * 0.5;
|
|
81
|
+
const pressureRight = left.b * 0.5;
|
|
82
|
+
return {
|
|
83
|
+
o1: opinion(
|
|
84
|
+
left.b - pressureLeft * 0.3,
|
|
85
|
+
left.d + pressureLeft * 0.3,
|
|
86
|
+
left.u,
|
|
87
|
+
left.a
|
|
88
|
+
),
|
|
89
|
+
o2: opinion(
|
|
90
|
+
right.b - pressureRight * 0.3,
|
|
91
|
+
right.d + pressureRight * 0.3,
|
|
92
|
+
right.u,
|
|
93
|
+
right.a
|
|
94
|
+
)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ../confidence/src/v1/operations/dynamics/defeat.ts
|
|
99
|
+
function applyNegativeSupport(source, target, weight, metadata = {}) {
|
|
100
|
+
if (metadata.constraint === "xor") {
|
|
101
|
+
const result = constraintFusion(
|
|
102
|
+
source,
|
|
103
|
+
target,
|
|
104
|
+
metadata.normalization ?? "pressure"
|
|
105
|
+
);
|
|
106
|
+
return {
|
|
107
|
+
opinion: result.o2,
|
|
108
|
+
operator: "constraint_fusion",
|
|
109
|
+
rationale: `XOR constraint: source belief at ${project(source).toFixed(
|
|
110
|
+
2
|
|
111
|
+
)} pressures target`
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const discounted = trustDiscount(negate(source), Math.abs(weight));
|
|
115
|
+
return {
|
|
116
|
+
opinion: cumulativeFusion(target, discounted),
|
|
117
|
+
operator: "cumulative_fusion",
|
|
118
|
+
rationale: `Contradicting evidence (weight=${weight.toFixed(
|
|
119
|
+
2
|
|
120
|
+
)}) from source at ${project(source).toFixed(2)}`
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/edges/utils.ts
|
|
125
|
+
function readEdgeMetadata(edge) {
|
|
126
|
+
return {
|
|
127
|
+
constraint: edge.constraint ?? void 0,
|
|
128
|
+
normalization: edge.normalization ?? void 0,
|
|
129
|
+
propagation: edge.propagation ?? void 0,
|
|
130
|
+
conditionalA: edge.conditionalA ?? void 0,
|
|
131
|
+
conditionalNotA: edge.conditionalNotA ?? void 0
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function applyPerHopDamping(sourceOpinion, damping) {
|
|
135
|
+
if (damping >= 1) {
|
|
136
|
+
return sourceOpinion;
|
|
137
|
+
}
|
|
138
|
+
return trustDiscount(sourceOpinion, Math.max(0, damping));
|
|
139
|
+
}
|
|
140
|
+
function annotateRationale(result, spec, hop) {
|
|
141
|
+
return {
|
|
142
|
+
...result,
|
|
143
|
+
rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(
|
|
144
|
+
2
|
|
145
|
+
)} :: ${result.rationale}`
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function propagatePositiveSupport(sourceOpinion, targetOpinion, edgeWeight) {
|
|
149
|
+
const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));
|
|
150
|
+
return {
|
|
151
|
+
opinion: cumulativeFusion(targetOpinion, discounted),
|
|
152
|
+
operator: "cumulative_fusion",
|
|
153
|
+
rationale: `Supporting evidence (weight=${edgeWeight.toFixed(
|
|
154
|
+
2
|
|
155
|
+
)}) from source at ${project(sourceOpinion).toFixed(2)}`
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function propagateNegativeSupportWithMetadata(sourceOpinion, targetOpinion, edgeWeight, edge) {
|
|
159
|
+
return applyNegativeSupport(
|
|
160
|
+
sourceOpinion,
|
|
161
|
+
targetOpinion,
|
|
162
|
+
edgeWeight,
|
|
163
|
+
readEdgeMetadata(edge)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/edges/supports.ts
|
|
168
|
+
var supportsPropagationSpec = {
|
|
169
|
+
edgeType: "supports",
|
|
170
|
+
direction: "outgoing",
|
|
171
|
+
transitivity: "full",
|
|
172
|
+
damping: 0.85,
|
|
173
|
+
maxHops: "unbounded",
|
|
174
|
+
operator: (sourceOpinion, targetOpinion, edge, context) => {
|
|
175
|
+
const dampedSource = applyPerHopDamping(
|
|
176
|
+
sourceOpinion,
|
|
177
|
+
context.spec.damping
|
|
178
|
+
);
|
|
179
|
+
const weight = edge.weight ?? 1;
|
|
180
|
+
const result = weight < 0 ? propagateNegativeSupportWithMetadata(
|
|
181
|
+
dampedSource,
|
|
182
|
+
targetOpinion,
|
|
183
|
+
weight,
|
|
184
|
+
edge
|
|
185
|
+
) : propagatePositiveSupport(dampedSource, targetOpinion, weight);
|
|
186
|
+
return annotateRationale(result, context.spec, context.hop);
|
|
187
|
+
},
|
|
188
|
+
description: "Belief-to-belief influence. Supports chains transitively with moderate per-hop damping."
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export { supportsPropagationSpec };
|
|
192
|
+
//# sourceMappingURL=supports.js.map
|
|
193
|
+
//# sourceMappingURL=supports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../confidence/src/v1/operations/subjectiveLogic/index.ts","../../../confidence/src/v1/operations/dynamics/defeat.ts","../../src/edges/utils.ts","../../src/edges/supports.ts"],"names":[],"mappings":";AAEO,SAAS,OAAA,CACd,MAAA,EACA,SAAA,EACA,WAAA,EACA,WAAmB,GAAA,EACV;AACT,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,MAAM,CAAC,CAAA;AACzC,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,SAAS,CAAC,CAAA;AAC5C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,CAAA,EAAG,WAAW,CAAC,CAAA;AAC9C,EAAA,MAAM,CAAA,GAAI,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,GAAA,CAAI,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;AAAA,EACT,QAAA,EAAU,CAAC,aAAA,EAAe,aAAA,EAAe,MAAM,OAAA,KAAY;AACzD,IAAA,MAAM,YAAA,GAAe,kBAAA;AAAA,MACnB,aAAA;AAAA,MACA,QAAQ,IAAA,CAAK;AAAA,KACf;AACA,IAAA,MAAM,MAAA,GAAS,KAAK,MAAA,IAAU,CAAA;AAC9B,IAAA,MAAM,MAAA,GACJ,SAAS,CAAA,GACL,oCAAA;AAAA,MACE,YAAA;AAAA,MACA,aAAA;AAAA,MACA,MAAA;AAAA,MACA;AAAA,KACF,GACA,wBAAA,CAAyB,YAAA,EAAc,aAAA,EAAe,MAAM,CAAA;AAElE,IAAA,OAAO,iBAAA,CAAkB,MAAA,EAAQ,OAAA,CAAQ,IAAA,EAAM,QAAQ,GAAG,CAAA;AAAA,EAC5D,CAAA;AAAA,EACA,WAAA,EACE;AACJ","file":"supports.js","sourcesContent":["import type { Opinion } from \"../../types\";\n\nexport function opinion(\n belief: number,\n disbelief: number,\n uncertainty: number,\n baseRate: number = 0.5\n): Opinion {\n const b = Math.max(0, Math.min(1, belief));\n const d = Math.max(0, Math.min(1, disbelief));\n const u = Math.max(0, Math.min(1, uncertainty));\n const a = Math.max(0, Math.min(1, baseRate));\n const sum = b + d + u;\n\n if (sum === 0) {\n return { b: 0, d: 0, u: 1, a };\n }\n\n return {\n b: b / sum,\n d: d / sum,\n u: u / sum,\n a,\n };\n}\n\nexport function vacuous(baseRate: number = 0.5): Opinion {\n return { b: 0, d: 0, u: 1, a: baseRate };\n}\n\nexport function dogmatic(probability: number, baseRate: number = 0.5): Opinion {\n const p = Math.max(0, Math.min(1, probability));\n return { b: p, d: 1 - p, u: 0, a: baseRate };\n}\n\nexport function project(o: Opinion): number {\n return o.b + o.a * o.u;\n}\n\nexport function confidenceLevel(o: Opinion): \"high\" | \"medium\" | \"low\" {\n const projected = project(o);\n if (o.u > 0.5) {\n return \"low\";\n }\n if (projected >= 0.8 && o.u < 0.2) {\n return \"high\";\n }\n if (projected >= 0.6) {\n return \"medium\";\n }\n return \"low\";\n}\n\nexport function cumulativeFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u - left.u * right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function averagingFusion(left: Opinion, right: Opinion): Opinion {\n if (left.u === 0 && right.u === 0) {\n return opinion(\n (left.b + right.b) / 2,\n (left.d + right.d) / 2,\n 0,\n (left.a + right.a) / 2\n );\n }\n\n const k = left.u + right.u;\n if (k === 0) {\n return vacuous((left.a + right.a) / 2);\n }\n\n return opinion(\n (left.b * right.u + right.b * left.u) / k,\n (left.d * right.u + right.d * left.u) / k,\n (2 * left.u * right.u) / k,\n (left.a + right.a) / 2\n );\n}\n\nexport function trustDiscount(sourceOpinion: Opinion, trust: number): Opinion {\n const weight = Math.max(0, Math.min(1, Math.abs(trust)));\n return opinion(\n weight * sourceOpinion.b,\n weight * sourceOpinion.d,\n 1 - weight * (sourceOpinion.b + sourceOpinion.d),\n sourceOpinion.a\n );\n}\n\nconst EPSILON = 1e-9;\n\nfunction childBaseRateFallback(\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number | undefined\n): number {\n if (fallbackBaseRate !== undefined) {\n return Math.max(0, Math.min(1, fallbackBaseRate));\n }\n\n if (Math.abs(ifTrue.a - ifFalse.a) <= EPSILON) {\n return ifTrue.a;\n }\n\n return (ifTrue.a + ifFalse.a) / 2;\n}\n\nfunction computeConditionalDeductionBaseRate(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate: number\n): number {\n const denominator =\n 1 - opinionA.a * ifTrue.u - (1 - opinionA.a) * ifFalse.u;\n\n if (ifTrue.u + ifFalse.u < 2 - EPSILON && Math.abs(denominator) > EPSILON) {\n const baseRate =\n (opinionA.a * ifTrue.b + (1 - opinionA.a) * ifFalse.b) / denominator;\n if (baseRate >= -EPSILON && baseRate <= 1 + EPSILON) {\n return Math.max(0, Math.min(1, baseRate));\n }\n }\n\n return fallbackBaseRate;\n}\n\nfunction safeCorrectionTerm(\n numerator: number,\n denominator: number\n): number | undefined {\n if (Math.abs(denominator) <= EPSILON) {\n return undefined;\n }\n\n const value = numerator / denominator;\n if (!Number.isFinite(value)) {\n return undefined;\n }\n\n return Math.max(0, value);\n}\n\nexport function conditionalDeduction(\n opinionA: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n fallbackBaseRate?: number\n): Opinion {\n const fallbackChildBaseRate = childBaseRateFallback(\n ifTrue,\n ifFalse,\n fallbackBaseRate\n );\n const childBaseRate = computeConditionalDeductionBaseRate(\n opinionA,\n ifTrue,\n ifFalse,\n fallbackChildBaseRate\n );\n const projectedAntecedent = project(opinionA);\n const projectedAntecedentComplement = 1 - projectedAntecedent;\n const intermediateBelief =\n opinionA.b * ifTrue.b +\n opinionA.d * ifFalse.b +\n opinionA.u * (ifTrue.b * opinionA.a + ifFalse.b * (1 - opinionA.a));\n const intermediateDisbelief =\n opinionA.b * ifTrue.d +\n opinionA.d * ifFalse.d +\n opinionA.u * (ifTrue.d * opinionA.a + ifFalse.d * (1 - opinionA.a));\n const intermediateUncertainty =\n opinionA.b * ifTrue.u +\n opinionA.d * ifFalse.u +\n opinionA.u * (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedVacuousDeduction =\n ifTrue.b * opinionA.a +\n ifFalse.b * (1 - opinionA.a) +\n childBaseRate *\n (ifTrue.u * opinionA.a + ifFalse.u * (1 - opinionA.a));\n const projectedConditionalA =\n ifTrue.b + childBaseRate * (1 - ifTrue.b - ifTrue.d);\n let correction = 0;\n\n if (\n (ifTrue.b > ifFalse.b && ifTrue.d > ifFalse.d) ||\n (ifTrue.b <= ifFalse.b && ifTrue.d <= ifFalse.d)\n ) {\n correction = 0;\n } else if (ifTrue.b > ifFalse.b && ifTrue.d <= ifFalse.d) {\n const beliefGap = ifTrue.b - ifFalse.b;\n const disbeliefGap = ifFalse.d - ifTrue.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedent * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d) * beliefGap,\n projectedAntecedentComplement * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedent * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedentComplement * (1 - childBaseRate)\n ) ?? 0;\n }\n } else {\n const beliefGap = ifFalse.b - ifTrue.b;\n const disbeliefGap = ifTrue.d - ifFalse.d;\n\n if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) *\n opinionA.u *\n (intermediateDisbelief - ifTrue.d) *\n beliefGap,\n projectedAntecedent * childBaseRate * disbeliefGap\n ) ?? 0;\n } else if (\n projectedVacuousDeduction <= projectedConditionalA &&\n projectedAntecedent > opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n (1 - opinionA.a) * opinionA.u * (intermediateBelief - ifTrue.b),\n projectedAntecedentComplement * childBaseRate\n ) ?? 0;\n } else if (\n projectedVacuousDeduction > projectedConditionalA &&\n projectedAntecedent <= opinionA.a\n ) {\n correction =\n safeCorrectionTerm(\n opinionA.a * opinionA.u * (intermediateDisbelief - ifTrue.d),\n projectedAntecedent * (1 - childBaseRate)\n ) ?? 0;\n } else {\n correction =\n safeCorrectionTerm(\n opinionA.a *\n opinionA.u *\n (intermediateBelief - ifTrue.b) *\n disbeliefGap,\n projectedAntecedentComplement * (1 - childBaseRate) * beliefGap\n ) ?? 0;\n }\n }\n\n return opinion(\n intermediateBelief - childBaseRate * correction,\n intermediateDisbelief - (1 - childBaseRate) * correction,\n intermediateUncertainty + correction,\n childBaseRate\n );\n}\n\n/**\n * Abductive inference over a conditional.\n * Given an opinion on Y and conditionals P(Y|X), P(Y|~X), infer an opinion on X.\n */\nexport function conditionalAbduction(\n opinionY: Opinion,\n ifTrue: Opinion,\n ifFalse: Opinion,\n baseRateX: number\n): Opinion {\n const priorX = Math.max(0, Math.min(1, baseRateX));\n const probabilityY = project(opinionY);\n const probabilityGivenTrue = project(ifTrue);\n const probabilityGivenFalse = project(ifFalse);\n\n const posteriorIfYDenominator =\n probabilityGivenTrue * priorX +\n probabilityGivenFalse * (1 - priorX);\n const posteriorIfY =\n posteriorIfYDenominator === 0\n ? priorX\n : (probabilityGivenTrue * priorX) / posteriorIfYDenominator;\n\n const posteriorIfNotYDenominator =\n (1 - probabilityGivenTrue) * priorX +\n (1 - probabilityGivenFalse) * (1 - priorX);\n const posteriorIfNotY =\n posteriorIfNotYDenominator === 0\n ? priorX\n : ((1 - probabilityGivenTrue) * priorX) / posteriorIfNotYDenominator;\n\n const projectedX =\n probabilityY * posteriorIfY + (1 - probabilityY) * posteriorIfNotY;\n const uncertainty = Math.max(\n opinionY.u * 0.5,\n probabilityY * ifTrue.u + (1 - probabilityY) * ifFalse.u\n );\n\n return opinion(\n projectedX * (1 - uncertainty),\n (1 - projectedX) * (1 - uncertainty),\n uncertainty,\n priorX\n );\n}\n\nexport function negate(o: Opinion): Opinion {\n return { b: o.d, d: o.b, u: o.u, a: 1 - o.a };\n}\n\nexport function constraintFusion(\n left: Opinion,\n right: Opinion,\n mode: \"pressure\" | \"redistribute\" = \"pressure\"\n): { o1: Opinion; o2: Opinion } {\n if (mode === \"redistribute\") {\n const leftProjected = project(left);\n const rightProjected = project(right);\n const total = leftProjected + rightProjected;\n\n if (total <= 1) {\n return { o1: left, o2: right };\n }\n\n const scale = 1 / total;\n return {\n o1: opinion(\n left.b * scale,\n left.d + left.b * (1 - scale),\n left.u,\n left.a\n ),\n o2: opinion(\n right.b * scale,\n right.d + right.b * (1 - scale),\n right.u,\n right.a\n ),\n };\n }\n\n const pressureLeft = right.b * 0.5;\n const pressureRight = left.b * 0.5;\n\n return {\n o1: opinion(\n left.b - pressureLeft * 0.3,\n left.d + pressureLeft * 0.3,\n left.u,\n left.a\n ),\n o2: opinion(\n right.b - pressureRight * 0.3,\n right.d + pressureRight * 0.3,\n right.u,\n right.a\n ),\n };\n}\n\nexport function evidenceBalance(o: Opinion): number {\n const total = o.b + o.d;\n if (total === 0) {\n return 0;\n }\n return (o.b - o.d) / total;\n}\n\nexport function areTensioned(left: Opinion, right: Opinion): boolean {\n return (\n project(left) > 0.5 &&\n project(right) > 0.5 &&\n (left.d > 0.2 || right.d > 0.2)\n );\n}\n\nexport function informationGain(o: Opinion): number {\n if (o.u === 0) {\n return 0;\n }\n if (o.u === 1) {\n return 1;\n }\n return o.u * (1 - Math.abs(o.b - o.d));\n}\n","import type { EdgeMetadata, Opinion, PropagationResult } from \"../../types\";\nimport {\n constraintFusion,\n cumulativeFusion,\n negate,\n project,\n trustDiscount,\n} from \"../subjectiveLogic\";\n\nexport function applyNegativeSupport(\n source: Opinion,\n target: Opinion,\n weight: number,\n metadata: EdgeMetadata = {}\n): PropagationResult {\n if (metadata.constraint === \"xor\") {\n const result = constraintFusion(\n source,\n target,\n metadata.normalization ?? \"pressure\"\n );\n return {\n opinion: result.o2,\n operator: \"constraint_fusion\",\n rationale: `XOR constraint: source belief at ${project(source).toFixed(\n 2\n )} pressures target`,\n };\n }\n\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(\n 2\n )}) from source at ${project(source).toFixed(2)}`,\n };\n}\n\nexport function applyNegativeEvidence(\n source: Opinion,\n target: Opinion,\n weight: number\n): PropagationResult {\n const discounted = trustDiscount(negate(source), Math.abs(weight));\n return {\n opinion: cumulativeFusion(target, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Contradicting evidence (weight=${weight.toFixed(2)})`,\n };\n}\n","import {\n applyNegativeEvidence,\n applyNegativeSupport,\n cumulativeFusion,\n project,\n trustDiscount,\n type EdgeMetadata,\n type Opinion,\n type PropagationResult,\n} from \"@lucern/confidence\";\nimport type {\n EdgePropagationSpec,\n PropagationEdgeRecord,\n} from \"./propagationTypes\";\n\nexport function readEdgeMetadata<TNodeId extends string>(\n edge: PropagationEdgeRecord<TNodeId>\n): EdgeMetadata {\n return {\n constraint: edge.constraint ?? undefined,\n normalization: edge.normalization ?? undefined,\n propagation: edge.propagation ?? undefined,\n conditionalA: edge.conditionalA ?? undefined,\n conditionalNotA: edge.conditionalNotA ?? undefined,\n };\n}\n\nexport function applyPerHopDamping(\n sourceOpinion: Opinion,\n damping: number\n): Opinion {\n if (damping >= 1) {\n return sourceOpinion;\n }\n\n return trustDiscount(sourceOpinion, Math.max(0, damping));\n}\n\nexport function annotateRationale(\n result: PropagationResult,\n spec: EdgePropagationSpec,\n hop: number\n): PropagationResult {\n return {\n ...result,\n rationale: `hop=${hop} edge=${spec.edgeType} damping=${spec.damping.toFixed(\n 2\n )} :: ${result.rationale}`,\n };\n}\n\nexport function propagatePositiveSupport(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(\n 2\n )}) from source at ${project(sourceOpinion).toFixed(2)}`,\n };\n}\n\nexport function propagatePositiveInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n const discounted = trustDiscount(sourceOpinion, Math.abs(edgeWeight));\n return {\n opinion: cumulativeFusion(targetOpinion, discounted),\n operator: \"cumulative_fusion\",\n rationale: `Supporting evidence (weight=${edgeWeight.toFixed(2)})`,\n };\n}\n\nexport function propagateNegativeSupportWithMetadata(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number,\n edge: PropagationEdgeRecord\n): PropagationResult {\n return applyNegativeSupport(\n sourceOpinion,\n targetOpinion,\n edgeWeight,\n readEdgeMetadata(edge)\n );\n}\n\nexport function propagateNegativeInform(\n sourceOpinion: Opinion,\n targetOpinion: Opinion,\n edgeWeight: number\n): PropagationResult {\n return applyNegativeEvidence(sourceOpinion, targetOpinion, edgeWeight);\n}\n\n","import type { EdgePropagationSpec } from \"./propagationTypes\";\nimport {\n annotateRationale,\n applyPerHopDamping,\n propagateNegativeSupportWithMetadata,\n propagatePositiveSupport,\n} from \"./utils\";\n\nexport const supportsPropagationSpec: EdgePropagationSpec = {\n edgeType: \"supports\",\n direction: \"outgoing\",\n transitivity: \"full\",\n damping: 0.85,\n maxHops: \"unbounded\",\n operator: (sourceOpinion, targetOpinion, edge, context) => {\n const dampedSource = applyPerHopDamping(\n sourceOpinion,\n context.spec.damping\n );\n const weight = edge.weight ?? 1;\n const result =\n weight < 0\n ? propagateNegativeSupportWithMetadata(\n dampedSource,\n targetOpinion,\n weight,\n edge\n )\n : propagatePositiveSupport(dampedSource, targetOpinion, weight);\n\n return annotateRationale(result, context.spec, context.hop);\n },\n description:\n \"Belief-to-belief influence. Supports chains transitively with moderate per-hop damping.\",\n};\n\n"]}
|