Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Python function and save it as GIMME_THAT_AFTER_A_YEAR The function will predict if a portfolio yields the minimum return desired by an investor after

Create a Python function and save it as GIMME_THAT_AFTER_A_YEAR The function will predict if a portfolio yields the minimum return desired by an investor after one year. The function will have the following arguments: TICKERS : The symbols of the selected securities on Yahoo Finance. WEIGHTS : The asset weights in the portfolio. BEGIN : The beginning date of the historic data (inclusive). PROFIT : The minimum return desired by an investor after one year. For instance, if the investor desires at least 15% return after one year, PROFIT=0.15. The function will apply the steps similar to those in week #6, #7, and #8 of the asynchronous content. The function will output the following: The confusion matrix from the test results, The accuracy of the model on the test set, The no information rate of the test set, The predicted probability that the portfolio yields the minimum required return after one year. Fill in the blanks and complete the code. Please do not type any space characters in the blanks. GIMME_THAT_AFTER_A_YEAR( TICKERS , WEIGHTS , BEGIN , PROFIT ) #Compute the weighted sum of the historic prices as in video 6.1.1 from yfinance import download as PUMPKIN PORTFOLIO = 0 for X , Y in zip ( TICKERS , WEIGHTS ) : PORTFOLIO += Y * ( tickers= X , start= ) #For each trading day, compute the return on the portfolio after 252 trading days. #Save the findings as a new column in PORTFOLIO as in video 6.1.2 PORTFOLIO["ROLLING YEARLY RETURN"] = PORTFOLIO["Adj Close"]. (252). (-252) #Convert ROLLING YEARLY RETURN into a categorical variable as in video 6.1.2 from pandas import cut as GINGER PORTFOLIO["OUTCOME"] = ( x=PORTFOLIO["ROLLING YEARLY RETURN"] , =[ float("-inf") , , float("inf") ] , labels=[ f"< .0%} return after a year" , f" .0%} return after a year" ] ) #Compute J. Wilder's directional movement indicators as in video 6.1.3 from import adx as NUTMEG PREDICTORS = ( =PORTFOLIO.High , =PORTFOLIO.Low , =PORTFOLIO. , length=252 ) #Combine the predictor variables with the target variable into a data frame as in video 6.1.4 from pandas import concat as SPICE ALLES = ( objs=[ PORTFOLIO.OUTCOME , ] , axis= ) #Drop the columns with missing values since there is not sufficient historic data to compute those values. ALLES ( =True ) #Randomly split the data into training and test sets as in video 7.1.2 from sklearn.model_selection import train_test_split as LATTE TARGET = ALLES.OUTCOME FEATURES = ALLES.drop( columns="OUTCOME" ) TRAIN_X , TEST_X , TRAIN_Y , TEST_Y = ( , , =0.3 , =9999 ) #Train an extremely randomized trees classifier model as in video 7.1.3 from sklearn.ensemble import ExtraTreesClassifier as MAPLE FOREST = ( =250 , random_state=99 ) . ( , ) #Test the model and create the confusion matrix as in video 7.1.4 PREDICTIONS = . ( ) from pycm import ConfusionMatrix as CANDY_CORN TEST_RESULTS = ( actual_vector= . , = ) #Print the confusion matrix and save it as in video 8.1.2 PRINTED_MATRIX = . #Print the accuracy of the model on the test set as in video 8.1.2 ACCURACY = "The accuracy of the model on the test set is . :.2%}" #Print the no information rate of the test set as in video 8.1.2 NIR = "The no information rate of the test set is . :.2%}" #Compute the probability that the portfolio will yield the minimum required return after a year. #Output the predicted probabilities in the form of a data frame as shown in video 8.1.2 from pandas import DataFrame as PECAN_PIE PROBS = ( data=FOREST. ( PREDICTORS. (n=1) ) , columns=FOREST. ) return PRINTED_MATRIX , ACCURACY , NIR , PROBS

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Finance questions