Problem 5: Correlation Many of us are interested in determining whether one thing is related to...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Problem 5: Correlation Many of us are interested in determining whether one thing is related to another functionally. In particular, assume you have the stock prices for a couple of companies: Acme ZXY 62.50 85.40 112.0 202.55 62.00 103.55 122.5 202.15 55.00 98.90 75.50 151.10 117.50 181.85 118.50 197.30 59.50 85.70 121.50 195.20 The simplest type of mathematical relationship is linear. If we let ACME's value bez and ZXY's value be y, we might look for: y = mx + b (22) for constants m, b. We identify mas slope and as the y-intercept. Looking at the data, the values seem a bit all over the place. But sometimes, it's better to check mathematically and visually. We first need to define some values. The mean is the value that is nearest to all the values. Suppose we have a list of numbers 21,22,... The mean is defined: EM- 2₂ (23) +2₂+...+2) (24) We've briefly discussed. It's just an fancy way to sum! In fact, Python's sum() takes a list of numbers and returns the sum. If you divide by the length of the list, you'll get the mean. Session >>> x1st = [2,4,1,1,5] >>> mean = sum(x1st)/len(x1st) >>> mean 2.6 We need one last sum-it's called the sample variance (it measures the spread around the mean): (2-2)² (25) We finally have what need. Suppose we have two lists of numbers x,y that both have n values. We define the correlation coefficient as: r(x,y) (4) (26) Observe we've taken the square root of the variance-called standard deviation. The value of r ranges from 1 to 1. When it's near zero, r indicates a linear relationship is unlikely. When it's near 1, r indicates a positive relationship and for -1, a negative relationship. Consider this set of numbers A and B B value 5 Implementing the functions, we find that r = .7735. We plotted the data and tried to fit a line (we'll learn how to do this computationally next week) that captures the linear relationship. at m 2 A 2 4 1 1 1 5 B 3 3 1.4 .5 3 r=0.774 3 A values 6 Figure 4: Plot of points and a line. The variable r is the correlation coefficient. 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 def mean(1st): 5 6 7 8 def sd(xlst): 9 10 #TODO: Implement function return 1 11 12 def r(x, y): 13 27 28 # TODO: Implement function return 1 14 15 16 def theGraph(): 17 18 19 20 21 if _name == "__main__": 22 23 24 25 26 32 33 34 35 36 37 # TODO: Implement function return 1 pass # TODO: Implement function (D2) 29 30 plt.plot(t, t.65.5,'g') 31 plt.axis([0,6,0,61) x= [[2,3], [4,3],[1,1.4],[1,.5], [5,3]] # TODO: Complete code (D1) by calling the important function print (rValue) # I wonder where this value came from # Example of creating a plot plt.plot([i[0] for i in x], [i[1] for i in x], 'ro') t = np.arange(0,6,.1) correlation.py plt.xlabel("A values") plt.ylabel("B value") plt.title("r= (0:.3)". format (rValue)) plt.show() the Graph() Output 0.7735676940395084 Deliverables Programming Problem 5 • Complete the functions based on the information above. • Complete the code to get the output of the small example. (D2) Create a file with the data from the two stocks and call it acme_zyx.txt (by hand). • acme_zyx.txt should be 2 columns, 10 rows (no column headers). The values should be separated by spaces. • Using the data from the file you created (Assignment10/acme_zyx.txt), find the correlation and render the plot naming it stock.png. (D2) • The plot needs to be saved to stock.png programmatically. • Put your finished code in a new module correlation.py Problem 5: Correlation Many of us are interested in determining whether one thing is related to another functionally. In particular, assume you have the stock prices for a couple of companies: Acme ZXY 62.50 85.40 112.0 202.55 62.00 103.55 122.5 202.15 55.00 98.90 75.50 151.10 117.50 181.85 118.50 197.30 59.50 85.70 121.50 195.20 The simplest type of mathematical relationship is linear. If we let ACME's value bez and ZXY's value be y, we might look for: y = mx + b (22) for constants m, b. We identify mas slope and as the y-intercept. Looking at the data, the values seem a bit all over the place. But sometimes, it's better to check mathematically and visually. We first need to define some values. The mean is the value that is nearest to all the values. Suppose we have a list of numbers 21,22,... The mean is defined: EM- 2₂ (23) +2₂+...+2) (24) We've briefly discussed. It's just an fancy way to sum! In fact, Python's sum() takes a list of numbers and returns the sum. If you divide by the length of the list, you'll get the mean. Session >>> x1st = [2,4,1,1,5] >>> mean = sum(x1st)/len(x1st) >>> mean 2.6 We need one last sum-it's called the sample variance (it measures the spread around the mean): (2-2)² (25) We finally have what need. Suppose we have two lists of numbers x,y that both have n values. We define the correlation coefficient as: r(x,y) (4) (26) Observe we've taken the square root of the variance-called standard deviation. The value of r ranges from 1 to 1. When it's near zero, r indicates a linear relationship is unlikely. When it's near 1, r indicates a positive relationship and for -1, a negative relationship. Consider this set of numbers A and B B value 5 Implementing the functions, we find that r = .7735. We plotted the data and tried to fit a line (we'll learn how to do this computationally next week) that captures the linear relationship. at m 2 A 2 4 1 1 1 5 B 3 3 1.4 .5 3 r=0.774 3 A values 6 Figure 4: Plot of points and a line. The variable r is the correlation coefficient. 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 def mean(1st): 5 6 7 8 def sd(xlst): 9 10 #TODO: Implement function return 1 11 12 def r(x, y): 13 27 28 # TODO: Implement function return 1 14 15 16 def theGraph(): 17 18 19 20 21 if _name == "__main__": 22 23 24 25 26 32 33 34 35 36 37 # TODO: Implement function return 1 pass # TODO: Implement function (D2) 29 30 plt.plot(t, t.65.5,'g') 31 plt.axis([0,6,0,61) x= [[2,3], [4,3],[1,1.4],[1,.5], [5,3]] # TODO: Complete code (D1) by calling the important function print (rValue) # I wonder where this value came from # Example of creating a plot plt.plot([i[0] for i in x], [i[1] for i in x], 'ro') t = np.arange(0,6,.1) correlation.py plt.xlabel("A values") plt.ylabel("B value") plt.title("r= (0:.3)". format (rValue)) plt.show() the Graph() Output 0.7735676940395084 Deliverables Programming Problem 5 • Complete the functions based on the information above. • Complete the code to get the output of the small example. (D2) Create a file with the data from the two stocks and call it acme_zyx.txt (by hand). • acme_zyx.txt should be 2 columns, 10 rows (no column headers). The values should be separated by spaces. • Using the data from the file you created (Assignment10/acme_zyx.txt), find the correlation and render the plot naming it stock.png. (D2) • The plot needs to be saved to stock.png programmatically. • Put your finished code in a new module correlation.py
Expert Answer:
Answer rating: 100% (QA)
Solution import numpy as np import matplotlibpyplot as plt ... View the full answer
Related Book For
Elementary Statistics Picturing the World
ISBN: 978-0321911216
6th edition
Authors: Ron Larson, Betsy Farber
Posted Date:
Students also viewed these programming questions
-
A random sample of the closing stock prices for a company in a recent year is listed. Assume that is $2.62. Use the information to construct the 90% and 99% confidence intervals for the population...
-
The following table lists the stock prices for Microsoft from 1989 to 1998. The company did not pay any dividends during the period Year Price (dollars) 1989 .......... 1.20 1990 .......... 2.09 1991...
-
The following are the closing stock prices for a pharmaceutical firm over the past two weeks. a. Specify the competing hypotheses to determine whether the median stock price is greater than $61.25....
-
What is the relationship between group norms and group cohesiveness? What roles do both cohesiveness and norms plan in shaping group performance?
-
1. Our Topic: After reading and discussing past research about academic honesty and the use of technology throughout this textbook, we realize that these topics do not need to be mutually exclusive....
-
prepare the cash budget for October, November, and December, and for the quarter in total thank you! The company generally borrows money during this quarter to support peak sales. The above cash...
-
Write a two-page report explaining the two basic strategies that S&S, Inc. can pursue and the three different strategic positions that it can adopt. Include in your report a discussion and examples...
-
The bookkeeper for Geronimo Company has prepared the following balance sheet as of July 31, 2014. The following additional information is provided. 1. Cash includes $1,200 in a petty cash fund and...
-
Business Model Innovation: How Dollar Shave Club Disrupted Gillette . How was Gillette initially able to gain a competitive advantage? Was Gillette able to sustain its competitive advantage? If so,...
-
Determine the breakeven volume of injections for 2016 using the following formula for the contribution margin ratio approach: Breakeven revenue = Total fixed costs + [(Total variable costs / Total...
-
About the microstructure of cathodes in Li ion batteries, which one of the following requirements is not necessary? A) Li ions can reach most particles of active cathode material (such as Li1-xC0O2)....
-
What different types of governmental auditing positions are there? How does governmental auditing compare to internal auditing?
-
What is the standards-making body for public accounting firms?
-
What are the five General Standards contained in the Standards for the Professional Practice of Internal Auditing?
-
Explain briefly each of the provisions in the Statement of Responsibilities of Internal Auditing.
-
What authority does The IIA have in the enforcement of its standards and Code of Ethics? Explain.
-
Write 2,500 word paper, compare and contrast the fundamental principles, applications, and advantages of job order costing, process costing, and activity-based costing. Investigate how these costing...
-
A. Select a recent issue (paper or online) of Report on Business Magazine, Canadian Business Magazine (online only), Bloomberg Businessweek, Fast Company, The Economist, or another business magazine....
-
An automobile manufacturer finds that 1 in every 2500 automobiles produced has a particular manufacturing defect. (a) Use a binomial distribution to find the probability of finding 4 cars with the...
-
A misleading graph is a statistical graph that is not drawn appropriately. This type of graph can misrepresent data and lead to false conclusions. In Exercises 39, (a) Explain why the graph is...
-
A company that makes cola drinks states that the mean caffeine content per 12-ounce bottle of cola is 40 milligrams. You want to test this claim. During your tests, you find that a random sample of...
-
What are the basic rights of the owner of a share of corporate stock? In what way are these basic rights commonly modified with respect to the owner of a share of preferred stock?
-
Explain the following terms: a. Stock transfer agent. b. Stockholders subsidiary ledger. c. Underwriter. d. Stock registrar.
-
On April 1,2008 , Sanders Construction paid \(\$ 10,000\) for equipment with an estimated useful life of 10 years and a residual value of \(\$ 2,000\). The company uses the doubledeclining-balance...
Study smarter with the SolutionInn App