# 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)
``````
3 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!

1 Like

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

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.