Category: R
Nelson-Siegel-Svensson Yield Curve model using R code
Feed: R-bloggers. Author: sang-heon lee. #========================================================## Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee ## https://kiandlee.blogspot.com#——————————————————–## Estimation of Nelson-Siegel-Svensson model#========================================================#graphics.off(); rm(list = ls())library(alabama)#———————————————–# Nelson-Siegel-Svensson function#———————————————–# NSS factor loadingnss_loading – function(la,m) { la1 – la[1]; la2 – la[2] C – cbind( rep(1,length(m)), (1–exp(–la1*m))/(la1*m), (1–exp(–la1*m))/(la1*m)–exp(–la1*m), (1–exp(–la2*m))/(la2*m)–exp(–la2*m)) return(C)}# fitting functionnss_fit – function(para, m) { beta – para[1:4] return(nss_loading(para[5:6],m)%*%beta)}# objective functionnss_objf – function(para, y, m) { return(sqrt(mean((y – nss_fit(para, m))^2)))}# constrOptim.nl constraint function ( > )nss_constf – function(para, y, m) { beta – para[1:4] la1 – para[5]; la2 – para[6] h – rep(NA, 6) # b1 > 0, b1+b2 > 0 h[1] – beta[1] h[2] – beta[1]+beta[2] # 0.15 > la1 > 0.03 h[3] – 0.15–la1 h[4] – la1–0.03 # 0.025 > la2 > 0.0075 h[5] – 0.025–la2 h[6] – la2–0.0075 return(h)}#===========================================================# 1. Read data#===========================================================# Estimated parameters of Nelson-Siegel (Benchmark)str.ns_est– “ beta1 beta2 beta3 lambda rmse 7.949446 0.2933681 -0.08749462 0.11555381 0.07665367 7.052864 -1.5778886 -0.42253575 0.01823114 0.06432032 5.586903 -0.5600670 -1.43976821 0.03357762 0.06423196 6.011739 0.3470154 -1.00018107 0.04565435 0.09314726″m.ns_est – read.table(text = str.ns_est, header=TRUE)str.zero – “ mat 19890630 19950929 19980831 20000929 3 8.171964 5.413123 4.931036 6.180960 6 8.143305 5.509881 4.949141 6.207158 9 8.123782 5.592753 4.962873 6.204367 12 8.111922 5.654558 4.962513 6.176521 24 7.974091 5.782513 4.824109 5.932045 36 7.943757 5.841313 4.868401 5.862433 48 7.959505 5.920732 4.888494 5.825789 60 7.942511 5.967207 4.900205 5.799035 72 8.000704 6.013260 4.980854 5.826354 84 8.010956 6.055806 4.977891 5.847383 96 8.020059 6.151399 5.007556 5.859937 108 8.027901 6.224049 5.054961 5.875451 120 8.037343 6.265079 5.100250 5.912506 144 8.063573 6.364383 5.200412 6.002328 180 8.052892 6.512078 5.347487 6.093959 240 7.971860 6.718515 5.482335 6.091155 300 7.861711 6.789321 5.422557 5.972525 360 7.721083 6.599990 5.246325 5.697709″df – read.table(text = str.zero, header=TRUE)m – df$mat; nmat – length(m)#===========================================================# 2. Parameter estimation#===========================================================ctrl.optim – list(maxit=50000, trace=0, reltol = 1e–10, abstol = 1e–10)ctrl.outer – list(eps = 1e–10, itmax = 50000, method = “Nelder-Mead”, NMinit = TRUE)# output containerm.nss_est – matrix(NA, 4, 7)m.nss_fit – matrix(NA, length(m), 4)# Estimation of Nelson-Siegel-Svenssonfor(i in 1:4) { y – df[,1+i] x_init – c(y[nmat], y[1]–y[nmat], 2*y[6] –y[1]–y[nmat], 2*y[15]–y[1]–y[nmat], 0.0609, 0.01) opt – constrOptim.nl(y = y, m = m, par=x_init, fn=nss_objf, hin=nss_constf, control.optim =ctrl.optim, control.outer=ctrl.outer) m.nss_est[i,] – c(opt$par, opt$value) m.nss_fit[,i] – nss_fit(opt$par, m) colnames(m.nss_est) – c(“beta1”, “beta2”, “beta3”, “beta4”, “lambda1”, “lambda2”,“rmse”)}colnames(m.nss_fit) – paste0(“fitting-“,1:4)#=======================================================# 3. Estimation results#=======================================================# NSS factor loadingmi – 1:360x11(width = 16/2.5, height = 5); matplot(mi, nss_loading(c(0.0609, 0.01),mi), type = “l”, lwd=6, xlab = “Maturity (month)”, ylab = “Factor loading”)legend(“right”, legend=c(“Level”, “Slope”, “Curvature1”, “Curvature2”), pch = c(15,16), border=“white”, box.lty=0, cex=1, col = c(“black”,“#DF536B”, “#61D04F”, “#2297E6”))# Parameter estimatesm.ns_estm.nss_est# data and fitted yieldsround(cbind(df[,–1], m.nss_fit),3) ... Read More
Model-Based Causal Forests for Heterogeneous Treatment Effects
Feed: R-bloggers. Author: Achim Zeileis. [This article was first published on Achim Zeileis, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. A new arXiv paper investigates which building blocks of random forests, especially causal forests and model-based forests, make them work for heterogeneous treatment effect estimation, both in randomized trials and observational studies. Citation Susanne Dandl, Torsten Hothorn, Heidi Seibold, Erik Sverdrup, Stefan Wager, Achim Zeileis (2022). “What Makes Forest-Based Heterogeneous Treatment Effect ... Read More
Slight inconsistency between forcats’ fct_lump_min and fct_lump_prop
Feed: R-bloggers. Author: kjytay. I recently noticed a slight inconsistency between the forcats package’s fct_lump_min and fct_lump_prop functions. (I’m working with v0.5.1, which is the latest version at the time of writing.) These functions lump levels that meet a certain criteria into an “other” level. According to the documentation, fct_lump_min “lumps levels that appear fewer than min times”, and fct_lump_prop “lumps levels that appear fewer than prop * n times”, where n is the length of the factor variable. Let’s try this out in an example: x <- factor(c(rep(1, 6), rep(2, 3), rep(3, 1)))
x
# [1] 1 1 1 1 ... Read More
A Major Contribution to Learning R
Feed: R-bloggers. Author: matloff. [This article was first published on Mad (Data) Scientist, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. Prominent statistician Frank Harrell has come out with a radically new R tutorial, rflow. The name is short for “R workflow,” but I call it “R in a box” –everything one needs for beginning serious usage of R, starting from little or no background. By serious usage I mean real applications in ... Read More
Mapping a picture on a donut or a Hopf torus
Feed: R-bloggers. Author: Stéphane Laurent. Given a number (s geqslant 1), the following map: [ (u, v) mapsto (x, y, z) = frac{Bigl(scosfrac{u}{s}, ssinfrac{u}{s}, sin vBigr)}{sqrt{s^2+1}-cos v} ] is a conformal parameterization of the torus (the donut), where (-spi leqslant u and (pi leqslant v . I found it in this paper by J.M. Sullivan. The number (s) is the ratio of the major radius over the minor radius. The conformality of the map has the following consequence: you can easily map a doubly periodic image on the torus in such a way that it will perfectly fit on the ... Read More
Building a Kaggle Leaderboard with gtExtras
Feed: R-bloggers. Author: R on head spin - the Heads or Tails blog. Building a Kaggle Leaderboard with gtExtras | R-bloggers % select(rank, avatar, author, total, perc) %>% gt() %>% gt_img_rows(avatar, img_source = "local") %>% gt_plt_bar_pct(column = perc, scaled = TRUE, fill = kaggle_blue) %>% gt_highlight_rows(rows = 1, font_weight = "normal", fill = kaggle_gold) %>% gt_highlight_rows(rows = 2, font_weight = "normal", fill = kaggle_silver) %>% gt_highlight_rows(rows = 3, font_weight = "normal", fill = kaggle_bronze) %>% tab_header( title = md("**Kaggle Hidden Gems Competition - Final Results**"), subtitle = html(str_c("A 2022 Notebooks Competition scored by Community Judges ")) ) %>% tab_style( style ... Read More
Developing React Applications in RStudio Workbench
Feed: R-bloggers. Author: The Jumping Rivers Blog. Introduction RStudio Workbench provides a development environment for R, Python, and many other languages. When developing a performant web application you may progress from Shiny towards tools like Plumber. This allows you to continue development of the true application code, modelling, data processing in the language you already know, R, while providing an interface to those processes suitable for embedding in larger web-based applications. As a Shiny developer, one popular front-end library you might already be familiar with is React. React is used in many popular {htmlwidgets} such as {reactable} and anything built ... Read More
Food Crisis Analysis and, Forecasting with Neural Network Autoregression
Feed: R-bloggers. Author: Selcuk Disci. [This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. The war between Russia and Ukraine has affected the global food supply other than many vital things. Primarily cereal crop products have been affected the most because the imports have been provided to the world mainly through Ukraine and Russia. Let’s check the situation we’ve mentioned for G20 countries. library(tidyverse) library(fpp3) library(bbplot) library(plotly) ... Read More
RObservations #34: Using NLP with keras to understand market sentiment with LSTM networks
Feed: R-bloggers. Author: Benjamin Smith. [This article was first published on r – bensstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. Natural Language Processing (NLP) is a powerful tool in the Machine Learning landscape that can (among other things) allow users to classify sentiment and predict text. Many of recent my blogs have been about data manipulation and data engineering, so I decided change things up to look into showing some applications ... Read More
How to Join Multiple Data Frames in R
Feed: R-bloggers. Author: Jim. [This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. The post How to Join Multiple Data Frames in R appeared first on Data Science Tutorials How to Join Multiple Data Frames in R?, you can find it useful to connect many data frames in R. Fortunately, the left join() function from the dplyr package makes this simple to accomplish. Crosstab calculation ... Read More
Recent Comments