File size: 4,382 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import numpy as np


# Define demand curve function
def demand_curve(price, prices, predicted_sales):
    # This function interpolates the predicted sales for a given price
    interpolated_sales = np.interp(price, prices, predicted_sales)
    #print(f'Interpolated Sales for price {price}: {interpolated_sales}')

    return interpolated_sales


# Define profit function
def profit(gmv0, r_lambda, E_r_lambda, c):
    # This function calculates the profit as described in the article
    if r_lambda is None:
        raise ValueError("r_lambda cannot be None")
    
    calculated_profit = np.sum(gmv0 * E_r_lambda * (r_lambda - c))
    #print(f'Calculated Profit: {calculated_profit}')
    return calculated_profit


# Define optimal price formula
def calculate_optimal_price(c, lambda_value, s):
    # This function calculates the optimal price using the formula given in the article
    if (lambda_value + 1) * (s - 1) == 0:
        return None
    else:
        optimal_price = c * (lambda_value * s) / ((lambda_value + 1) * (s - 1))
        #print(f'Optimal Price: {optimal_price}')

        return optimal_price
    


# Pricing algorithm function
def pricing_algorithm(prices, predicted_sales, c, s, initial_lambda=5, delta_lambda=0.1, P0=0, max_iterations=1000, D_lambda=4):
    # Step 0: Select initial 位 = 5.
    lambda_value = initial_lambda
    iteration = 0

    #print(f'Initial Lambda Value: {lambda_value}')

    # Loop until profit constraint is satisfied or max iterations are reached
    while True:
        # Step 2: For current 位 calculate prices, and find out what Profit(位) is.
        # Calculate prices with lambda
        prices_with_lambda = calculate_optimal_price(c, lambda_value, s)

        # Use demand_curve function to calculate E_r_lambda
        E_r_lambda = demand_curve(prices_with_lambda, prices, predicted_sales)
        gmv0 = demand_curve(prices, prices, predicted_sales)

        # Calculate the Profit(位)
        current_profit = profit(gmv0, prices_with_lambda, E_r_lambda, c)

        # Step 3: If Profit(位) < P0, slightly increase 位. If Profit(位) > P0, slightly decrease 位.
        if current_profit < P0:
            lambda_value += delta_lambda
            #print(f'Current Profit < P0; Increasing Lambda to {lambda_value}')
        else:
            lambda_value -= delta_lambda
            #print(f'Current Profit >= P0; Decreasing Lambda to {lambda_value}')

        # Limit the change in 位 in a week to D_lambda percent.
        lambda_value = max(lambda_value, initial_lambda * (1 - D_lambda / 100))
        lambda_value = min(lambda_value, initial_lambda * (1 + D_lambda / 100))

        # Increment the iteration
        iteration += 1

        #print(f'Iteration: {iteration}, Lambda Value: {lambda_value}, Current Profit: {current_profit}')

        # Check the exit condition
        if current_profit >= P0 or iteration >= max_iterations:
            break

    # Result of pricing algorithm
    print(f'Final Optimal Price: {prices_with_lambda}, Final Lambda Value: {lambda_value}')
    return prices_with_lambda, lambda_value

def grid_search_lambda(P0, prices, predicted_sales, c, s, lambda_min=3, lambda_max=5, lambda_step=1, D_lambda=4):
    # Iterate over a range of lambda values
    lambda_values = range(lambda_min, lambda_max + 1, lambda_step)
    best_prices = []
    best_lambdas = []

    #print(f'Starting grid search with Lambda values: {list(lambda_values)}')

    # Loop through each lambda value
    for lambda_multiplier in lambda_values:

        print(f'\nProcessing Lambda Multiplier: {lambda_multiplier}')
        # Call pricing_algorithm function to calculate optimal price and lambda for the current lambda_multiplier
        optimal_price, optimal_lambda = pricing_algorithm(prices, predicted_sales, c, s, initial_lambda=lambda_multiplier, P0=P0, D_lambda=D_lambda)

        print(f'Optimal Price for Lambda Multiplier {lambda_multiplier}: {optimal_price}')
        print(f'Optimal Lambda Value for Lambda Multiplier {lambda_multiplier}: {optimal_lambda}')

        # Store the optimal price and lambda
        best_prices.append(optimal_price)
        best_lambdas.append(optimal_lambda)

    # Return the best prices and corresponding lambda values
    print(f'\nFinal Results - Best Prices: {best_prices}, Best Lambda Values: {best_lambdas}')
    return best_prices, best_lambdas