Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # solution of Pruthwira Lenka for Python Assignment 2 > Problem 7
- # in 1st trimester of IIT Madras Web MTech (Industrial AI) 2025
- def simulate_sales(df: pd.DataFrame, n: int) -> list:
- """
- Simulate n future sales based on average price change.
- """
- np.random.seed(0)
- try:
- if 'OrderDate' in df.columns:
- last_price = df.sort_values(by='OrderDate')['Price'].iloc[-1]
- price_changes = df['Price'].diff()
- else:
- last_price = df['Price'].iloc[-1]
- except (IndexError, KeyError):
- last_price = 0.0
- if len(df) > 1:
- if 'OrderDate' in df.columns:
- price_changes = df.sort_values(by='OrderDate')['Price'].diff()
- else:
- price_changes = df['Price'].diff()
- mean_change = price_changes.mean()
- std_change = price_changes.std()
- if pd.isna(mean_change) or pd.isna(std_change) or std_change == 0:
- sim_mean = 0.0
- sim_std = 0.0
- else:
- sim_mean = mean_change
- sim_std = std_change
- else:
- sim_mean = 0.0
- sim_std = 5.0
- simulated_data = []
- current_price = last_price
- for _ in range(n):
- price_drift = np.random.normal(loc=sim_mean, scale=sim_std)
- current_price += price_drift
- quantity = np.random.randint(low=1, high=6)
- simulated_data.append((current_price, quantity))
- return simulated_data
Advertisement
Add Comment
Please, Sign In to add comment