-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
168 lines (168 loc) · 4.65 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
library(readxl)
Data=read_excel("Rancangan Data baru.xlsx")
library(readxl)
Data=read_excel("Data.xlsx")
par(mfrow=c(1,1))
plot(Data$HMSP, type="l",
ylim=c(1000,6000), lwd=2, col="blue",
axes=FALSE, ylab="Harga Saham", xlab="Waktu")
labels <- as.numeric(format(as.Date(Data$Date, "%m/%d/%Y"), "%Y"))
where.put <- c(1, which(diff(labels) == 1) + 1)
axis(side=1, at=where.put,
label=labels[where.put], lwd=0.5)
axis(side=2, at=seq(0,6000, by=2000),
label=seq(0,6000, by=2000), lwd=1)
lines(Data$JPFA, type="l", ylim=c(1000,6000),
lwd=2, col="red")
legend("topright", legend=c("JPFA", "HMSP" ), lwd=2,
col=c("red", "blue"), bg="white")
Data=read_excel("Data.xlsx")
par(mfrow=c(1,1))
plot(Data$HMSP, type="l",
ylim=c(1000,6000), lwd=2, col="blue",
axes=FALSE, ylab="Harga Saham", xlab="Waktu")
labels <- as.numeric(format(as.Date(Data$Date, "%m/%d/%Y"), "%Y"))
where.put <- c(1, which(diff(labels) == 1) + 1)
axis(side=1, at=where.put,
label=labels[where.put], lwd=0.5)
axis(side=2, at=seq(0,6000, by=2000),
label=seq(0,6000, by=2000), lwd=1)
lines(Data$JPFA, type="l", ylim=c(1000,6000),
lwd=2, col="red")
legend("topright", legend=c("JPFA", "HMSP" ), lwd=2,
col=c("red", "blue"), bg="white")
####HMSP####
library(readxl)
POT=read_excel("POT.xlsx")
# untuk l_1
l_1 <- function(data2) {
n <- length(data2)
data2 <- sort(data2)
hasil <- 0
for (i in 1:n) {
hasil <- hasil + data2[i]/n
}
return(hasil)
}
## Untuk data2 HMSP
data2 <- POT$HMSP
#l_1
l_1<-l_1(data2)
print(l_1)
#l_2
l_2<-l_2(data2)
print(l_2)
library(readxl)
POT=read_excel("POT.xlsx")
# untuk l_1
l_1 <- function(df) {
n <- length(df)
df <- sort(df)
hasil <- 0
for (i in 1:n) {
hasil <- hasil + df[i]/n
}
return(hasil)
}
# Untuk l_2
l_2 <- function(df) {
n <- length(df)
df <- sort(df) #Mengurutukan df
hasil <- 0
for (i in 1:(n-1)) {
for (j in (i+1):n) {
hasil <- hasil + (df[j] - df[i])
}
}
hasil <- hasil / (2 * choose(n, 2))
return(hasil)
}
# Untuk l_3
l_3 <- function(df) {
n <- length(df)
df <- sort(df) # Mengurutkan df
hasil <- 0
for (i in 1:(n-2)) {
for (j in (i+1):(n-1)) {
for (k in (j+1):n) {
hasil <- hasil + (df[k] - 2 * df[j] + df[i])
}
}
}
hasil <- hasil / (3 * choose(n, 3))
return(hasil)
}
##2. Untuk df JPFA
df=POT$JPFA
#Untuk l_1
l_1=l_1(df)
#Untuk l_2
l_2=l_2(df)
library(readxl)
Cycle=read_excel("Return.xlsx")
######BLOCK MAXIMA---##########
# Fungsi untuk mencari blok maksimum dan menyimpannya dalam dataframe
find_block_maxima <- function(stock_column, stock_name) {
max_values <- numeric(length = length(stock_column) %/% 5)
for (i in 1:(length(stock_column) %/% 5)) {
maximum <- stock_column[(i - 1) * 5 + 1]
for (j in 2:5) {
if (stock_column[(i - 1) * 5 + j] > maximum) {
maximum <- stock_column[(i - 1) * 5 + j]
}
}
max_values[i] <- maximum
}
return(max_values)
}
HMSP_df <- data.frame(HMSP = find_block_maxima(Cycle$HMSP))
#Manual (yang pangkat -xi)
#PDF distribusi GEV
gev_pdf <- function(x, mu, sigma, xi) {
if (xi != 0) {
return ((1/sigma) * (1 - xi * ((x - mu) / sigma))^((1/xi) - 1) * exp(-((1 - xi * ((x - mu) / sigma))^(1/xi))))
} else {
return ((1/sigma) * exp(-((x - mu) / sigma)) * exp(-exp(-((x - mu) / sigma))))
}
}
# Perkiraan nilai x
x <- seq(-6,10, length.out = 1000)
# Parameters
mu <- 0
sigma <- 1
xi_values <- c(-0.5, 0, 0.5)
# Compute the GEV PDF for different xi values
pdfs_gev <- sapply(xi_values, function(xi) sapply(x, gev_pdf, mu = mu, sigma = sigma, xi = xi))
# Set up plot colors
colors <- c("red", "blue", "green")
# Plot the PDF for each xi value
plot(x, pdfs_gev[,1], type="l", col=colors[1], lwd=2, ylim=c(0, max(pdfs_gev)), ylab="Probability Density Function of GEV Distribution", xlab="", main="")
for (i in 2:length(xi_values)) {
lines(x, pdfs_gev[,i], col=colors[i], lwd=2)
}
# Add legend
legend("topright", legend=c(xi<0~",Frechet", xi==0~",Gumbel", xi>0~",Weibull"), col=colors, lty=1,lwd=2)
### Tanpa package (Hosking approach with -xi)
gpd_pdf_modified <- function(x, mu, sigma, xi) {
if (xi != 0) {
return ((1/sigma) * (1 - (xi * (x - mu)) / sigma)^(1/xi - 1))
} else {
return ((1/sigma) * exp(-(x - mu) / sigma))
}
}
# Perkiraan nilai x
x <- seq(2,10, length.out = 1000)
# Parameter
mu <- 0.3
sigma <- 1
xi_values <- c(-0.1, 0, 0.1)
# Compute the modified GPD PDF for different xi values
pdfs_modified <- sapply(xi_values, function(xi) sapply(x, gpd_pdf_modified, mu = mu, sigma = sigma, xi = xi))
# Set up plot colors
colors <- c("red", "blue", "green")
# Plot the modified PDF for each xi value
plot(x, pdfs_modified[,1], type="l", col=colors[1], lwd=2, ylim=c(0, max(pdfs_modified)), ylab="Probability Density Function of GPD", xlab="x")
for (i in 2:length(xi_values)) {
lines(x, pdfs_modified[,i], col=colors[i], lwd=2)
}
legend("topright", legend=c(xi<0~",Pareto", xi==0~",Eksponensial", xi>0~",Beta"), col=colors, lty=1,lwd=2)