Skip to content

Commit d3e5af4

Browse files
committed
fix trust after mathjs upgrade
1 parent bf54044 commit d3e5af4

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

worker/trust.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ export async function trust ({ boss, models }) {
1818
const MAX_DEPTH = 10
1919
const MAX_TRUST = 1
2020
const MIN_SUCCESS = 1
21-
// increasing disgree_mult increases distrust when there's disagreement
22-
// ... this cancels DISAGREE_MULT number of "successes" for every disagreement
23-
const DISAGREE_MULT = 10
2421
// https://en.wikipedia.org/wiki/Normal_distribution#Quantile_function
2522
const Z_CONFIDENCE = 6.109410204869 // 99.9999999% confidence
2623
const GLOBAL_ROOT = 616
27-
const SEED_WEIGHT = 0.25
24+
const SEED_WEIGHT = 1.0
2825
const AGAINST_MSAT_MIN = 1000
29-
const MSAT_MIN = 1000
26+
const MSAT_MIN = 20001 // 20001 is the minimum for a tip to be counted in trust
3027
const SIG_DIFF = 0.1 // need to differ by at least 10 percent
3128

3229
/*
@@ -88,16 +85,16 @@ function trustGivenGraph (graph) {
8885
const std = sqapply(mat, math.std) // math.squeeze(math.std(mat, 1))
8986
const mean = sqapply(mat, math.mean) // math.squeeze(math.mean(mat, 1))
9087
const zscore = math.map(mat, (val, idx) => {
91-
const zstd = math.subset(std, math.index(idx[0]))
92-
const zmean = math.subset(mean, math.index(idx[0]))
88+
const zstd = math.subset(std, math.index(idx[0], 0))
89+
const zmean = math.subset(mean, math.index(idx[0], 0))
9390
return zstd ? (val - zmean) / zstd : 0
9491
})
9592
console.timeLog('trust', 'minmax')
9693
const min = sqapply(zscore, math.min) // math.squeeze(math.min(zscore, 1))
9794
const max = sqapply(zscore, math.max) // math.squeeze(math.max(zscore, 1))
9895
const mPersonal = math.map(zscore, (val, idx) => {
99-
const zmin = math.subset(min, math.index(idx[0]))
100-
const zmax = math.subset(max, math.index(idx[0]))
96+
const zmin = math.subset(min, math.index(idx[0], 0))
97+
const zmax = math.subset(max, math.index(idx[0], 0))
10198
const zrange = zmax - zmin
10299
if (val > zmax) return MAX_TRUST
103100
return zrange ? (val - zmin) / zrange : 0
@@ -123,7 +120,8 @@ async function getGraph (models) {
123120
WITH user_votes AS (
124121
SELECT "ItemAct"."userId" AS user_id, users.name AS name, "ItemAct"."itemId" AS item_id, min("ItemAct".created_at) AS act_at,
125122
users.created_at AS user_at, "ItemAct".act = 'DONT_LIKE_THIS' AS against,
126-
count(*) OVER (partition by "ItemAct"."userId") AS user_vote_count
123+
count(*) OVER (partition by "ItemAct"."userId") AS user_vote_count,
124+
sum("ItemAct".msats) as user_msats
127125
FROM "ItemAct"
128126
JOIN "Item" ON "Item".id = "ItemAct"."itemId" AND "ItemAct".act IN ('FEE', 'TIP', 'DONT_LIKE_THIS')
129127
AND "Item"."parentId" IS NULL AND NOT "Item".bio AND "Item"."userId" <> "ItemAct"."userId"
@@ -136,9 +134,9 @@ async function getGraph (models) {
136134
),
137135
user_pair AS (
138136
SELECT a.user_id AS a_id, b.user_id AS b_id,
139-
count(*) FILTER(WHERE a.act_at > b.act_at AND a.against = b.against) AS before,
140-
count(*) FILTER(WHERE b.act_at > a.act_at AND a.against = b.against) AS after,
141-
count(*) FILTER(WHERE a.against <> b.against) * ${DISAGREE_MULT} AS disagree,
137+
sum(CASE WHEN b.user_msats > a.user_msats THEN a.user_msats / b.user_msats::FLOAT ELSE b.user_msats / a.user_msats::FLOAT END) FILTER(WHERE a.act_at > b.act_at AND a.against = b.against) AS before,
138+
sum(CASE WHEN b.user_msats > a.user_msats THEN a.user_msats / b.user_msats::FLOAT ELSE b.user_msats / a.user_msats::FLOAT END) FILTER(WHERE b.act_at > a.act_at AND a.against = b.against) AS after,
139+
sum(log(1 + a.user_msats / 10000::float) + log(1 + b.user_msats / 10000::float)) FILTER(WHERE a.against <> b.against) AS disagree,
142140
b.user_vote_count AS b_total, a.user_vote_count AS a_total
143141
FROM user_votes a
144142
JOIN user_votes b ON a.item_id = b.item_id
@@ -180,7 +178,7 @@ async function storeTrust (models, graph, vGlobal, mPersonal) {
180178
})
181179

182180
math.forEach(mPersonal, (val, [fromIdx, toIdx]) => {
183-
const globalVal = vGlobal.get([toIdx])
181+
const globalVal = vGlobal.get([toIdx, 0])
184182
if (isNaN(val) || val - globalVal <= SIG_DIFF) return
185183
if (personalValues) personalValues += ','
186184
personalValues += `(${graph[fromIdx].id}, ${graph[toIdx].id}, ${val}::FLOAT)`
@@ -199,6 +197,14 @@ async function storeTrust (models, graph, vGlobal, mPersonal) {
199197
SELECT id, oid, trust
200198
FROM (values ${personalValues}) g(id, oid, trust)
201199
ON CONFLICT ("fromId", "toId") DO UPDATE SET "zapTrust" = EXCLUDED."zapTrust"`
200+
),
201+
// select all arcs that don't exist in personalValues and delete them
202+
models.$executeRawUnsafe(
203+
`DELETE FROM "Arc"
204+
WHERE ("fromId", "toId") NOT IN (
205+
SELECT id, oid
206+
FROM (values ${personalValues}) g(id, oid, trust)
207+
)`
202208
)
203209
])
204210
}

0 commit comments

Comments
 (0)