@@ -18,15 +18,12 @@ export async function trust ({ boss, models }) {
18
18
const MAX_DEPTH = 10
19
19
const MAX_TRUST = 1
20
20
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
24
21
// https://en.wikipedia.org/wiki/Normal_distribution#Quantile_function
25
22
const Z_CONFIDENCE = 6.109410204869 // 99.9999999% confidence
26
23
const GLOBAL_ROOT = 616
27
- const SEED_WEIGHT = 0.25
24
+ const SEED_WEIGHT = 1.0
28
25
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
30
27
const SIG_DIFF = 0.1 // need to differ by at least 10 percent
31
28
32
29
/*
@@ -88,16 +85,16 @@ function trustGivenGraph (graph) {
88
85
const std = sqapply ( mat , math . std ) // math.squeeze(math.std(mat, 1))
89
86
const mean = sqapply ( mat , math . mean ) // math.squeeze(math.mean(mat, 1))
90
87
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 ) )
93
90
return zstd ? ( val - zmean ) / zstd : 0
94
91
} )
95
92
console . timeLog ( 'trust' , 'minmax' )
96
93
const min = sqapply ( zscore , math . min ) // math.squeeze(math.min(zscore, 1))
97
94
const max = sqapply ( zscore , math . max ) // math.squeeze(math.max(zscore, 1))
98
95
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 ) )
101
98
const zrange = zmax - zmin
102
99
if ( val > zmax ) return MAX_TRUST
103
100
return zrange ? ( val - zmin ) / zrange : 0
@@ -123,7 +120,8 @@ async function getGraph (models) {
123
120
WITH user_votes AS (
124
121
SELECT "ItemAct"."userId" AS user_id, users.name AS name, "ItemAct"."itemId" AS item_id, min("ItemAct".created_at) AS act_at,
125
122
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
127
125
FROM "ItemAct"
128
126
JOIN "Item" ON "Item".id = "ItemAct"."itemId" AND "ItemAct".act IN ('FEE', 'TIP', 'DONT_LIKE_THIS')
129
127
AND "Item"."parentId" IS NULL AND NOT "Item".bio AND "Item"."userId" <> "ItemAct"."userId"
@@ -136,9 +134,9 @@ async function getGraph (models) {
136
134
),
137
135
user_pair AS (
138
136
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,
142
140
b.user_vote_count AS b_total, a.user_vote_count AS a_total
143
141
FROM user_votes a
144
142
JOIN user_votes b ON a.item_id = b.item_id
@@ -180,7 +178,7 @@ async function storeTrust (models, graph, vGlobal, mPersonal) {
180
178
} )
181
179
182
180
math . forEach ( mPersonal , ( val , [ fromIdx , toIdx ] ) => {
183
- const globalVal = vGlobal . get ( [ toIdx ] )
181
+ const globalVal = vGlobal . get ( [ toIdx , 0 ] )
184
182
if ( isNaN ( val ) || val - globalVal <= SIG_DIFF ) return
185
183
if ( personalValues ) personalValues += ','
186
184
personalValues += `(${ graph [ fromIdx ] . id } , ${ graph [ toIdx ] . id } , ${ val } ::FLOAT)`
@@ -199,6 +197,14 @@ async function storeTrust (models, graph, vGlobal, mPersonal) {
199
197
SELECT id, oid, trust
200
198
FROM (values ${ personalValues } ) g(id, oid, trust)
201
199
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
+ )`
202
208
)
203
209
] )
204
210
}
0 commit comments