df = pd.read_excel('Data/Trips2020.xlsx')
df.head()
df.describe()
expected_trips = possion.predict(X, linear = False)
from scipy import stats
poisson_predict = possion.predict()
counts = np.arange(5)
predict_prob = stats.poisson.pmf(counts, np.asarray(poisson_predict)[:, None])
predict_prob
np.mean(predict_prob, axis=0)
sum(np.mean(predict_prob, axis=0))
quantiles = np.quantile(df["Income"], q=[0.0, 0.25, 0.5, 0.75, 1])
df["Income_Group"] = pd.cut(df["Income"], bins=quantiles, labels=[1, 2, 3, 4]).fillna(1)
sum(group_by["Average"] * group_by["Count"])
df_20p = df.copy()
df_20p["Income"] = df_20p["Income"]*1.2
sum(group_by_20p["Average"] * group_by_20p["Count"])
sum(group_by_20p["Average"] / sum(group_by["Average"] * group_by["Count"]) * group_by_20p["Count"])*100
# Set estimated
k1 = df_logit[df_logit["Parameter"] == "k1"]["Estimate"].iloc[0]
k2 = df_logit[df_logit["Parameter"] == "k2"]["Estimate"].iloc[0]
k3 = df_logit[df_logit["Parameter"] == "k3"]["Estimate"].iloc[0]
k4 = df_logit[df_logit["Parameter"] == "k4"]["Estimate"].iloc[0]
k5 = df_logit[df_logit["Parameter"] == "k5"]["Estimate"].iloc[0]
b1 = df_logit[df_logit["Parameter"] == "b1"]["Estimate"].iloc[0]
b2 = df_logit[df_logit["Parameter"] == "b2"]["Estimate"].iloc[0]
b3 = df_logit[df_logit["Parameter"] == "b3"]["Estimate"].iloc[0]
b4 = df_logit[df_logit["Parameter"] == "b4"]["Estimate"].iloc[0]
b5 = df_logit[df_logit["Parameter"] == "b5"]["Estimate"].iloc[0]
b6 = df_logit[df_logit["Parameter"] == "b6"]["Estimate"].iloc[0]
# Calculate V
df["V0"] = 0
df["V1"] = k1 + b4 * df["Age"]
df["V2"] = k2 + b1 * df["Income"] + b4 * df["Age"] + b6 * df["Male"]
df["V3"] = k3 + b1 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
df["V4"] = k4 + b1 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
df["V5"] = k5 + b2 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
# Calculate P
df["P0"] = np.exp(df["V0"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P1"] = np.exp(df["V1"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P2"] = np.exp(df["V2"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P3"] = np.exp(df["V3"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P4"] = np.exp(df["V4"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P5"] = np.exp(df["V5"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
# Calculate trips
df["Trips"] = 0 * df["P0"] + 1 * df["P1"] + 2 * df["P2"] + 3 * df["P3"] + 4 * df["P4"] + 5 * df["P5"]
# Expected number of trips
df["Trips"].sum()
df["Income"] = df["Income"] * 1.2
# Set estimated
k1 = df_logit[df_logit["Parameter"] == "k1"]["Estimate"].iloc[0]
k2 = df_logit[df_logit["Parameter"] == "k2"]["Estimate"].iloc[0]
k3 = df_logit[df_logit["Parameter"] == "k3"]["Estimate"].iloc[0]
k4 = df_logit[df_logit["Parameter"] == "k4"]["Estimate"].iloc[0]
k5 = df_logit[df_logit["Parameter"] == "k5"]["Estimate"].iloc[0]
b1 = df_logit[df_logit["Parameter"] == "b1"]["Estimate"].iloc[0]
b2 = df_logit[df_logit["Parameter"] == "b2"]["Estimate"].iloc[0]
b3 = df_logit[df_logit["Parameter"] == "b3"]["Estimate"].iloc[0]
b4 = df_logit[df_logit["Parameter"] == "b4"]["Estimate"].iloc[0]
b5 = df_logit[df_logit["Parameter"] == "b5"]["Estimate"].iloc[0]
b6 = df_logit[df_logit["Parameter"] == "b6"]["Estimate"].iloc[0]
# Calculate V
df["V0"] = 0
df["V1"] = k1 + b4 * df["Age"]
df["V2"] = k2 + b1 * df["Income"] + b4 * df["Age"] + b6 * df["Male"]
df["V3"] = k3 + b1 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
df["V4"] = k4 + b1 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
df["V5"] = k5 + b2 * df["Income"] + b3 * df["Cars"] + b5 * df["Age"] + b6 * df["Male"]
# Calculate P
df["P0"] = np.exp(df["V0"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P1"] = np.exp(df["V1"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P2"] = np.exp(df["V2"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P3"] = np.exp(df["V3"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P4"] = np.exp(df["V4"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
df["P5"] = np.exp(df["V5"])/(np.exp(df["V0"]) + np.exp(df["V1"]) + np.exp(df["V2"]) + np.exp(df["V3"]) + np.exp(df["V4"]) + np.exp(df["V5"]))
# Calculate trips
df["Trips"] = 0 * df["P0"] + 1 * df["P1"] + 2 * df["P2"] + 3 * df["P3"] + 4 * df["P4"] + 5 * df["P5"]
# Expected number of trips
df["Trips"].sum()