Python中的法务会计Forensic accounting in Python
逆向工程数据分析时,常面临变量取值不确定但总和已知的情况。作者通过类似法务会计的案例,展示了如何利用Python推断出这些不确定变量的真实取值。该方法能够有效消除数据分析过程中的歧义,还原原始计算逻辑。掌握这种逆向推断技巧,有助于在数据审计和复盘中快速定位问题根源。
John
I recently had a project in which I had to reverse engineer a data analysis. There was some ambiguity regarding which of several possibilities someone chose for several of the variables, something analogous to the following example.
Suppose you have three numbers with uncertain values with a known, or at least purported, sum. The first number could be 31, 41, or 59; the second could be either 26 or 53; the last could be 58, 97, 93, or 23.
The following code enumerates all 3 × 2 × 4 = 24 possibilities and prints their sums.
from itertools import product
# Example input
possibilities = [(31, 41, 59), (26, 53), (58, 97, 93, 23)]
for combo in product(*possibilities):
total = sum(combo)
print(f"Combination {combo} sums to: {total}")In this example all the sums are unique, though of course that might not happen in practice. If, for example, you know the sum is 187, you know the three numbers were 41, 53, and 93. If the reported sum is 200, you know some assumption has been violated because none of the possible choses add up to 200.
More forensics posts
需要完整排版与评论请前往来源站点阅读。