# AMA3632 AssignmentHelp With ,Ghostwriter Data Science Assignment,Ghostwriter R Programming Assignment,R AssignmentHelp With Debug Matlab Programming|Ghostwriter Python Programming

The Hong Kong Polytechnic University
Department of Applied Mathematics
AMA3632 Programming for Data Science
Assignment 1
Instructions:
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 on
your codes. Handwriting codes are NOT accepted.
4. Submission Method: Please submit ONLINE in Blackboard AND submit a HARD COPY
assignment DURING THE LECTURE before the deadline. LATE assignments could NOT
be accepted by the Blackboard system.
Questions:
1. Consider the following very simple genetic model. A population consists of equal numbers of two
sexes: male and female. At each generation men and women are paired at random, and each pair
produces exactly two offspring, one male and one female. We are interested in the distribution
of height from one generation to the next.
(a) Assume that the heights of the both male and female in the first generation are randomly
chosen 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 for
next generation. You may use sample command to permutate the male and female height
in random. For calculation of mean height for the offspring, you may use apply and mean
commands.
(c) Use the function from part (b) to generate nine generations and then use the lattice library
to obtain histogram of male heights for nine generations. The last plot of heights phenomen
of the male heights is called regression to the mean.
2. For f : R → R, the NewtonRaphson algorithm uses a sequence of linear approximations to f
to find a root. What happens if we use quadratic approximations instead? Suppose that xn
is our current approximation to f; then a quadratic approximation to f at xn is given by the
second-order Taylor expansion:
f(x) ≈ gn(x) = f(xn) + (x − xn)f
Let xn + 1 be the solution of gn(x) = 0 that is closest to xn, assuming a solution exists. If
gn(x) = 0 has no solution, then let xn + 1 be the point at which gn attains either its minimum
or maximum. Figure 1 illustrates the two cases.
Figure 1: The iterative root-finding scheme of the Newton-Raphson algorithm based on the secondorder
Taylor 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) x
3 − x − 3 using x0 = 0.
(d) x
3 − 7x
2 + 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) = a2x
2 + a1x + a0 then you can use the
1 − 4a2a0)/2a2 to find the roots of gn and thus xn+1. If gn has no roots then
the min/max occurs at the point g′n(x) = 0.