The Hong Kong Polytechnic UniversityDepartment of Applied MathematicsAMA3632 Programming for Data ScienceAssignment 1Instructions:1. Due Date: 1 Nov 2019 at 5:30pm.2. Full marks of this assignment is 6 marks.3. Type your codes and outputs in the word document or a R notebook file. Give comments onyour codes. Handwriting codes are NOT accepted.4. Submission Method: Please submit ONLINE in Blackboard AND submit a HARD COPYassignment DURING THE LECTURE before the deadline. LATE assignments could NOTbe accepted by the Blackboard system.Questions:1. Consider the following very simple genetic model. A population consists of equal numbers of twosexes: male and female. At each generation men and women are paired at random, and each pairproduces exactly two offspring, one male and one female. We are interested in the distributionof height from one generation to the next.(a) Assume that the heights of the both male and female in the first generation are randomlychosen from a normal distribution with mean 160 cm and variance of 400 cm2. Use commands,dataframe, rnorm, to generate a data frame called “pop” for the first generation.(b) Suppose that the height of both children is just the average of the height of their parents.Write a function to generate a data frame containing the height of males and/or females fornext generation. You may use sample command to permutate the male and female heightin random. For calculation of mean height for the offspring, you may use apply and meancommands.(c) Use tAMA3632代做、代写Data Science、代写R编程he function from part (b) to generate nine generations and then use the lattice libraryto obtain histogram of male heights for nine generations. The last plot of heights phenomenof the male heights is called regression to the mean.2. For f : R → R, the NewtonRaphson algorithm uses a sequence of linear approximations to fto find a root. What happens if we use quadratic approximations instead? Suppose that xnis our current approximation to f; then a quadratic approximation to f at xn is given by thesecond-order Taylor expansion:f(x) ≈ gn(x) = f(xn) + (x − xn)fLet xn + 1 be the solution of gn(x) = 0 that is closest to xn, assuming a solution exists. Ifgn(x) = 0 has no solution, then let xn + 1 be the point at which gn attains either its minimumor maximum. Figure 1 illustrates the two cases.Figure 1: The iterative root-finding scheme of the Newton-Raphson algorithm based on the secondorderTaylor expansion.Implement this algorithm in R and use it to find the fixed points of the following functions:(a) cos(x) − x using x0 = 1, 3, 6.(b) log(x) − exp(−x) using x0 = 2.(c) x3 − x − 3 using x0 = 0.(d) x3 − 7x2 + 14x − 8 using x0 = 1.1, 1.2, ..., 1.9.(e) log(x) exp(−x) using x0 = 2.For your implementation, assume that you are given a function ftn(x) that returns the vector(f(x), f′(x), f′′(x)). Given xn, if you rewrite gn as gn(x) = a2x2 + a1x + a0 then you can use the1 − 4a2a0)/2a2 to find the roots of gn and thus xn+1. If gn has no roots thenthe min/max occurs at the point g′n(x) = 0.转自:http://www.3daixie.com/contents/11/3444.html
讲解:AMA3632、Data Science、R、RMatlab|Python
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。