#!/usr/bin/env python # coding: utf-8 # In[6]: # !pip install yfinance # uncomment these to install missing packages if they are not already installed # !pip install pandas import yfinance as yf import pandas as pd def get_price(tick,start='2022-10-01',end=None): return yf.Ticker(tick).history(start=start,end=end)['Close'] def get_prices(tickers,start='2022-10-01',end=None): df=pd.DataFrame() for s in tickers: df[s]=get_price(s,start,end) return df # # Prepare training and testing data sets # In[2]: feature_stocks=['tsla','meta','twtr','amzn','nflx','gbtc','gdx','intc','dal','c'] predict_stock='msft' # training set start_date_train='2022-10-01' end_date_train='2022-12-31' X_train=get_prices(feature_stocks,start=start_date_train,end=end_date_train) y_train=get_prices([predict_stock],start=start_date_train,end=end_date_train) # testing set start_date_test='2023-01-01' # end date omit, default is doday X_test=get_prices(feature_stocks,start=start_date_test) y_test=get_prices([predict_stock],start=start_date_test) # In[4]: X_train # In[5]: y_train # # Convert training and testing data into numpy array # In[ ]: import numpy as np X_train=np.array(X_train) y_train=np.array(y_train) X_test=np.array(X_test) y_test=np.array(y_test) # # Use linear regression to predict msft stock price from the other stocks' prices # ## 1. Append a dummy feature to both X_train and X_test # In[ ]: # Your solution here # ## 2. Find the best linear regression model based on your training data ($w=(X X')^{-1} X y$) # ### Note that you may need to transpose the matrices to make things work # # # In[ ]: # Your solution here # ## 3. Report your training and testing error # ### How far your prediction from the actual price. Compute the mean square error for both training and testing # In[ ]: # Your solution here # In[ ]: # In[ ]: