improve trust
This commit is contained in:
parent
48990d5987
commit
1b6a7e7f95
|
@ -12,6 +12,7 @@ function trust ({ boss, models }) {
|
||||||
// only explore a path up to this depth from start
|
// only explore a path up to this depth from start
|
||||||
const MAX_DEPTH = 6
|
const MAX_DEPTH = 6
|
||||||
const MAX_TRUST = 0.9
|
const MAX_TRUST = 0.9
|
||||||
|
const MIN_SUCCESS = 5
|
||||||
// https://en.wikipedia.org/wiki/Normal_distribution#Quantile_function
|
// https://en.wikipedia.org/wiki/Normal_distribution#Quantile_function
|
||||||
const Z_CONFIDENCE = 2.326347874041 // 98% confidence
|
const Z_CONFIDENCE = 2.326347874041 // 98% confidence
|
||||||
|
|
||||||
|
@ -162,39 +163,70 @@ function trustGivenGraph (graph, start) {
|
||||||
// return graph
|
// return graph
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// upvote confidence graph
|
// old upvote confidence graph
|
||||||
|
// async function getGraph (models) {
|
||||||
|
// const [{ graph }] = await models.$queryRaw`
|
||||||
|
// select json_object_agg(id, hops) as graph
|
||||||
|
// from (
|
||||||
|
// select id, json_agg(json_build_object('node', oid, 'trust', trust)) as hops
|
||||||
|
// from (
|
||||||
|
// select s.id, s.oid, confidence(s.shared, count(*), ${Z_CONFIDENCE}) as trust
|
||||||
|
// from (
|
||||||
|
// select a."userId" as id, b."userId" as oid, count(*) as shared
|
||||||
|
// from "ItemAct" b
|
||||||
|
// join users bu on bu.id = b."userId"
|
||||||
|
// join "ItemAct" a on b."itemId" = a."itemId"
|
||||||
|
// join users au on au.id = a."userId"
|
||||||
|
// join "Item" on "Item".id = b."itemId"
|
||||||
|
// where b.act = 'VOTE'
|
||||||
|
// and a.act = 'VOTE'
|
||||||
|
// and "Item"."parentId" is null
|
||||||
|
// and "Item"."userId" <> b."userId"
|
||||||
|
// and "Item"."userId" <> a."userId"
|
||||||
|
// and b."userId" <> a."userId"
|
||||||
|
// and "Item".created_at >= au.created_at and "Item".created_at >= bu.created_at
|
||||||
|
// group by b."userId", a."userId") s
|
||||||
|
// join users u on s.id = u.id
|
||||||
|
// join users ou on s.oid = ou.id
|
||||||
|
// join "ItemAct" on "ItemAct"."userId" = s.oid
|
||||||
|
// join "Item" on "Item".id = "ItemAct"."itemId"
|
||||||
|
// where "ItemAct".act = 'VOTE' and "Item"."parentId" is null
|
||||||
|
// and "Item"."userId" <> s.oid and "Item"."userId" <> s.id
|
||||||
|
// and "Item".created_at >= u.created_at and "Item".created_at >= ou.created_at
|
||||||
|
// group by s.id, s.oid, s.shared
|
||||||
|
// ) a
|
||||||
|
// group by id
|
||||||
|
// ) b`
|
||||||
|
// return graph
|
||||||
|
// }
|
||||||
|
|
||||||
async function getGraph (models) {
|
async function getGraph (models) {
|
||||||
const [{ graph }] = await models.$queryRaw`
|
const [{ graph }] = await models.$queryRaw`
|
||||||
select json_object_agg(id, hops) as graph
|
SELECT json_object_agg(id, hops) AS graph
|
||||||
from (
|
FROM (
|
||||||
select id, json_agg(json_build_object('node', oid, 'trust', trust)) as hops
|
SELECT id, json_agg(json_build_object('node', oid, 'trust', trust)) AS hops
|
||||||
from (
|
FROM (
|
||||||
select s.id, s.oid, confidence(s.shared, count(*), ${Z_CONFIDENCE}) as trust
|
WITH user_votes AS (
|
||||||
from (
|
SELECT "ItemAct"."userId" AS user_id, users.name AS name, "ItemAct"."itemId" AS item_id, "ItemAct".created_at AS act_at,
|
||||||
select a."userId" as id, b."userId" as oid, count(*) as shared
|
users.created_at AS user_at, "Item".created_at AS item_at, count(*) OVER (partition by "ItemAct"."userId") AS user_vote_count
|
||||||
from "ItemAct" b
|
FROM "ItemAct"
|
||||||
join users bu on bu.id = b."userId"
|
JOIN "Item" ON "Item".id = "ItemAct"."itemId" AND "ItemAct".act = 'VOTE' AND "Item"."parentId" IS NULL
|
||||||
join "ItemAct" a on b."itemId" = a."itemId"
|
JOIN users ON "ItemAct"."userId" = users.id
|
||||||
join users au on au.id = a."userId"
|
),
|
||||||
join "Item" on "Item".id = b."itemId"
|
user_pair AS (
|
||||||
where b.act = 'VOTE'
|
SELECT a.user_id AS a_id, a.name AS a_name, b.user_id AS b_id, b.name AS b_name,
|
||||||
and a.act = 'VOTE'
|
count(*) FILTER(WHERE a.act_at > b.act_at) AS before,
|
||||||
and "Item"."parentId" is null
|
count(*) FILTER(WHERE b.act_at > a.act_at) AS after,
|
||||||
and "Item"."userId" <> b."userId"
|
CASE WHEN b.user_at > a.user_at THEN b.user_vote_count ELSE a.user_vote_count END AS total
|
||||||
and "Item"."userId" <> a."userId"
|
FROM user_votes a
|
||||||
and b."userId" <> a."userId"
|
JOIN user_votes b ON a.item_id = b.item_id
|
||||||
and "Item".created_at >= au.created_at and "Item".created_at >= bu.created_at
|
GROUP BY a.user_id, a.name, a.user_at, a.user_vote_count, b.user_id, b.name, b.user_at, b.user_vote_count
|
||||||
group by b."userId", a."userId") s
|
)
|
||||||
join users u on s.id = u.id
|
SELECT a_id AS id, a_name, b_id AS oid, b_name, confidence(before, total - after, ${Z_CONFIDENCE}) AS trust, before, after, total
|
||||||
join users ou on s.oid = ou.id
|
FROM user_pair
|
||||||
join "ItemAct" on "ItemAct"."userId" = s.oid
|
WHERE before >= ${MIN_SUCCESS}
|
||||||
join "Item" on "Item".id = "ItemAct"."itemId"
|
|
||||||
where "ItemAct".act = 'VOTE' and "Item"."parentId" is null
|
|
||||||
and "Item"."userId" <> s.oid and "Item"."userId" <> s.id
|
|
||||||
and "Item".created_at >= u.created_at and "Item".created_at >= ou.created_at
|
|
||||||
group by s.id, s.oid, s.shared
|
|
||||||
) a
|
) a
|
||||||
group by id
|
GROUP BY a.id
|
||||||
) b`
|
) b`
|
||||||
return graph
|
return graph
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue