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 / is_contiguous_dates.py
Created November 16, 2023 01:27
is_contiguous_dates
def is_contiguous_dates(dates):
start_date = dates.min()
end_date = dates.max()
date_range = pd.date_range(start=start_date, end=end_date)
diff = date_range.difference(dates)
if diff.size:
return False
else:
return True
@galenseilis
galenseilis / chatgpt_missing_dates_within_group.py
Created November 15, 2023 15:26
chatgpt_missing_dates_within_group.py
import pandas as pd
def fill_missing_dates(df, date_column='ds', value_column='y', group_column=None):
"""
Fill missing dates between the min and max date within each group
and assign the value of 0 for the specified column.
Parameters:
- df (pd.DataFrame): Input DataFrame.
- date_column (str): Name of the datetime column.
@galenseilis
galenseilis / 608110.py
Last active March 2, 2023 05:47
re 608110
import matplotlib.pyplot as plt
import numpy as np
m = 300
u1 = np.random.uniform(0.5, 4.5, size=m)
u2 = 2 / u1 + np.random.normal(scale=0.09, size=m)
plt.scatter(u1, u2)
@galenseilis
galenseilis / rethinking_0.4.py
Last active December 21, 2022 05:05
Statistical rethinking with Python
import matplotlib.pyplot as plt
import statsmodels.api as sm
import statsmodels.formula.api as smf
cars = sm.datasets.get_rdataset('cars')
cars = cars.raw_data
model = smf.ols('dist ~ speed', data=cars)
results = model.fit()
@galenseilis
galenseilis / rethinking_0.3.py
Created December 21, 2022 04:40
Statistical rethinking with Python
# Tensorflow
import tensorflow as tf
print(tf.math.log(0.01 ** 200))
print(200 * tf.math.log(0.01))
# PyMC
import numpy as np
print(np.log(0.01 ** 200))
print(200 * np.log(0.01))
@galenseilis
galenseilis / rethinking_0.2.py
Created December 20, 2022 19:39
Statistical rethinking with Python
# Tensorflow
import tensorflow as tf
x = tf.range(1, 3, dtype='float32')
x *= 10
x = tf.math.log(x)
x = tf.reduce_sum(x)
x = tf.math.exp(x)
# PyMC
@galenseilis
galenseilis / python_alias_bash.md
Created December 20, 2022 18:27
Create Python alias in BASH

On some systems I find that python is not a defined command, but python3 is. Since Python 2 is no longer supported, and will hopefully become scarcer in dependencies, we can assume that python represents the latest installed Python.

Assuming python3 is defined, we can define our alias in a permanent way by:

  1. Open config: vim ~/.bashrc
  2. Add the line alias python='python3'
  3. Save and exit (:wq)
  4. Run . ~/.bashrc from home.
@galenseilis
galenseilis / rethinking_0.1.py
Created December 20, 2022 18:21
Statistical rethinking with Python
print("All models are wrong, but some are useful.")
@galenseilis
galenseilis / short_example_conditional_normal.py
Created December 19, 2022 01:16
Short example of a conditional normal distribution.
import matplotlib.pyplot as plt
import numpy as np
L = np.random.multinomial(1, pvals=[1/3]*3, size=1000)
Q = L @ np.diag([1, 7, 31])
M = np.sum(Q, axis=1)
X = np.random.normal(loc=M, size=M.size)
@galenseilis
galenseilis / zerocated.py
Created December 7, 2022 15:06
Early attempt at zerocated numbers.
from itertools import product
from sympy.abc import a,b,c
def phi(x,y):
if y != 0 and y not in (a,b,c):
return x / y
elif y in (a,b,c):
return x * y
elif x == 0 and y == 0: