Problem 1

Write an R function “sum.geom” to calculate the sum of n terms in a geometric series

\(s_n(x,r)=x+xr+xr^2+xr^3+.....+xr^n-1\)

sum.geom <-function(x , r , n){
  sn = 0            ## The trivial cases are that sn(0,r)=0 for all n and r
                    ## and sn(0,r)= x for all n and x
  if (n > 1000000 && abs(r) <1){
    return(x/(1-r))                       ##  For the infinite sum lower 
  } else if (n > 1000000 && abs(r) >= 1){
    return(Inf)                           ##  For the infinite sum  upper 
  } else if (n < 1){
    return("Positive n Terms")   
  } else if (r != 1) {
    return(x*(1-r^(n))/(1-r))            
  } else {
    for(i in 1:n){
      sn = sn + x*r^(i-1)                 
    }
    return(sn)
  }
}

problem 1 output

Test Your Function at the following parameters

sum.geom(0,0.75,25) 
## [1] 0
sum.geom(3,0,25)
## [1] 3
sum.geom(3,1,25) 
## [1] 75
sum.geom(5, 0.75, 25)
## [1] 19.98495

Problem 2

Write an R function “vec.cos” which takes two vectors in \(R^d\) and returns their lengths as well as the cosine of the angle between them.

vec.cos <-function(x,y){
# Calculates the norm 
  x.len = norm(x, type = "2")
  y.len = norm(y, type = "2")     
  
  if (x.len == 0){
    return("Function cannot be performed because magnitude of first vector is 0!")
  } else if (y.len == 0) {
    return("Function cannot be performed because magnitude of second vector is 0!")
  } else if(length(x) != length(y)) {
    return("Vectors are not of the same Dimension!")
  }else {
  
  dot_xy = x%*%y                  ### Dot product as done by in numerator.
  
  cosine_xy = dot_xy/(x.len*y.len) ## Cosine of angle between x and y
  
  return(cosine_xy)
  }
  }

Testing Function

# Example 1
x <- c(12,3,45,22,67) 
y <- c(91,32,17,13,90)
vec.cos(x,y) 
##           [,1]
## [1,] 0.7316315
# Example 2
x <- c(0,0,0,0,0) 
y <- c(91,32,17,13,90) 
vec.cos(x,y) 
## [1] "Function cannot be performed because magnitude of first vector is 0!"
# Example 3
x <- c(12,3,45,22,67,11) 
y <- c(91,32,17,13,90)
vec.cos(x,y) 
## [1] "Vectors are not of the same Dimension!"

Problem 3

Write an R recursive function “choose.rec” to calculate \((n / x)\) using these facts.

choose.rec <-function(n,x){
  if(x == 0 | x == n){
    return(1)
  } else {
    return(factorial(n-1)/(factorial(x)*factorial(n-x-1)) + choose.rec(n-1,x-1))
  }
}
n <- 10 
x <- 3  
choose.rec(n=10,x=3)
## [1] 120
n <- 10 
x <- n 
choose.rec(n=10,x=n)
## [1] 1
n <- 10 
x <- 0 
choose.rec(n=10,x=0) 
## [1] 1

Problem 4

Write an R function “Test.Conf.Int.for.variance” to conduct a test statistic and construct a confidence interval for the population variance

library( EnvStats)
## 
## Attaching package: 'EnvStats'
## The following objects are masked from 'package:stats':
## 
##     predict, predict.lm
## The following object is masked from 'package:base':
## 
##     print.default
cholesterol.level <- c(1.7, 2.5, 2.2, 1,9, 3.6, 6.9, 5.1, 4.2, 5.5, 7.2, 
                       3.0, 5.8, 4.9, 9.9, 7.1, 5.4, 6.2, 4.5, 6.3, 8.2, 
                       5.7, 4.4, 7.9, 3.2, 4.8, 3.5, 7.6)

### Confidence interval function.
Test.Conf.Int.for.variance<- function(data, sigma.squared = 1, 
                                      conf = 0.95, tail.test = "Two Tail"){
  df = length(data)-1      ##degree of freedom.
  
  v = sd(data)^2           ##sample Variance
  
  chi_test = (length(data)-1)*v/sigma.squared ## Chi-Squared Statistic
  
  cat("Sample Variance: ",v,"\n")
  cat("Chi-Squared Test Statistic: ",chi_test,"\n")

  if (tail.test == "Lower-Tailed Test"){
    
    Upper= (df*v)/qchisq(1-conf, df, lower.tail = TRUE)
    
    Lower= 0
    
    p.value = pchisq(chi_test, df,lower.tail =TRUE) 
    cat("Chi-Squared p.Value: ",p.value,"\n")
    cat("95% Confidence Interval: (",Upper,", ",Lower,")")
  
    } else if  (tail.test == "Upper-Tailed Test"){
  
    Upper= Inf
      
    Lower= (df*v)/qchisq(1-conf, df, lower.tail = FALSE) 
      
    p.value = pchisq(chi_test, df,lower.tail =FALSE) 
    cat("Chi-Squared p.Value: ",p.value,"\n")
    cat("95% Confidence Interval: (",Upper,", ",Lower,")")
    
  } else {
  
    Upper= (df*v)/qchisq((1+conf)/2, df, lower.tail = FALSE)
  
    Lower= (df*v)/qchisq((1-conf)/2, df, lower.tail = FALSE)
    
    p.value = 2*pchisq(chi_test, df,lower.tail =FALSE) 
    cat("Chi-Squared p.Value: ",p.value,"\n")
    cat("95% Confidence Interval: (",Upper,", ",Lower,")")
    
    }
}
##Testing Function at 1
Test.Conf.Int.for.variance(cholesterol.level)
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  134.7868 
## Chi-Squared p.Value:  5.551474e-16 
## 95% Confidence Interval: ( 9.248833 ,  3.120461 )
##Testing Function at 2
Test.Conf.Int.for.variance(cholesterol.level,sigma.squared = 2)
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  67.39339 
## Chi-Squared p.Value:  5.239093e-05 
## 95% Confidence Interval: ( 9.248833 ,  3.120461 )
##Testing Function at 3
Test.Conf.Int.for.variance(cholesterol.level,sigma.squared = 3)
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  44.92893 
## Chi-Squared p.Value:  0.03308689 
## 95% Confidence Interval: ( 9.248833 ,  3.120461 )
##Testing Function at 3
Test.Conf.Int.for.variance(cholesterol.level,sigma.squared = 4)
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  33.6967 
## Chi-Squared p.Value:  0.3500844 
## 95% Confidence Interval: ( 9.248833 ,  3.120461 )
##Testing Function at 1
Test.Conf.Int.for.variance(cholesterol.level,sigma.squared = 1)
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  134.7868 
## Chi-Squared p.Value:  5.551474e-16 
## 95% Confidence Interval: ( 9.248833 ,  3.120461 )
##Testing Function at 1
Test.Conf.Int.for.variance(cholesterol.level,sigma.squared = 1,tail.test = "Upper-Tailed Test")
## Sample Variance:  4.992103 
## Chi-Squared Test Statistic:  134.7868 
## Chi-Squared p.Value:  2.775737e-16 
## 95% Confidence Interval: ( Inf ,  3.360154 )