Spaces:
Sleeping
Sleeping
File size: 2,691 Bytes
a55ae21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import numpy as np
# Define demand curve function
def demand_curve(price, prices, predicted_sales):
return np.interp(price, prices, predicted_sales)
# Define profit function
def profit(gmv0, r_lambda, E_r_lambda, c, P0):
return np.sum(gmv0 * E_r_lambda * (r_lambda - c)) - P0
# Run the pricing algorithm to find the optimal value of 位
def pricing_algorithm(P0, demand_curve, prices, predicted_sales, c, initial_lambda=5, delta_lambda=4):
# Step 0: Select an initial value of 位 = 5. Select fixed global stability parameter D_\lambda = 4.
lambda_value = initial_lambda
while True:
# Calculate prices with lambda
prices_with_lambda = prices * lambda_value
# Step 2: For current 位 calculate prices, and find out what a
gmv0 = demand_curve(P0, prices, predicted_sales)
# Use demand_curve function to calculate E_r_lambda
E_r_lambda = demand_curve(prices_with_lambda, prices, predicted_sales)
r_lambda = E_r_lambda + (gmv0 - E_r_lambda) / (1 + np.exp((prices - prices_with_lambda) / c))
current_profit = profit(gmv0, r_lambda, E_r_lambda, c, P0)
# Step 3: If \mathrm{Profit}(\lambda)< P_0, slightly increase 位.
# If \mathrm{Profit}(\lambda)> P_0, slightly decrease 位.
# Change in 位 in a week should be no more than D_\lambdapercent.
if current_profit < 0:
lambda_value += delta_lambda
elif current_profit > 0:
lambda_value -= delta_lambda
else:
break
# Step 1: Select/update value of P0 according to business needs and strategy.
# Go to Step 2.
# Return the optimal value of 位
return lambda_value
# Define grid search lambda function
def grid_search_lambda(P0, demand_curve, prices, predicted_sales, c, lambda_min=3, lambda_max=10, lambda_step=1):
# Step 0: Select initial 位 = 5. Select fixed global stability parameter D_\lambda = 4.
lambda_values = range(lambda_min, lambda_max + 1, lambda_step)
best_prices = []
for lambda_multiplier in lambda_values:
# Step 2: For current 位 calculate prices, and find out what a \mathrm{Profit}(\lambda)is.
lambda_value = pricing_algorithm(P0, demand_curve, prices, predicted_sales, c, lambda_multiplier)
prices_with_lambda = prices * lambda_value
profits = profit(demand_curve(P0, prices_with_lambda, predicted_sales), 1, 1, c, P0)
best_price = prices_with_lambda[np.argmax(profits)]
best_prices.append(best_price)
# Step 1: Select/update value of P0 according to business needs and strategy.
# Go to Step 2.
# Return the best prices
return best_prices
|