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