Skip to content

Instantly share code, notes, and snippets.

View wiredtoserve's full-sized avatar

mehul wiredtoserve

  • Melbourne
View GitHub Profile
@wiredtoserve
wiredtoserve / constraint_roster.py
Created October 25, 2020 07:54
constraints of the rostering problem
# 17 roles per meeting
for d in days:
model += pulp.lpSum(x[m][d][r] for m in members for r in roles) == 17
# every member has at least done a role once
for m in members:
for r in roles:
model += pulp.lpSum(x[m][d][r] for d in days) >= 1
# only one role per member per meeting
@wiredtoserve
wiredtoserve / rostering_objective.py
Created October 25, 2020 07:38
objective function of the rostering problem
# objective function
model = pulp.LpProblem("Toastmasters", pulp.LpMaximize)
model += pulp.lpSum(x[si][dj][ri] for si in members for dj in days for ri in roles)
@wiredtoserve
wiredtoserve / initialising_variables.py
Created October 25, 2020 07:22
setting up the decision variables
import pulp
from pulp import *
members = range(1,27)
days = range(1,28)
roles = range(1,18)
# defining the variables
x = pulp.LpVariable.dicts("x",(members,days,roles),0,1,pulp.LpInteger)
@wiredtoserve
wiredtoserve / volatility_stocks.py
Created April 11, 2020 03:48
calculate the risk in each stock
import numpy as np
import pandas as pd
# calculating volatility
risk_df = pd.DataFrame(returns.std(), columns = ['returns'])
# output
risk_df.head()
@wiredtoserve
wiredtoserve / stocks_discount.py
Created April 10, 2020 17:06
calculate the discounted stocks
# dictionary to store all the values
discount_dict = {}
def calculate_discount(df):
max_price = df['Adj Close'].describe()['75%']
min_price = df['Adj Close'].min()
return (max_price - min_price)/max_price
for tick in stock_dict.keys():
discount_dict[tick] = calculate_discount(stock_dict[tick])
@wiredtoserve
wiredtoserve / calculating_returns.py
Created April 10, 2020 13:12
returns for stocks
import pandas as pd
from collections import defaultdict
# creating a returns data frame
returns = pd.DataFrame()
# computes the percentage change from the immediately previous row by default
for key in stock_dict.keys():
returns[key + ' Return'] = stock_dict[key]['Adj Close'].pct_change()
@wiredtoserve
wiredtoserve / yahoo_finance.py
Created April 10, 2020 11:17
reading in the stocks data
import pandas_datareader as pdr
from datetime import datetime
# get a list of ticker names
tickers = [tick for tick in stocks_df['Symbol']]
# initialize start and end times
start = datetime(2019, 12, 1)
end = datetime.today()
import pandas as pd
# get a list of stocks
ticker_df = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
stocks_df = ticker_df[0]
# dataframe output
stocks_df.head()
@wiredtoserve
wiredtoserve / monte_carlo_continous_review.py
Created March 22, 2020 10:24
Continuous Review Monte Carlo
def cc_monte_carlo_ray(product, q, r):
inventory = product.starting_stock
mean = product.mean
sd = product.sd
lead_time = product.lead_time
probability = product.probability
order_placed = False
order_time = 0
stock_out = 0
@wiredtoserve
wiredtoserve / perodic_review_profit.py
Created March 22, 2020 09:27
calculate profit periodic review
def calculate_profit(data, product):
unit_cost = product.unit_cost
selling_price = product.selling_price
holding_cost = product.holding_cost
order_cost = product.ordering_cost
size = product.size
days = 365
revenue = sum(data['units_sold']) * selling_price
Co = len(data['orders']) * order_cost