Cauchy-distribution / Data analysis_cauchy.R
Data analysis_cauchy.R
Raw

####################### Data Anlaysis for Cauchy distribution ######################

## Example 1
# German stock index data (30 observations)
x <- c(0.0011848, -0.0057591, -0.0051393, -0.0051781, 0.0020043, 0.0017787,
       0.0026787, -0.0066238, -0.0047866, -0.0052497, 0.0004985, 0.0068006,
       0.0016206, 0.0007411, -0.0005060, 0.0020992, -0.0056005, 0.0110844,
       -0.0009192, 0.0019014, -0.0042364, 0.0146814, -0.0002242, 0.0024545,
       -0.0003083, -0.0917876, 0.0149552, 0.0520705, 0.0117482, 0.0087458)

# Example 2
#cryptocurrency- EOS closing values in U.S. dollars (www.cryptodatadownload.com/data)
  file_path <- "C:\\Users\\ADMIN\\Desktop\\Research\\Cauchy Distribution\\Data\\^GDAXI.xlsx"
   library(readxl)
  data <- read_excel(file_path)
  x<-data$rd
    
 ########################### JEL ans AJEL  ##################################### 

  n<-length(x)
  k1= 1/choose(n,3)
  k11=1/choose((n-1),3)
     
    xs=  sort(x)
    s1<-0
    for(i in 1:(n-2)){
      for(j in (i+1):(n-1)){
        for(k in (j+1):(n )){
          s1<-s1+1*(0.5*((xs[i]*xs[j]-1)/xs[j]) < xs[k])
        }
      }
    }
    delta1= k1*s1
    s1<-0
    for(i in 1:(n-2)){
      for(j in (i+1):(n-1)){
        for(k in (j+1):(n )){
          s1<-s1+1*(0.5*((xs[i]*xs[k]-1)/ xs[k]) < xs[j])
        }
      }
    }
    delta2=  k1*s1
    s1<-0
    for(i in 1:(n-2)){
      for(j in (i+1):(n-1)){
        for(k in (j+1):(n )){
          s1<-s1+1*(0.5*((xs[j]*xs[k]-1)/ xs[k]) < xs[i])
        }
      }
    }
    delta3= k1*s1
    t<- ((1/3)*(delta1+delta2+delta3))-0.5
    #~~~~~~~~~~~~~~~~~~~~ 
    v<-rep()  
    tm<-rep()  
    for(m in 1:n){
      x1<-x[-m]
      xs1= sort(x1)
      s11<-0
      for(i in 1:(n-3)){
        for(j in (i+1):(n-2)){
          for(k in (j+1):(n-1)){
            
            s11<-s11+1*(0.5*((xs1[i]*xs1[j]-1)/xs1[j]) < xs1[k])
            
          }
        }
      }
      delta11=k11*s11
      s11<-0
      for(i in 1:(n-3)){
        for(j in (i+1):(n-2)){
          for(k in (j+1):(n-1)){
            s11<-s11+1*(0.5*((xs1[i]*xs1[k]-1)/xs1[k]) <  xs1[j])
            
          }
        }
      }
      delta12=k11*s11
      s11<-0
      for(i in 1:(n-3)){
        for(j in (i+1):(n-2)){
          for(k in (j+1):(n-1)){
            
            s11<-s11+1*(0.5*((xs1[j]*xs1[k]-1)/xs1[k]) <  xs1[i])
            
          }
        }
      }
      delta13=k11*s11
      
      tm[m]<- ((1/3)*(delta11+delta12+delta13))-0.5
      v[m]= (n*t) - (n-1)*tm[m]               ##### Pseduo Value   ######
    }
    t   # test statistics for JEL
    tm   # test statistics for AJEL
   ###################### P-value calculation 
    p_value<- 1-pchisq(t, 1)  # P-value for JEL
    p_value1<- 1-pchisq(tm, 1) # P-value for JEL
    
    ###################### KS test 
    ks_result<- ks.test(x, "pcauchy",location=median(x), scale= IQR(x)/2)
    
    ##################### Graphical analysis #########
    sorted_data <- sort(x)
    n <- length(sorted_data)
    p <- (1:n - 0.5) / n
    theoretical_quantiles <- qcauchy(p)
    qq_data <- data.frame(Theoretical = theoretical_quantiles, Sample = sorted_data)
    
    plot1 <- ggplot(qq_data, aes(x = Theoretical, y = Sample)) +
      geom_point() +
      geom_abline(intercept = 0, slope = 0.001, color = "blue") +
      labs(title = "Q-Q Plot Against Cauchy Distribution",
           x = "Theoretical Quantiles (Cauchy)",
           y = "Sample Quantiles") +
      theme_minimal() +
      coord_cartesian(xlim = c(-20, 20), ylim = c(-0.1, 0.1))
    
    
    # Histogram and density plot 
    plot2 <-ggplot(data = data.frame(x), aes(x = x)) +
      geom_histogram(aes(y = ..density..), bins = 15, fill = "gray", color = "black", alpha = 0.5) +
      geom_density(color = "blue", size = 0.5) +
      labs(title = "Histogram with Fitted Cauchy Density",
           x = "Observations ",
           y = "Frequency") +
      theme_minimal() +
      theme(panel.grid.major = element_blank(),  # Remove major grid lines
            panel.grid.minor = element_blank(),  # Remove minor grid lines
            panel.background = element_blank(),  # Remove panel background
            plot.background = element_blank(),   # Remove plot background
            axis.line = element_line(color = "black"))  # Add axis lines
    
    
    
    grid.arrange(plot1, plot2, ncol = 2)