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
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...