Midday置信度评分组件:嵌入、金额、货币和日期评分的详细算法
Midday置信度评分组件:嵌入、金额、货币和日期评分的详细算法
【免费下载链接】midday 项目地址: https://gitcode.com/GitHub_Trending/mi/midday
概述
Midday的置信度评分系统是一个智能匹配引擎,用于自动关联银行交易与对应的发票或收据。该系统通过多维度评分算法,结合语义嵌入、金融数据匹配和时间序列分析,实现高精度的自动匹配功能。
核心评分维度
1. 嵌入评分(Embedding Score)
嵌入评分基于语义相似性计算,使用PostgreSQL的向量相似性搜索功能:
// PostgreSQL余弦距离转换为相似度评分const embeddingScore = Math.max(0, 1 - candidate.embeddingScore);// 嵌入相似度阈值配置export const EMBEDDING_THRESHOLDS = { PERFECT_MATCH: 0.15, // 非常相似的嵌入 STRONG_MATCH: 0.35, // 强语义相似性 GOOD_MATCH: 0.45, // 中等相似性 WEAK_MATCH: 0.6, // 弱但可能的匹配} as const;
嵌入评分转换表
2. 金额评分(Amount Score)
金额评分采用分层容差算法,针对不同金额规模和应用场景进行优化:
function calculateAmountDifferenceScore( amount1: number, amount2: number, matchType: string): number { const diff = Math.abs(amount1 - amount2); const maxAmount = Math.max(Math.abs(amount1), Math.abs(amount2)); const percentageDiff = diff / maxAmount; // 分层容差评分 if (percentageDiff === 0) return 1.0; if (percentageDiff <= 0.01) return 0.98; // 1%容差 if (percentageDiff <= 0.02) return 0.95; // 2%容差 if (percentageDiff <= 0.025) return 0.92; // 2.5%容差 if (percentageDiff <= 0.03) return 0.9; // 3%容差 if (percentageDiff <= 0.05) return 0.85; // 5%容差 if (percentageDiff <= 0.1) return 0.6; // 10%容差 if (percentageDiff <= 0.2) return 0.3; // 20%容差 return 0;}
跨货币匹配算法
3. 货币评分(Currency Score)
货币评分采用保守策略,优先确保货币一致性:
export function calculateCurrencyScore( currency1?: string, currency2?: string,): number { if (!currency1 || !currency2) return 0.5; // 中性评分 // 最高优先级:完全相同的货币 if (currency1 === currency2) return 1.0; // 较低优先级:不同货币 - 更加保守 return 0.3; // 从0.5降低到0.3以更加保守}
货币匹配策略
4. 日期评分(Date Score)
日期评分根据文档类型(发票或支出)采用不同的时间逻辑:
export function calculateDateScore( inboxDate: string, transactionDate: string, inboxType?: string | null,): number { const diffDays = Math.abs( (new Date(inboxDate).getTime() - new Date(transactionDate).getTime()) / (1000 * 60 * 60 * 24) ); const type = inboxType || \"expense\"; if (type === \"invoice\") { // 发票逻辑:付款通常在发票日期之后 if (diffDays <= 6) return 0.99; // 0-3天 + 3天银行延迟 if (diffDays <= 11) return 0.93; // Net 7 (6-8天 + 3天延迟) if (diffDays <= 20) return 0.95; // Net 15 (13-17天 + 3天延迟) if (diffDays <= 38) return 0.98; // Net 30 (27-35天 + 3天延迟) if (diffDays <= 68) return 0.96; // Net 60 (58-65天 + 3天延迟) if (diffDays <= 98) return 0.94; // Net 90 (88-95天 + 3天延迟) } else { // 支出逻辑:收据通常在交易之后 if (diffDays <= 4) return 0.99; // 当天或次日(考虑延迟) if (diffDays <= 10) return 0.95; // 一周内(考虑延迟) if (diffDays <= 33) return 0.9; // 一个月内(考虑延迟) if (diffDays <= 63) return 0.8; // 两个月内(考虑延迟) if (diffDays <= 93) return 0.7; // 非常晚的收据(考虑延迟) } // 标准接近度评分 if (diffDays === 0) return 1.0; if (diffDays <= 1) return 0.95; if (diffDays <= 3) return 0.85; if (diffDays <= 7) return 0.75; if (diffDays <= 14) return 0.6; if (diffDays <= 30) return Math.max(0.3, 1 - (diffDays / 30) * 0.7); return 0.1; // 非常旧 = 最小评分但不为零}
综合置信度计算
权重分配
const teamWeights = { embeddingWeight: 0.5, // 增加:需要更强的语义相似性以防止错误匹配 amountWeight: 0.35, // 保持财务准确性高 - 对正确性至关重要 currencyWeight: 0.1, // 减少:当大多数交易使用相同货币时,货币匹配意义较小 dateWeight: 0.05, // 支持时间对齐的信号};
置信度计算公式
// 综合置信度计算let confidenceScore = (embeddingScore * teamWeights.embeddingWeight) + (amountScore * teamWeights.amountWeight) + (currencyScore * teamWeights.currencyWeight) + (dateScore * teamWeights.dateWeight);// 增强的置信度提升 - 财务准确性优先,然后是语义if (amountScore >= 0.95 && embeddingScore >= 0.85 && dateScore >= 0.8) { confidenceScore = Math.max(confidenceScore, 0.96);}// 确保置信度评分始终在有效范围内confidenceScore = Math.max(0.0, Math.min(1.0, confidenceScore));
匹配阈值系统
自动校准机制
// 基于用户反馈的阈值校准export const CALIBRATION_LIMITS = { MAX_ADJUSTMENT: 0.03, // 每次校准最大3%阈值调整 MIN_SAMPLES_AUTO: 5, // 自动匹配校准的最小样本数 MIN_SAMPLES_SUGGESTED: 3, // 建议匹配校准的最小样本数 MIN_SAMPLES_CONSERVATIVE: 8, // 保守调整的更高阈值} as const;
匹配类型阈值
实际应用场景
场景1:完美匹配(发票-付款)
场景2:跨货币匹配
// 跨货币匹配算法function isCrossCurrencyMatch( item1: CrossCurrencyComparableItem, item2: CrossCurrencyComparableItem, tolerancePercent = 0.02, minTolerance = 15,): boolean { // 必须有不同的货币 if (!item1.currency || !item2.currency || item1.currency === item2.currency) { return false; } // 必须有相同的基础货币 if (!item1.baseCurrency || !item2.baseCurrency || item1.baseCurrency !== item2.baseCurrency) { return false; } // 必须有基础金额 if (!item1.baseAmount || !item2.baseAmount) { return false; } const baseAmount1 = Math.abs(item1.baseAmount); const baseAmount2 = Math.abs(item2.baseAmount); const difference = Math.abs(baseAmount1 - baseAmount2); const avgAmount = (baseAmount1 + baseAmount2) / 2; // 基于金额大小的分层容差 let adjustedTolerance: number; if (avgAmount < 100) { // 小金额:更保守(四舍五入误差、费用、小额交易) adjustedTolerance = Math.max(10, avgAmount * 0.04); } else if (avgAmount < 1000) { // 中等金额:更保守的容差 adjustedTolerance = Math.max(15, avgAmount * 0.02); } else { // 大金额:非常严格(汇率应该稳定) adjustedTolerance = Math.max(25, avgAmount * 0.015); } return difference < adjustedTolerance;}
性能优化策略
1. 分层查询优化
系统采用四级查询策略,从精确匹配到语义匹配逐步放宽条件:
- 第一级:完美金融匹配(精确金额+货币)
- 第二级:完美基础货币匹配(跨货币但基础货币相同)
- 第三级:强语义匹配(高嵌入相似性)
- 第四级:良好语义匹配(中等嵌入相似性)
2. 实时校准系统
// 基于用户反馈的实时校准async function getTeamCalibration(db: Database, teamId: string) { // 获取过去90天的性能数据 const performanceData = await db.select(...); // 计算准确率指标 const autoMatchAccuracy = autoMatches.filter(d => d.status === \"confirmed\").length / autoMatches.length; const suggestedMatchAccuracy = suggestedMatches.filter(d => d.status === \"confirmed\").length / suggestedMatches.length; // 根据性能调整阈值 if (autoMatchAccuracy > 0.95 && autoMatches.length >= 5) { calibratedAutoThreshold = Math.max(0.92, 0.95 - 0.02); } return calibratedThresholds;}
总结
Midday的置信度评分系统通过多维度算法和智能校准机制,实现了高精度的金融文档自动匹配。系统特点包括:
- 多维度评分:嵌入、金额、货币、日期四个维度的综合评估
- 智能容差:基于金额大小的分层容差算法
- 实时校准:根据用户反馈动态调整匹配阈值
- 跨货币支持:完善的多货币处理能力
- 性能优化:分层查询策略确保高效运行
这套系统不仅提高了匹配准确性,还通过机器学习机制不断优化性能,为用户提供可靠的自动化财务处理体验。
【免费下载链接】midday 项目地址: https://gitcode.com/GitHub_Trending/mi/midday
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考