Skip to content

Instantly share code, notes, and snippets.

View HYChou0515's full-sized avatar
🏝️
Relax

HYChou HYChou0515

🏝️
Relax
  • tsmc
  • Taiwan
View GitHub Profile
from artest import autoreg, automock
@autoreg("adder")
def adder(a, b):
return a + b
# @autoreg("summation")
# def summation(args):
# # a complex way to implement summation
# if len(args) == 1:
@HYChou0515
HYChou0515 / bayes_flow.ipynb
Last active February 8, 2023 15:12
Use optimization and bayes rule to calculate probability for each step of flows.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@HYChou0515
HYChou0515 / solve.py
Created February 5, 2023 04:46
Orthogonal projection of a point onto a line
def vec_scale(v: tuple[float, float], a: float):
return (a * v[0], a * v[1])
def vec_mul_add(v1: tuple[float, float], v2: tuple[float, float], a: float = 1):
"""calc v1+a*v2"""
return (v1[0] + a * v2[0], v1[1] + a * v2[1])
def inner_prod(v1: tuple[float, float], v2: tuple[float, float]):
import itertools
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
def get_random_dist(seed=None):
rng = np.random.default_rng(seed)
nsamples = int(np.exp(rng.random()*2+3))
dists = [
@HYChou0515
HYChou0515 / poisson_mixture.ipynb
Last active February 19, 2022 00:38
poisson_mixture with em algorithm
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Tasks involving search in a sorted list are pretty common. Sometimes using list literal would be very convenient and fast enough for small list, e.g., [v for v >= x for v in a].

However, when the list is too large, this kind of linear search can cost much time than it is actually needed. Using binary search is preffered in this situation.

Python has a built in library bisect for this kind of job. Sometimes it could be quite confusing how to use, though.

Here I tried to summary the basic usage and some useful application of bisect.

@HYChou0515
HYChou0515 / install-python.sh
Last active April 5, 2021 08:16
Install python from source (debian)
# Reference: https://linuxize.com/post/how-to-install-python-3-9-on-debian-10/
PYVER=3.9.1
# Install the dependencies necessary to build Python:
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev liblzma-dev
wget https://www.python.org/ftp/python/${PYVER}/Python-{PYVER}.tgz
tar -xf Python-${PYVER}.tgz
[alias]
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"
@HYChou0515
HYChou0515 / linesearch.py
Last active February 13, 2021 02:12
Line search, searching for zero point of a increasing function.
# Given func() and cond(), assume there is a number A such that
# cond(func(x)) == 0 if x < A
# == 1 if x >= A
# line search will find the number A according to func() and cond().
# Starting from point=x0 with step=s0, the searching has two phases:
# 1) expanding and 2) concentrating.
#
# 1) Expanding
# In the expanding phase, if cond(func(x0)) == 0,
# then we take larger and larger steps toward positive,
@HYChou0515
HYChou0515 / loan.py
Last active July 18, 2020 03:50
Calculate loan
def loan2(R, M=[55000], L=8500000, F=3000000, P=12, verbose=0):
# R is a list of rate of each year
# M is a list of month pay-back of each year
# P is period per year
R = [1.0 * r for r in R]
M = [1.0 * m for m in M]
L = L * 1.0
F = F * 1.0
P = P * 1.0
principal = L-F