Skip to content

Instantly share code, notes, and snippets.

View galenseilis's full-sized avatar
📈
f(data | model, expertise) = insight

Galen Seilis galenseilis

📈
f(data | model, expertise) = insight
View GitHub Profile
@galenseilis
galenseilis / diffusion.py
Created October 7, 2022 15:34
Diffusion with Laplacian Operator (NumPy)
import numpy as np
import matplotlib.pyplot as plt
def laplacian(grid):
return np.roll(grid, 1, 0) +\
np.roll(grid, -1, 0) +\
np.roll(grid, 1, 1) +\
np.roll(grid, -1, 1) - 4 * grid
def evolve(grid, df, D=1):
'''
Easier method: regress y−2x against x and read the t-test results directly off the summary, making sure to halve any reported p-values to account for the one-sided alternative.
- whuber
'''
import numpy as np
import statsmodels.api as sm
x = np.array([1,2,3,3,4,5,5])
'''
Adding very small probabilities - How to compute?
'''
import mpmath as mp
mp.mp.dps = 130
print(mp.mpf(1.3573e-123) + mp.mpf(0.99))
from math import exp, log1p
l1 = -3006
l2 = -3012
def logsum(l1, l2):
return max(l1, l2) + log1p(exp(-abs(l1-l2)))
print(logsum(l1, l2))
f = lambda n: sum([int(i) for i in str(n)])
print(f(2**1000))
cache = {1: 1}
def collatz_count(n):
if n not in cache:
if n % 2 == 0:
cache[n] = 1 + collatz_count(n // 2)
else:
cache[n] = 1 + collatz_count(3 * n + 1)
return cache[n]
from itertools import permutations
names = ['Amelia', 'Boris', 'Carlos', 'Dara']
for p in permutations(names):
if p.index('Dara') >= p.index('Amelia'):
continue
elif p.index('Boris') == 2:
continue
elif abs(p.index('Amelia') - p.index('Carlos')) != 3:
import numpy as np
from scipy import stats
def wilson_cont(n1, n2, alpha=0.05):
'''
Wilson score interval with continuity correction.
Two-tail interval is assumed.
Parameters:
n1 (int): Count of outcome 1.
from more_itertools import set_partitions
S = set(range(1,6))
for i in S:
s_i = S - {i}
for left, right in set_partitions(s_i, 2):
if len(left) == len(right) and sum(left) + i == 9 and sum(right) + i == 9:
print(i, left, right)
break
import numpy as np
from scipy.stats import t
import statsmodels.api as sm
# Prepare data
x = np.array([1,2,3,3,4,5,5])
y = np.array([3,7,5,8,11,14,12])
n = y.size
X = sm.add_constant(x)