Skip to content

Instantly share code, notes, and snippets.

@gbushnell
gbushnell / invoice_pull.sql
Created October 15, 2020 14:38
Query Invoices for RFM Analysis
SELECT IV.client__description as "client_description",
IV.invoice_date,
IV.amount
FROM Invoice_View IV
# Calculate Confidence Intervals for probability matrix
def calc_confidence_interval(alpha):
alpha_stat = stats.norm.ppf(1 - (1 - alpha) / 2)
for d_i in range(expected_prob_matrix.shape[0]):
for n_i in range(expected_prob_matrix.shape[1]):
prop_exp = expected_prob_matrix[d_i, n_i]
half_length = alpha_stat * (prop_exp * (1 - prop_exp) / data_cols_length[n_i]) ** .5 + (
1 / (2 * data_cols_length[n_i]))
u_bound = prop_exp + half_length
@gbushnell
gbushnell / benfords_law_expected.py
Last active October 1, 2020 17:56
Expected Frequency of digit occurrences in leading 2 digits per Benford's Law
# Probability of digit (d) occurring in nth position
def calc_expected_probability(d: int, n: int) -> float:
# generalization of Benford's Law ~ Summation( log[ 1 + (1/(10k+d)] ) where d = digit, k = (10^(n-2), 10^(n-1))
# source: https://en.wikipedia.org/wiki/Benford%27s_law
prob = 0
if (d == 0) and (n == 1):
prob = 0
elif n == 1:
prob = math.log10(1 + (1 / d))
else: