ラベル Set Operator の投稿を表示しています。 すべての投稿を表示
ラベル Set Operator の投稿を表示しています。 すべての投稿を表示

2021年3月14日日曜日

金利 NASDAQ 相関 cor.test() intersect()


choose date both TNX and NDX delta is available.


 d <- intersect(index(diff(TNX[,4])[!is.na(diff(TNX[,4]))]), index(diff(NDX[,4])[!is.na(diff(NDX[,4]))]))

check outputs.


head(as.Date(d))

[1] "2007-01-04" "2007-01-05" "2007-01-09" "2007-01-10" "2007-01-11" "2007-01-12"

Check correlation in 2021.


cor.test(diff(TNX[,4])["2021"][as.Date(d)],diff(NDX[,4])["2021"][as.Date(d)])

Pearson's product-moment correlation
data:  diff(TNX[, 4])["2021"][as.Date(d)] and diff(NDX[, 4])["2021"][as.Date(d)]
t = -2.3358, df = 36, p-value = 0.02519
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.61153962 -0.04874962
sample estimates:
       cor 
-0.3627793 
 警告メッセージ: 
 z + c(-1, 1) * sigma * qnorm((1 + conf.level)/2) で: 
  Recycling array of length 1 in array-vector arithmetic is deprecated.
  Use c() or as.vector() instead.

Check correlation all time.


cor.test(diff(TNX[,4])[as.Date(d)],diff(NDX[,4])[as.Date(d)])

Pearson's product-moment correlation
data:  diff(TNX[, 4])[as.Date(d)] and diff(NDX[, 4])[as.Date(d)]
t = 9.819, df = 2787, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.1467388 0.2184915
sample estimates:
      cor 
0.1828587 
 警告メッセージ: 
 z + c(-1, 1) * sigma * qnorm((1 + conf.level)/2) で: 
  Recycling array of length 1 in array-vector arithmetic is deprecated.
  Use c() or as.vector() instead.


> plot.default(diff(TNX[,4])[as.Date(d)],diff(NDX[,4])[as.Date(d)])
> plot.default(diff(TNX[,4])["2021"][as.Date(d)],diff(NDX[,4])["2021"][as.Date(d)])

2019年2月17日日曜日

day to day comparison 2018 vs. 2019 intersect merge



 use intersect() to calculate AND between index(bp.day["2019"]) and 2019 calendar.

> intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))
 [1] 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918
<skip>
[41] 17939 17940 17941 17942 17943 17944 17945
> length(seq(as.Date("1970-01-01"),as.Date("2019-01-01"),by='days'))
[1] 17898
> length(seq(as.Date("1970-01-01"),as.Date("1970-01-01"),by='days'))
[1] 1

resutls are given as the number of the days since "1970-01-01". therefore sbstract 17896 and 17896-365 will calculate the case for 2019 and 2018 respectively. intersecting between 2019 and 2018 will give back the date which exit both 2018 and 2019 in the daily sequential data.

>intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))
[1]  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
[39] 41 43 44 45 46 47

below will creates the day-to-day comparison graph between 2018 and 2019.


bp.day <- apply.daily(bp.bangkok,mean)
d <- intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))

bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]]
bp.day[seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]

merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18"))


plot(merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18")))





2016年6月8日水曜日

Set Operator - count working days in 2016

Combined previous two samples will bring the list which are weekday bu not holiday.

> seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))][!is.element(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))],holiday2016)]


Then you now know how many days you have to go to the office.

> length(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))][!is.element(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))],holiday2016)])
[1] 245

2016年6月7日火曜日

Set operator - how to create the list of Monday to Friday seq number for 2016.


Create the list of seq number of weekday for 2016. Here 4 represents Monday. 5 Tuesday, 6 Thursday and 0 for Friday.

> seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))]
  [1]   1   4   5   6   7   8  11  12  13  14  15  18  19  20  21  22  25  26  27  28  29  32  33  34  35  36  39  40  41
 [30]  42  43  46  47  48  49  50  53  54  55  56  57  60  61  62  63  64  67  68  69  70  71  74  75  76  77  78  81  82
 [59]  83  84  85  88  89  90  91  92  95  96  97  98  99 102 103 104 105 106 109 110 111 112 113 116 117 118 119 120 123
 [88] 124 125 126 127 130 131 132 133 134 137 138 139 140 141 144 145 146 147 148 151 152 153 154 155 158 159 160 161 162
[117] 165 166 167 168 169 172 173 174 175 176 179 180 181 182 183 186 187 188 189 190 193 194 195 196 197 200 201 202 203
[146] 204 207 208 209 210 211 214 215 216 217 218 221 222 223 224 225 228 229 230 231 232 235 236 237 238 239 242 243 244
[175] 245 246 249 250 251 252 253 256 257 258 259 260 263 264 265 266 267 270 271 272 273 274 277 278 279 280 281 284 285
[204] 286 287 288 291 292 293 294 295 298 299 300 301 302 305 306 307 308 309 312 313 314 315 316 319 320 321 322 323 326
[233] 327 328 329 330 333 334 335 336 337 340 341 342 343 344 347 348 349 350 351 354 355 356 357 358 361 362 363 364 365


2016年6月2日木曜日

Set Operator - how to extract only working day from all Friday of 2016.


When holiday2016 is the list of seq. number to represent national holidays and seq(1,365,7) is for all Friday in Friday
seq(1,365,7)[!is.element(seq(1,365,7),holiday2016)] will generate the list of sequence numbers for every working Friday in the year.

> holiday2016
 [1]   1   2  11  42  81 120 124 125 126 200 224 263 267 284 308 328 358
> seq(1,365,7)
 [1]   1   8  15  22  29  36  43  50  57  64  71  78  85  92  99 106 113 120 127 134 141 148 155 162 169 176 183 190 197
[30] 204 211 218 225 232 239 246 253 260 267 274 281 288 295 302 309 316 323 330 337 344 351 358 365
> seq(1,365,7)[!is.element(seq(1,365,7),holiday2016)]
 [1]   8  15  22  29  36  43  50  57  64  71  78  85  92  99 106 113 127 134 141 148 155 162 169 176 183 190 197 204 211
[30] 218 225 232 239 246 253 260 274 281 288 295 302 309 316 323 330 337 344 351 365