This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = lambda n: sum([int(i) for i in str(n)]) | |
print(f(2**1000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer