Analyzing Stock Data Project

Hello,

INTRODUCTION TO PYTHON

Analyzing Stock Data

I’m unable to figure out whats wrong below. Any help or suggestions is appreciated.

def display_as_percentage(val):
 return '{:.1f}%'.format(val * 100)

amazon_prices = [1699.8, 1777.44, 2012.71, 2003.0, 1598.01, 1690.17, 1501.97, 1718.73, 1639.83, 1780.75, 1926.52, 1775.07, 1893.63]
ebay_prices = [35.98, 33.2, 34.35, 32.77, 28.81, 29.62, 27.86, 33.39, 37.01, 37.0, 38.6, 35.93, 39.5]

# Write code here

def get_returns(prices):
 returns=[]
 for i in range(len(prices)):
   start_price = prices[i]
   end_price = prices[i-1]
   log_return = calculate_log_return(start_price, end_price)
   returns.append(log_return)
 return returns
amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)

print(amazon_returns)

you call a function here:

log_return = calculate_log_return(start_price, end_price)

where is calculate_log_return defined? Could you share the project url? So we can access the lesson

here is mine… dont know what went wrong either

I set the range to (len(prices)-1). The length of the get_returns result would be 12, shorter than the length of prices (13). If we leave range equal len(prices), the end_price would be (i+1) = (13+1) = the 14th price, which does not exist.

def get_returns(prices):
 returns=[]
 for i in range(len(prices)-1):
   start_price = prices[i]
   end_price = prices[i+1]
   log_return = calculate_log_return(start_price, end_price)
   returns.append(log_return)
 return returns

amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)
5 Likes

My final output doesn’t look right. Unfortunately, the only answer I can find is the final correlation number, which should be 0.67 (apparently). These numbers seem easy enough to calculate, so I’m not sure what I’m doing wrong. Any help would be really appreciated.

from utils import *

def display_as_percentage(val):
  return '{:.1f}%'.format(val * 100)

amazon_prices = [1699.8, 1777.44, 2012.71, 2003.0, 1598.01, 1690.17, 1501.97, 1718.73, 1639.83, 1780.75, 1926.52, 1775.07, 1893.63]
ebay_prices = [35.98, 33.2, 34.35, 32.77, 28.81, 29.62, 27.86, 33.39, 37.01, 37.0, 38.6, 35.93, 39.5]

# Write code here

# Calculate Log Return
#calculate_log_return(start_price, end_price)

def get_returns(prices):
  returns = []
  
  for price in range(len(prices)):
    start_price = prices[price]
    end_price = prices[price + 1]
    returns.append(calculate_log_return(start_price, end_price))
    
    return returns

amazon_returns = get_returns(amazon_prices)
ebay_returns = get_returns(ebay_prices)

print("The average monthly return of Amazon is " + display_as_percentage(amazon_returns[0]))
print("The average monthly return of Ebay is " + display_as_percentage(ebay_returns[0]))

print("The annual return of Amazon is " + display_as_percentage((sum(amazon_returns)) / 12))
print("The annual return of Ebay is " + display_as_percentage((sum(ebay_returns)) / 12))

# Calculate Variance
#calculate_variance(dataset)

amazon_monthly_variance = calculate_variance(amazon_prices)
ebay_monthly_variance = calculate_variance(ebay_prices)

print(f"The monthly variance of Amazon is {amazon_monthly_variance:.1f}")
print(f"The montly variance of Ebay is {ebay_monthly_variance:.1f}")

# Calculate Standard Deviation
#calculate_stddev(dataset)

amazon_stddev = display_as_percentage(calculate_stddev(amazon_prices))
ebay_stddev = display_as_percentage(calculate_stddev(ebay_prices))

print("The standard deviation of Amazon is " + amazon_stddev)
print("The standard deviation of Ebay is " + ebay_stddev)


# Calculate Correlation Coefficient
#calculate_correlation(set_x, set_y)

print(f"The correlation of Amazon and Ebay is  {calculate_correlation(amazon_prices, ebay_prices):.2f}")

