 # 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)
``````
2 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))
print("The average monthly return of Ebay is " + display_as_percentage(ebay_returns))

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!