Advertisement
Nam_Hoang_Waw

Copula & Marginal Distribution Classes

Jan 10th, 2021
1,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 9.59 KB | None | 0 0
  1. library("R6")
  2. library("assertive")
  3.  
  4. Copula_Class <- R6Class("Copula_Class",
  5.                        
  6.                         private = list(
  7.                                 ..Confidence_Level = 0.05,
  8.                                 ..Rotations = FALSE,
  9.                                 ..Method = "mle",
  10.                                 ..Copula_Family = c(1,2,3,4,5),
  11.                                 ..Selection_Criteria = "AIC"
  12.                         ),
  13.                        
  14.                         active = list(
  15.                                
  16.                                 Confidence_Level = function(value){
  17.                                         if(missing(value)){
  18.                                                 private$..Confidence_Level
  19.                                         } else{
  20.                                                 assert_is_numeric(value)
  21.                                                 assert_all_are_in_closed_range(value, lower = 0, upper = 1)
  22.                                                 private$..Confidence_Level <- value
  23.                                         }
  24.                                        
  25.                                 },
  26.                                
  27.                                 Rotations = function(value){
  28.                                         if(missing(value)){
  29.                                                 private$..Rotations
  30.                                         } else{
  31.                                                 assert_is_a_bool(value)
  32.                                                 private$..Rotations <- value
  33.                                         }
  34.                                 },
  35.                                
  36.                                 Method = function(value){
  37.                                         if(missing(value)){
  38.                                                 private$..Method
  39.                                         } else{
  40.                                                 assert_is_a_string(value)
  41.                                                 private$..Method <- value
  42.                                         }
  43.                                        
  44.                                 },
  45.                                
  46.                                 Copula_Family = function(value){
  47.                                         if(missing(value)){
  48.                                                 private$..Copula_Family
  49.                                         } else{
  50.                                                 assert(assert_is_vector(value), assert_is_numeric(value), combine = "or")
  51.                                                 private$..Copula_Family <- value
  52.                                         }
  53.                                 },
  54.                                
  55.                                 Selection_Criteria = function(value){
  56.                                         if(missing(value)){
  57.                                                 private$..Selection_Criteria
  58.                                         } else{
  59.                                                 assert_is_a_string(value)
  60.                                                 private$..Selection_Criteria <- value
  61.                                         }
  62.                                 }
  63.                                
  64.                         ),
  65.                        
  66.                         public = list(
  67.                                
  68.                                 Copula_Calibration = function(index_input, first_series, second_series){
  69.                                         u1 <- pobs(index_input[,first_series])
  70.                                         u2 <- pobs(index_input[,second_series])
  71.                                         return (BiCopSelect(u1,u2,selectioncrit = private$..Selection_Criteria,
  72.                                                                     familyset = private$..Copula_Family, level = private$..Confidence_Level,
  73.                                                                     rotations = private$..Rotations, method =private$..Method))
  74.                                 },
  75.                                
  76.                                 Copula_Generator = function(Cop_family, Cop_par, Cop_par2, no_of_simulation){
  77.                                         Simulated_U_Value = BiCopSim(no_of_simulation, family = Cop_family,
  78.                                                                      par = Cop_par, par2 = Cop_par2)
  79.                                         return (BiCopHinv(Simulated_U_Value[,1], Simulated_U_Value[,2],
  80.                                                                   family = Cop_family, par = Cop_par,
  81.                                                           par2 = Cop_par2)
  82.                                                 )
  83.                                 },
  84.                                
  85.                                 Set_Parameters = function(confidence_interval, rotations, method, copula_family){
  86.                                         private$..Confidence_Level <- confidence_interval
  87.                                         private$..Rotations <- rotations
  88.                                         private$..Method <- method
  89.                                         private$..Copula_Family <- copula_family
  90.                                 },
  91.                                
  92.                                
  93.                                 initialize = function(Confidence_Level, Rotations, Method, Copula_Family){
  94.                                        
  95.                                         if(!missing(Confidence_Level)){
  96.                                                 private$..Confidence_Level <- Confidence_Level
  97.                                         }
  98.                                        
  99.                                         if(!missing(Rotations)){
  100.                                                 private$..Rotations <- Rotations
  101.                                         }
  102.                                        
  103.                                         if(!missing(Method)){
  104.                                                 private$..Method <- Method
  105.                                         }
  106.                                        
  107.                                         if(!missing(Copula_Family)){
  108.                                                 private$..Copula_Family <- Copula_Family
  109.                                         }
  110.                                        
  111.                                 }
  112.                                
  113.                                
  114.                         )
  115.                        
  116. )
  117.  
  118. Marginal_Dist_Class <- R6Class("Marginal_Dist_Class",
  119.                          
  120.                                private = list(
  121.                                   ..distribution_name = "norm",
  122.                                   ..df = 10000
  123.                                  
  124.                           ),
  125.                          
  126.                                 active = list(
  127.                                         distribution_name = function(value){
  128.                                                 if(missing(value)){
  129.                                                   private$..distribution_name
  130.                                                 } else{
  131.                                                   assert_is_a_string(value)
  132.                                                   private$..Confidence_Level <- value
  133.                                                 }
  134.                                         },
  135.                                  
  136.                                         df = function(value){
  137.                                                 if(missing(value)){
  138.                                                         private$..distribution_name
  139.                                                 } else{
  140.                                                         assert_is_integer(value)
  141.                                                         private$..df <- value
  142.                                                 }
  143.                                         }
  144.                          
  145.                                 ),
  146.                          
  147.                                 public = list(
  148.                                         norm_dist_generation = function(series, CDF_value){
  149.                                                 norm_dist_fitting <- fitdist(series, distr = "norm")
  150.                                                 norm_dist_generator <- qnorm(p = CDF_value, mean = norm_dist_fitting$estimate['mean'],
  151.                                                                             sd = norm_dist_fitting$estimate['sd'])
  152.                                                
  153.                                         },
  154.                                
  155.                                         initialize = function(distribution_name, df){
  156.                                                
  157.                                                 if(!missing(distribution_name)){
  158.                                                         private$..distribution_name <- distribution_name
  159.                                                 }
  160.                                                
  161.                                                 if(!missing(df)){
  162.                                                         private$..df<- df
  163.                                                 }
  164.                                         }
  165.                                        
  166.                                         )
  167.                          
  168.                                
  169. )
  170.  
  171.                          
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement