联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Algorithm 算法作业Algorithm 算法作业

日期:2020-03-04 10:30

Stats 102A HW4 Due March 3, 2020

General Notes

• You will submit a minimum of three files, the core files must conform to the following

naming conventions (including capitalization and underscores). 123456789 is a placeholder,

please replace these nine digits with your nine-digit Bruin ID. The files you must submit are:

1. 123456789_stats102a_hw1.R: An R script file containing all of the functions you wrote

for the homework. The first line of your .Rmd file, after loading libraries, should be

sourcing this script file.

2. 123456789_stats102a_hw1.Rmd: Your markdown file which generates the output file

of your submission.

3. 123456789_stats102a_hw1.html/pdf : Your output file, either a PDF or an HTML file

depending on the output you choose to generate.

4. included image files: You may name these what you choose, but you must include all of

the image files you generated for your structured flowcharts, otherwise your file will not

knit.

If you fail to submit any of the required core files you will receive ZERO points for the

assignment. If you submit any files which do not conform to the specified naming

convention, you will receive (at most) half credit for the assignment.

• Your .Rmd file must knit. If your .Rmd file does not knit you will receive (at most) half

credit for the assignment.

The two most common reason files fail to knit are because of workspace/directory structure

issues and because of missing include files. To remedy the first, ensure all of the file paths in

your document are relative paths pointing at the current working directory. To remedy the

second, simply make sure you upload any and all files you source or include in your .Rmd

file.

• Your coding should adhere to the tidyverse style guide: https://style.tidyverse.org/.

• All flowcharts should be done on separate sheets of paper, but be included, inline as images,

in your final markdown document.

• Any functions/classes you write should have the corresponding comments as the following

format.

my_function <- function(x, y, ...){

#A short description of the function

#Args:

#x: Variable type and dimension

#y: Variable type and dimension

#Return:

#Variable type and dimension

Your codes begin here

}

1

Stats 102A HW4 Due March 3, 2020

NOTE: Everything you need to do this assignment is here, in your class notes, or was covered in

discussion or lecture.

• DO NOT look for solutions online.

• DO NOT collaborate with anyone inside (or outside) of this class.

• Work INDEPENDENTLY on this assignment.

• EVERYTHING you submit MUST be 100% your, original, work product. Any student

suspected of plagiarizing, in whole or in part, any portion of this assignment, will be

immediately referred to the Dean of Student’s office without warning.

1: Dealing with Large Numbers

To calculate with large floating point numbers we define objects called (p, q) number with a list as

its base type. The list should have four components. The first one is either the integer +1 or the

integer −1. It gives the sign of the number. The second and third are p and q. And the fourth

component is a vector of p + q + 1 integers between zero and nine. For example,

x <- structure(list(sign = 1, p = 3, q = 4, nums = 1:8), class = "pqnumber")

(a) Write a constructor function, an appropriate predicate function, appropriate coercion

functions, and a useful print() method.

• The constructor takes the four arguments: sign, p, q, and nums. Then check if the

arguments satisfy all requirements for a (p, q) number. If not, stop with an error

message. If yes, return a (p, q) number object.

• A predicate is a function that returns a single TRUE or FALSE, like is.character(), or

is.NULL(). Your predicate function should be is_pqnumber() and should behave as

expected.

• A useful print() method should allow users to print a (p, q) number object with the

decimal representation defined as follows:

x =

X

q

s=−p

xs × 10s

Thus p = 3 and q = 4 with nums = 1:8 has the decimal value

0.001 + 0.002 + 0.3 + 4 + 50 + 600 + 7000 + 80000 = 87654.321

• A coercion function forces an object to belong to a class, such as as.character() or

as_tibble(). You will create a generic coercion function as_pqnumber() which will

accept a numeric or integer argument x and return the appropriate (p, q) number object.

For example, given the decimal number 3.14 with p = 3 and q = 4, the function will

return a (p, q) number with num = c(0, 4, 1, 3, 0, 0, 0, 0). You should also create a

generic as_numeric() function and a method to handle a (p, q) number.

2

Stats 102A HW4 Due March 3, 2020

(b) Write an addition function and a subtraction function

Suppose we have two positive (p, q) number objects x and y. Write a function to calculate the

sum of x and y. Clearly its decimal representation is

x + y =

X

q

s=−p

(xs + ys) × 10s

but this cannot be used directly because we can have xs + ys > 9.

So we need a carry-over algorithm that moves the extra digits in the appropriate way. Same

as one would do when adding two large numbers on paper. Also we need a special provision

for overflow, because the sum of two (p, q) number objects can be too big to be a (p, q)

number.

A subtraction algorithm should have a carry-over algorithm that borrows 10 in the same way

as you would do a subtraction with pencil-and-paper.

Your functions should work for both positive and negative (p, q) numbers.

(c) Write a multiplication function Write a function which can multiply two pqnumber objects.

Think about how you would multiply two large numbers by hand and implement that

algorithm in R for two pqnumber objects.

2: Statistical Computing

Skewness and Kurtosis:

For a vector of values x = (x1, x2, ..., xn), the kth sample moment of x around c is defined as.

The first (k = 1) sample moment around c = 0 is called the sample mean x¯ =

1ni=1 xi.

A sample moment is called central if it is a moment around the mean, i.e., c = ¯x. That is, the

kth sample central moment of x is

The skewness of x is defined as

γ1 =µ3(µ2)3/2,

and the kurtosis of x is defined as

γ2 =µ4µ22− 3,

where muk is the kth central moment of x.

Suppose we have a vector of numbers x.

(a) Write a function to compute the sample moments of x around 0 for k = 1, 2, 3, 4.

3

Stats 102A HW4 Due March 3, 2020

(b) Write a function to compute the sample central moments of x for k = 1, 2, 3, 4.

(c) Write a function to compute the skewness and kurtosis of x.

For each question, the function should take the vector x as an argument and return a list or

vector of results. The functions can call each other if that seems desirable. Try to avoid loops. Do

not use built-in function such as mean() or var(), except possibly for checking your results. You

may use the sum() function.

For each function you need to test that it works by applying it to randomly generated numbers.

Specifically, use at least 4 different built-in R functions for generating random numbers (use

?Distributions for a list of possible functions to use) to generate random vectors (of at least

length 1,000) to try out your code. Generate a table with skewness and kurtosis results for the

four distributions you chose. Do not print a table directly from R; collect the results that are

needed and then put them in a Markdown table.

4


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp