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
# MP setup *must* happen at the very start of the process! | |
# Otherwise you'll get errors or it will be ignored. | |
import multiprocessing | |
multiprocessing.set_start_method('forkserver') | |
import os | |
os.environ["OMP_NUM_THREADS"] = "10" | |
import numpy as np | |
import pandas as pd | |
import pickle |
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
CREATE OR REPLACE FUNCTION pnorm(z double precision) RETURNS double precision AS $$ | |
SELECT CASE | |
WHEN $1 >= 0 THEN 1 - POWER(((((((0.000005383*$1+0.0000488906)*$1+0.0000380036)*$1+0.0032776263)*$1+0.0211410061)*$1+0.049867347)*$1+1),-16)/2 | |
ELSE 1 - pnorm(-$1) | |
END; | |
$$ LANGUAGE SQL IMMUTABLE STRICT; |
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
library(MASS) | |
library(ggplot2) | |
results <- NULL | |
# Generate simulated data | |
for ( seed in 1:30 ) { | |
set.seed(seed+42) | |
mu = c(0,-1+seed/10) | |
Sigma = matrix(c(1.5,-.5,-.5,.7),2,2) |
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
# Knuth's algorithm to partition ns into m sets. | |
# https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions | |
# http://cs.utsa.edu/~wagner/knuth/ | |
# http://cs.utsa.edu/~wagner/knuth/fasc3b.pdf | |
# ns must be a list of integers | |
# m must be an integer less than len(ns) | |
# this is a generator expression | |
def algorithm_u(ns, m): | |
def visit(n, a): |
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
# https://www.youtube.com/watch?v=d3mHfqd0VZY | |
def harmonic(x, y, n=6, epsilon=1e-3): | |
r = sqrt(x/y) | |
for a in range(1, n): | |
for b in range(1, n): | |
if abs(r - a/b) < epsilon: | |
return True | |
return False | |
for x in np.arange(1.6, 6.4, 0.2): |
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
X = np.hstack([np.ones(shape=(23, 1)), np.random.normal(size=(23, 2))]) | |
Theta = np.array([0.5, +0.1, -0.2]).reshape( (3, 1) ) | |
Y = X @ Theta + np.random.normal(0, 0.1, size=(23, 1)) | |
Theta_hat = np.linalg.inv(X.T @ X) @ X.T @ Y | |
Theta_hat |
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 typing import NamedTuple, Any, Optional | |
class Node(NamedTuple): | |
"""A single Node in a binary tree.""" | |
value: Any | |
left: Node | |
right: Node | |
def count(node: Optional[Node]) -> int: | |
"""Count the total number of nodes in a tree rooted at this node.""" |
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 os | |
import sys | |
from time import sleep | |
from threading import Thread | |
from queue import Queue | |
import codecs | |
def hard_work(x): | |
for n in range(100001): | |
x = codecs.encode(x, 'rot_13') |
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 scipy.optimize import linear_sum_assignment | |
import numpy as np | |
def maximize_trace(X): | |
""" | |
Maximize the trace of a square matrix using only row and | |
column permutations. Furthermore, sort the trace | |
in desending order so that largest value ends | |
up the upper left and the smallest in the lower right. | |