Skip to content

Instantly share code, notes, and snippets.

@270ajay
Last active August 14, 2018 06:29
Show Gist options
  • Save 270ajay/b53dc83c1634c7951dae085a9a7265e9 to your computer and use it in GitHub Desktop.
Save 270ajay/b53dc83c1634c7951dae085a9a7265e9 to your computer and use it in GitHub Desktop.
Uses Monte Carlo Simulation to maximize profit. -- Newspaper demand per day is normally distributed. Can be done easily on excel.
import numpy as np
import math
'''Using MonteCarlo Simulation to maximize profit.
A Newspaper boy can buy 100, 120, 140, or 160 newspapers/day/month.
He will only be able to buy same number of newspapers for the whole month.
For example, if he buys 100 newspapers/day/month, then he will not be able to
buy 120, 140, or 160 newspapers in any day in that month.
The demand for newspaper in a day is normally distributed with mean of 135.70
and standard deviation of 27.10.
Newspaper boy can buy at a Whole sale price of $0.55/paper.
He can sell at a retail price of $1/paper.
He can sell the leftover newspapers at a scrap price of $0.03/paper.
In this problem, we want to find out whether the newspaper buy should buy
100, 120, 140, or 160 newspapers/day/month in order to maximize his average profit
for the month (30 days).
T-Distribution for 95% confidence intervals.
alpha=0.025, dof=29 -> t=2.045
'''
np.random.seed(0)
Days = 30
NewspaperBuyOptions = [100, 120, 140, 160]
WholeSalePrice = 0.55
RetailPrice = 1
ScrapPrice = 0.03
MeanDemand = 135.70
SDDemand = 27.10
AverageProfitForNewspaperBuy = [0, 0, 0, 0]
ProfitValues = [[], [], [], []]
StandardDeviation = []
ConfidenceIntervalsNewspaperBuy = [[], [], [], []]
# checks and gives error if this is not satisfied
assert len(NewspaperBuyOptions) == len(AverageProfitForNewspaperBuy) == len(ConfidenceIntervalsNewspaperBuy)
for day in range(Days):
Demand = round(np.random.normal(MeanDemand, SDDemand))
for i in range(len(NewspaperBuyOptions)):
profit = (min(Demand, NewspaperBuyOptions[i]) * RetailPrice) + (max(NewspaperBuyOptions[i] - Demand, 0) * ScrapPrice) - \
(NewspaperBuyOptions[i] * WholeSalePrice)
AverageProfitForNewspaperBuy[i] += profit
ProfitValues[i].append(profit)
for i in range(len(NewspaperBuyOptions)):
AverageProfitForNewspaperBuy[i] /= Days
for i in range(len(ProfitValues)):
StandardDeviation.append(np.std(ProfitValues[i], ddof=1))
for i in range(len(ConfidenceIntervalsNewspaperBuy)):
ConfidenceIntervalsNewspaperBuy[i].append(AverageProfitForNewspaperBuy[i] -
(2.045 * StandardDeviation[i])/math.sqrt(Days))
ConfidenceIntervalsNewspaperBuy[i].append(
AverageProfitForNewspaperBuy[i] + (2.045 * StandardDeviation[i]) / math.sqrt(Days))
for i in range(len(NewspaperBuyOptions)):
print("\nIf {} newspaper/day/month is bought: \nAverage profit is {} "
"\n95% Confidence Interval on Average Profit: {} ".format(NewspaperBuyOptions[i],
AverageProfitForNewspaperBuy[i],
ConfidenceIntervalsNewspaperBuy[i]))
print("\n140 newspapers/day/month seems to be the best choice to get maximum profit.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment