Airbnbsp
Airbnbsp
May 5, 2024
Data Preprocessing
[6]: import pandas as pd
df = pd.read_csv("C:/Users/KIIT/Downloads/abnb_stock_data.csv")
df
Volume
0 70447500
1 26980800
2 16966100
3 10914400
4 20409600
.. …
804 4851100
805 5277900
806 4623700
807 4329100
808 8002300
[8]: df.head()
1
1 2020-12-11 146.550003 151.500000 135.100006 139.250000 139.250000
2 2020-12-14 135.000000 135.300003 125.160004 130.000000 130.000000
3 2020-12-15 126.690002 127.599998 121.500000 124.800003 124.800003
4 2020-12-16 125.830002 142.000000 124.910004 137.990005 137.990005
Volume
0 70447500
1 26980800
2 16966100
3 10914400
4 20409600
[10]: df.tail()
Volume
804 4851100
805 5277900
806 4623700
807 4329100
808 8002300
2
[16]: # Calculate summary statistics
print(df.describe())
Volume
count 8.090000e+02
mean 6.489790e+06
std 5.239688e+06
min 1.995400e+06
25% 4.090600e+06
50% 5.189000e+06
75% 7.136800e+06
max 7.478640e+07
Return Analysis
[18]: # Calculate daily returns
df['Daily_Return'] = df['Close'].pct_change()
3
[22]: df
Volume Daily_Return
0 70447500 NaN
1 26980800 -0.037731
2 16966100 -0.066427
3 10914400 -0.040000
4 20409600 0.105689
.. … …
804 4851100 -0.016746
805 5277900 -0.022206
806 4623700 0.018691
807 4329100 0.009010
808 8002300 0.026331
Volatility Analysis
[24]: # Calculate daily volatility (standard deviation of daily returns)
daily_volatility = df['Daily_Return'].std()
print(f"Daily Volatility: {daily_volatility}")
804 4.981215
805 4.831215
806 4.907072
807 4.778500
808 5.076357
4
dtype: float64
Moving Averages
[27]: # Calculate 20-day and 50-day moving averages
df['MA_20'] = df['Close'].rolling(window=20).mean()
df['MA_50'] = df['Close'].rolling(window=50).mean()
[29]: df['MA_20']
[29]: 0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
…
804 149.61325
805 149.59575
806 149.56375
807 149.76325
808 150.42975
Name: MA_20, Length: 809, dtype: float64
[32]: df['MA_50']
[32]: 0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
…
804 143.7221
805 143.8965
806 144.0475
807 144.1709
808 144.3883
Name: MA_50, Length: 809, dtype: float64
plt.show()
5
Trading Strategy Backtesting
[36]: # Define a simple moving average crossover strategy
df['Signal'] = 0
df.loc[df['MA_20'] > df['MA_50'], 'Signal'] = 1 # Buy signal
df.loc[df['MA_20'] < df['MA_50'], 'Signal'] = -1 # Sell signal
[40]: df['Strategy_Returns']
[40]: 0 NaN
1 -0.000000
2 -0.000000
3 -0.000000
4 0.000000
…
804 -0.016746
805 -0.022206
806 0.018691
807 0.009010
808 0.026331
Name: Strategy_Returns, Length: 809, dtype: float64
6
804 0.518918
805 0.507395
806 0.516879
807 0.521536
808 0.535268
Name: Strategy_Returns, dtype: float64
Volume Analysis
[46]: # Calculate the average daily volume
avg_daily_volume = df['Volume'].mean()
print(f"Average Daily Volume: {avg_daily_volume}")
ax2 = ax1.twinx()
ax2.bar(df.index, df['Volume'], alpha=0.3, label='Volume')
ax2.set_ylabel('Volume')
ax2.legend(loc='upper right')
plt.show()
Event Study
7
[50]: # Define the event date
event_date = pd.Timestamp('2023-02-15')
[55]: event_window
[61]: start_date
[65]: initial_price
[65]: 157.47000122070312
[69]: volatility
[69]: 0.5241890169139842
for i in range(num_simulations):
for j in range(1, num_days):
simulated_prices[i, j] = simulated_prices[i, j - 1] * np.exp((drift - 0.
↪5 * volatility ** 2) / 252 +
volatility␣
↪* np.sqrt(1 / 252) * np.random.normal())
8
[74]: # Plot the simulated price distributions
plt.figure(figsize=(12, 6))
plt.hist(simulated_prices[:, -1], bins=50, density=True, label='Simulated␣
↪Prices')
[ ]: