Advertisement
federico_carosio

Esercizio_4

Dec 27th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.49 KB | None | 0 0
  1. #   ESERCIZIO 4
  2.  
  3. rm(list = ls())
  4. library(moments)
  5. library(evd)
  6. library(goftest)
  7.  
  8. data <- read.table("Esercizio4.txt", sep = '\t', header = T)
  9. df <- data[ ,2:6]
  10. year <- data$Anno
  11.  
  12. #Vettore di durate
  13. duration <- c(1, 3, 6, 12, 24)
  14. par <- matrix(NA, nrow = length(duration), ncol = 2)
  15. colnames(par) <- c("scale", "location")
  16.  
  17. #METODO DEI QUANTILI REGOLARIZZATI ----------------
  18.  
  19. for (i in 1:length(duration)) {
  20.   x <- df[ ,i]
  21.   mu <- mean(x)
  22.   sd <- sqrt(var(x))
  23.   scale <- sqrt(6) * sd / pi
  24.   loc <- mu - 0.5772 * scale
  25.   par[i, 1] <- scale
  26.   par[i, 2] <- loc
  27. }
  28.  
  29. PR <- c(10, 25, 50, 100)
  30. FR <- (1 - 1/PR)
  31. LSPP <- matrix(NA, nrow = length(duration), ncol = length(PR))
  32. for (i in 1:length(duration)) {
  33.   qg <- qgumbel(FR, loc = par[i, 2], scale = par[i, 1])
  34.   LSPP[i, ] <- qg
  35. }
  36.  
  37. plot(duration, LSPP[ ,1], ylim = c(40, 310), xlab = "Duration [h]", ylab = 'Quantile', main = 'Quantili regolarizzati')
  38. points(duration, LSPP[ ,2])
  39. points(duration, LSPP[ ,3])
  40. points(duration, LSPP[ ,4])
  41.  
  42. # per ogni tempo di ritorno fitto le rette nel piano logaritmico e poi ritrasformo i parametri ottenuti
  43. par2 <- matrix(NA, nrow = length(PR), ncol = 2)
  44. colnames(par2) <- c("a", "n")
  45. for (i in 1:length(PR)) {
  46.   y <- log(LSPP[ ,i])
  47.   x <- log(duration)
  48.   fit <- lm(y ~ x)
  49.   a <- exp(fit$coefficients[1])
  50.   n <- fit$coefficients[2]
  51.   par2[i, 1] <- a
  52.   par2[i, 2] <- n
  53. }
  54.  
  55. dd <- 1:24
  56. plot(dd, par2[1, 1] * dd ^ par2[1, 2], ylim = c(40, 310), ylab="depth (mm)", xlab="duration (h)", main="DDF-Regularized quantile method", col = 'red', lwd = 3, type = 'l')
  57. lines(dd, par2[2, 1] * dd ^ par2[2, 2],col = 'blue', lwd = 3)
  58. lines(dd, par2[3, 1] * dd ^ par2[3, 2],col = 'orange', lwd = 3)
  59. lines(dd, par2[4, 1] * dd ^ par2[4, 2],col = 'green', lwd = 3)
  60. legend("topleft", legend=c("Return period=10", "Return period=25", "Return period=50", "Return period=100"),
  61.        col=c("red", "blue", "orange", "green"), lwd=c(1.5, 1.5, 1.5, 1.5), text.width = 0.4, bty = 'n', text.font=3)
  62.  
  63.  
  64.  
  65. # METODO SCALA INVARIANTE -------------
  66.  
  67. mean_duration <- apply(df, 2, mean)
  68. X <- c()
  69.  
  70. for(i in 1: length(duration)){
  71.   X <- c(X, df[,i]/mean_duration[i])
  72. }
  73.  
  74. # APPLICAZIONE L-MOMENTS PER STIMARE PARAMETRI GEV
  75.  
  76. xs <- sort(X)
  77. N <- length(X)
  78. rank <- seq(1, N)
  79. PP <- rank / (N + 1)
  80.  
  81.  
  82. M0 <- 1
  83. xPP <- xs * PP
  84. M1 <- (1/N) * sum(xPP)
  85. xPP2 <- xs * PP^2
  86. M2 <- (1/N) * sum(xPP2)
  87.  
  88. L1 <- M0
  89. L2 <- 2 * M1 - M0
  90. L3 <- 6 * M2 - 6 * M1 + M0
  91.  
  92. c <- 2 * L2 / (L3 + 3 * L2) - log(2) / log(3)
  93. shape <- 7.8590 * c + 2.955 * c ^ 2
  94. scale2 <- shape * L2 / ((1 - 2 ^ (-shape)) * gamma(1 + shape))
  95. loc2 <- L1 - scale2 / shape * (1 - gamma(1 + shape))
  96.  
  97. x2 <- log(duration)
  98. y2 <- log(mean_duration)
  99. fit <- lm(y2 ~ x2)
  100. a2 <- exp(fit$coefficients[1])
  101. n2 <- fit$coefficients[2]
  102.  
  103. LSPP2 <- matrix(NA, nrow=length(PR), ncol=length(duration))
  104. for(i in 1: length(PR)){
  105.   xT <- qgev(FR[i], scale=scale2, loc=loc2, shape=shape)
  106.   LSPP2[i,] <- xT
  107. }
  108.  
  109.  
  110. plot(dd, LSPP2[1, 1] * a2 * dd ^ n2, type="l", ylab="depth (mm)", xlab="duration (h)", main="DDF-scale invariant method",
  111.      col="red", ylim=c(40,310), lwd = 3)
  112. lines(dd, LSPP2[2, 1]* a2 * dd ^ n2, type="l", col="blue", lwd = 3)
  113. lines(dd, LSPP2[3, 1]* a2 * dd ^ n2, type="l", col="orange", lwd = 3)
  114. lines(dd, LSPP2[4, 1]* a2 * dd ^ n2, type="l", col="green", lwd = 3)
  115. legend("topleft", legend=c("Return period=10", "Return period=25", "Return period=50", "Return period=100"),
  116.        col=c("red", "blue", "orange", "green"), lwd=c(1.5, 1.5, 1.5, 1.5), text.font=3, text.width = 0.4, bty = 'n')
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement