Write a Python code that randomly generates 6 time series of asset prices. The time…
Private research annotation
Sign in to add a private research label or note.
- User
Write a Python code that randomly generates 6 time series of asset prices. The time series should be weekly and is 6 years long, starting from 1 January 2018. Use the cumsum function. Save these series in a dataframe called asset_df.
- Assistant
Here's an example Python code that uses NumPy to randomly generate 6 time series of asset prices using the
cumsumfunction, and then stores them in a Pandas DataFrame calledasset_df. The time series is weekly, and is 6 years long starting from 1 January 2018.pythonimport numpy as np import pandas as pd # Define number of weeks and years num_weeks = 6 * 52 num_assets = 6 # Set random seed for reproducibility np.random.seed(42) # Create date range for weekly data starting from 1 January 2018 date_rng = pd.date_range(start='1/1/2018', end='31/12/2023', freq='W') # Generate random price movements for each asset price_movements = np.random.randn(num_weeks, num_assets) # Use cumsum function to generate prices for each asset prices = 100 * np.cumsum(price_movements, axis=0) # Create a DataFrame to store asset prices asset_df = pd.DataFrame(prices, index=date_rng, columns=[f"Asset {i}" for i in range(1, num_assets+1)]) - User
Using the above data, use FFT to analyze the series in a sliding window fashion. The window should be 6 months long and each shift should be 1 month. Save the resulting frequencies in a new dataframe called fft_df.
- Assistant
Here's an example Python code that uses the Fast Fourier Transform (FFT) to analyze the 6 time series of asset prices in a sliding window fashion. The window is 6 months long, and each shift is 1 month. The resulting frequencies are stored in a new Pandas DataFrame called
fft_df.python - User
Use the frequencies in fft_df to train a self-organizing map. The SOM should be 4x4 in size. Afterwards, print out the cluster assignment of each data in a new dataframe, cluster_df. Perform all plotting with plotly,
- Assistant
- User
For each cluster in the SOM, plot the value of the averaged asset prices from each class.
- Assistant
Sure! Here's an example Python code that uses the cluster assignments in
cluster_dfto plot the average asset prices for each cluster in the SOM.pythonimport pandas as pd import plotly.graph_objects as go # Merge the cluster assignments with the original asset prices merged_df = pd.merge(cluster_df, asset_df, left_on="Data", right_index=True) # Calculate the average asset prices for each cluster grouped_df = merged_df.groupby("Cluster").mean() # Create a line plot for each cluster fig = go.Figure() for i, col in enumerate(grouped_df.columns): fig.add_trace(go.Scatter(x=grouped_df.index, y=grouped_df[col], mode='lines', name=f"Asset {i+1}")) # Set the axis labels and title fig.update_layout(xaxis_title="Cluster", yaxis_title="Average Asset Price", title="Average Asset Prices by Cluster") fig.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.