My output is:

The average monthly return of Amazon is 4.5%
The average monthly return of Ebay is -8.0%
The annual return of Amazon is 0.4%
The annual return of Ebay is -0.7%
The monthly variance of Amazon is 21959.0
The montly variance of Ebay is 12.6
The standard deviation of Amazon is 14818.6%
The standard deviation of Ebay is 354.4%
The correlation of Amazon and Ebay is  0.53

Hi fellow learner,

The get_return function is the one that all other data resort to.
And your code in get_return seems to be wrong.

This is my get-return function code and it has given me correct results all through the final value of correlation of 0.67

I would like to point out 2 things:

  1. range(len(prices)-1: You need to subtract one from the length of the range, because in the for-loop you want to refer to indeces. Remember indeces start counting from zero.
  2. your “return returns” needs to be indented more to the left, in line with the function, and not with the for loop.

Good luck!

4 Likes

Hello Jabevan

Most of the calculation base on the monthly return, as your work show that you calculate base on the price of stock each month so that the results lead wrong.

My output look like:
The value formatted as a percentage is 7.5%
the monthly returns of Amazon is 4.5%
the monthly returns of Ebay is -8.0%
the annual returns of Amazon is 10.8%
the annual returns of Ebay is 9.3%
the variance of Amazon monthly returns 0.010738060556609724
the variance of Ebay monthly returns 0.007459046435081462
the standard deviation of Amazon is 10.4%
the standard deviation of Ebay is 8.6%
calculate the correlation between the stock returns is 0.6776978564073072

I hope this can help

1 Like

I’m not sure where I went wrong on this.

def get_returns(prices):

  returns = []

  for i in range(len(prices)-1):

    start_price = prices[i]

    end_price = prices[i+1]

    log_return = calculate_log_return(start_price, end_price)

    returns.append(log_return)

  return returns  

amazon_returns = get_returns(amazon_prices)

ebay_returns = get_returns(ebay_prices)

print("Amazon monthly returns is " +display_as_percentage(amazon_returns[0]))

print("Ebay monthyl return is " + display_as_percentage(ebay_returns[0]))
#Variance

amazon_variance = calculate_variance(amazon_prices)

ebay_variance = calculate_variance(ebay_prices)

print("Amazon Variance is ", amazon_variance)

print("Ebay Variance is ", ebay_variance)
#Stddev

amazon_stddev = display_as_percentage(calculate_stddev(amazon_prices))

ebay_stddev = display_as_percentage(calculate_stddev(ebay_prices))

print("Amazon Stddev is ", amazon_stddev)

print("Ebay Stddev is ", ebay_stddev)
#Correlation

correlation = calculate_correlation(amazon_prices, ebay_prices)

print("Correlation is ", correlation)

The output is

Amazon monthly returns is 4.5%
Ebay monthyl return is -8.0%
Amazon Variance is 21958.963528994085
Ebay Variance is 12.559163313609465
Amazon Stddev is 14818.6%
Ebay Stddev is 354.4%
Correlation is 0.5304921683060496

Hi,

When you are calculating the variance & standard deviation you should be using the monthly returns as the dataset not the prices for Amazon & ebay.

It would really help if this project had a solution available or a project walk-through video, the hints aren’t enough help.

1 Like

I hope this is helpful. The only reason I figured it out (I think I figured it out, anyway) is due to the posts in this thread. I used them as a starting point, ran and re-ran the code, re-worked the code, and ran it again until it worked and it looked right to me.

If something looks wonky, please let me know.

Happy coding!

# Code from utils.py from math import log, sqrt # Calculate Log Return def calculate_log_return(start_price, end_price): return log(end_price / start_price) # Calculate Variance def calculate_variance(dataset): mean = sum(dataset) / len(dataset) numerator = 0 for data in dataset: numerator += (data - mean) ** 2 return numerator / len(dataset) # Calculate Standard Deviation def calculate_stddev(dataset): variance = calculate_variance(dataset) return sqrt(variance) # Calculate Correlation Coefficient def calculate_correlation(set_x, set_y): sum_x = sum(set_x) sum_y = sum(set_y) sum_x2 = sum([x ** 2 for x in set_x]) sum_y2 = sum([y ** 2 for y in set_y]) sum_xy = sum([x * y for x, y in zip(set_x, set_y)]) n = len(set_x) numerator = n * sum_xy - sum_x * sum_y denominator = sqrt((n * sum_x2 - sum_x ** 2) * (n * sum_y2 - sum_y ** 2)) return numerator / denominator # Code from script.py begins here, sans import from utils def display_as_percentage(val): return "{:.1f}%".format(val * 100) amazon_prices = [ 1699.8, 1777.44, 2012.71, 2003.0, 1598.01, 1690.17, 1501.97, 1718.73, 1639.83, 1780.75, 1926.52, 1775.07, 1893.63, ] ebay_prices = [ 35.98, 33.2, 34.35, 32.77, 28.81, 29.62, 27.86, 33.39, 37.01, 37.0, 38.6, 35.93, 39.5, ] # Step 1 does not require code. # Step 2 def get_returns(prices): returns = [] # Step 3 for i in range(len(prices) - 1): start_price = prices[i] end_price = prices[i + 1] # Step 4 log_return = calculate_log_return(start_price, end_price) returns.append(log_return) return returns # Step 5 amazon_returns = get_returns(amazon_prices) ebay_returns = get_returns(ebay_prices) x = [display_as_percentage(returns) for returns in amazon_returns] y = [display_as_percentage(returns) for returns in ebay_returns] # Step 6 print("The monthly returns for Amazon stock are: ", ", ".join(x), ".") print("The monthly returns for eBay stock are: ", ", ".join(y), ".") print("Amazon has more profitable months than eBay.\n") # Step 7 print( "The annual rate of return for Amazon stock is", display_as_percentage(sum(amazon_returns)), ".", ) print( "The annual rate return for eBay stock is", display_as_percentage(sum(ebay_returns)), ".", ) print("Amazon’s annual return is slightly higher than eBay’s annual return.\n") # Step 8 amazon_variance = calculate_variance(amazon_returns) ebay_variance = calculate_variance(ebay_returns) print( "The variance for Amazon’s monthly returns is ", display_as_percentage(amazon_variance), ".", ) print( "The variance for eBay's monthly returns is ", display_as_percentage(ebay_variance), ".", ) print("The variance for Amazon’s monthly returns is slightly higher than eBay’s.\n") # Step 9 amazon_stddev = calculate_stddev(amazon_returns) ebay_stddev = calculate_stddev(ebay_returns) print( "The standard deviation for Amazon's monthly returns is ", display_as_percentage(amazon_stddev), ".", ) print( "The standard deviation for eBay's monthly returns is", display_as_percentage(ebay_stddev), ".\n", ) # Step 10 correlation = calculate_correlation(amazon_returns, ebay_returns) print( "The correlation between the stock returns is ", display_as_percentage(correlation), ", so there is a moderate positive correlation.", )

Man… I need some encouragement. There is no solution to this project and the prior learning lessons have been way too vague. How in the world does one make sense of any of this with the lack of proper instruction or supporting resources? Seriously, what am I missing… Per this forum everyone even has the same variable name “log_return” out of seemingly nowhere. This isn’t specified in the instructions and certainly can’t be a coincidence…

felt the same when I got to this point with no clear instructions or how to get a solution… basically gave up on this as I didn’t want to go through this without knowing for sure that each project comes with proper solutions…

Guys! the problem I’m seeing is that some of you are calculating the annual log return in the first step and the result should be log rate of return monthly, which will output two lists ebay returns and Amazon return, those are the one that we need to use to calculate variance, standard deviation, correlation, not prices as somebody said before. This is my code if it can help:



2 Likes

This is what I got as well

1 Like

If you read the question closely its asking for the variance of the returns, as opposed to the prices. Bit confusing since in the lesson we were calculating the variance of the prices.