Skip to content

Instantly share code, notes, and snippets.

@gavinwhyte
Last active August 23, 2016 09:26
Show Gist options
  • Save gavinwhyte/dca12980955098b654143eb6f90ab6d4 to your computer and use it in GitHub Desktop.
Save gavinwhyte/dca12980955098b654143eb6f90ab6d4 to your computer and use it in GitHub Desktop.
TripCount
from scipy.stats import poisson
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1)
x = np.fromfile('tripcountssample.txt',
sep='\n', dtype=np.int64)
mu = np.mean(x)
mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
print ("Mean, Variance, Skew, Kurtosis",mean, var , skew , kurt )
def plot_cdf(data, plot_range=None, scale_to=None, **kwargs):
num_bins= len(data)
sorted_data = np.array(sorted(data), dtype=np.float64)
data_range = sorted_data[-1] - sorted_data[0]
counts, bin_edges = np.histogram(sorted_data, bins=num_bins)
xvalues = bin_edges[1:]
yvalues = np.cumsum(counts)
if plot_range is None:
xmin = xvalues[0]
xmax = xvalues[-1]
else:
xmin, xmax = plot_range
# pad the arrays
xvalues = np.concatenate([[xmin, xvalues[0]], xvalues, [xmax]])
yvalues = np.concatenate([[0.0, 0.0], yvalues, [yvalues.max()]])
if scale_to:
yvalues = yvalues / len(data) * scale_to
plt.axis([xmin, xmax, 0, yvalues.max()])
return plt.step(xvalues, yvalues, **kwargs)
# Plot the graph and look at results.
plot_cdf(x, plot_range=[30, 60],
scale_to=100, lw=2)
plt.grid(lw=1)
plt.xlabel('Counts (x.1mm)', fontsize=18)
plt.ylabel('Percent', fontsize=18)
plt.show()
# How do we predict using cumulative distribution taking counts 36
predict = poisson.cdf(36,mu)
print ("The precentage prediction for trip count 36 ", predict * 100)
predict = poisson.cdf(44,mu)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment