{"id":13700132,"url":"https://github.com/dppalomar/covFactorModel","last_synced_at":"2025-05-04T18:34:27.385Z","repository":{"id":118734751,"uuid":"133761621","full_name":"dppalomar/covFactorModel","owner":"dppalomar","description":"Covariance Matrix Estimation via Factor Models","archived":false,"fork":false,"pushed_at":"2019-03-25T05:15:14.000Z","size":26385,"stargazers_count":33,"open_issues_count":0,"forks_count":12,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-12T17:02:41.469Z","etag":null,"topics":["covariance-matrix","factor-model","financial-data","pca"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dppalomar.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-05-17T05:08:45.000Z","updated_at":"2025-04-12T16:03:57.000Z","dependencies_parsed_at":"2023-03-14T09:30:50.273Z","dependency_job_id":null,"html_url":"https://github.com/dppalomar/covFactorModel","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FcovFactorModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FcovFactorModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FcovFactorModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dppalomar%2FcovFactorModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dppalomar","download_url":"https://codeload.github.com/dppalomar/covFactorModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252383001,"owners_count":21739257,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["covariance-matrix","factor-model","financial-data","pca"],"created_at":"2024-08-02T20:00:48.915Z","updated_at":"2025-05-04T18:34:22.340Z","avatar_url":"https://github.com/dppalomar.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":["Financial Instruments and Pricing"],"readme":"---\noutput:\n  html_document:\n    variant: markdown_github\n    keep_md: true\n  md_document:\n    variant: markdown_github\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, echo = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\"\n)\n```\n\n# covFactorModel\nEstimation of covariance matrix via factor models with application\n    to financial data. Factor models decompose the asset returns into an \n    exposure term to some factors and a residual idiosyncratic component. The \n    resulting covariance matrix contains a low-rank term corresponding to the \n    factors and another full-rank term corresponding to the residual component. \n    \nThis package provides a function to separate the data into the factor \n    component and residual component, as well as to estimate the corresponding \n    covariance matrix. Different kind of factor models are considered, namely, \n    macroeconomic factor models and statistical factor models. The estimation\n    of the covariance matrix accepts different kinds of structure on the \n    residual term: diagonal structure (implying that residual component is \n    uncorrelated) and block diagonal structure (allowing correlation within \n    sectors). The package includes a built-in database containing stock symbols\n    and their sectors.\n    \nThe package is based on the book:\n    R. S. Tsay, _Analysis of Financial Time Series._ John Wiley \u0026 Sons, 2005.\n\n\n## Installation\n```{r, eval = FALSE}\n# Installation from CRAN (not available yet)\n#install.packages(\"covFactorModel\")\n\n# Installation from GitHub\n# install.packages(\"devtools\")\ndevtools::install_github(\"dppalomar/covFactorModel\")\n\n# Getting help\nlibrary(covFactorModel)\nhelp(package = \"covFactorModel\")\npackage?covFactorModel\n?factorModel\n?covFactorModel\n?getSectorInfo\n```\n\n\n## Vignette\nFor more detailed information, please check the vignette: [GitHub-html-vignette](https://rawgit.com/dppalomar/covFactorModel/master/vignettes/covFactorModel-vignette.html) and [GitHub-pdf-vignette](https://rawgit.com/dppalomar/covFactorModel/master/vignettes/covFactorModel-vignette.pdf).\n\n\n## Usage of `factorModel()`\nThe function `factorModel()` builds a factor model for the data, i.e., it decomposes the asset returns into a factor component and a residual component. The user can choose different types of factor models, namely, macroeconomic, BARRA, or statistical. We start by generating some synthetic data:\n```{r, message = FALSE, warning = FALSE}\nlibrary(covFactorModel)\nlibrary(xts)\nlibrary(MASS)\n\n# generate synthetic data\nset.seed(234)\nN \u003c- 3  # number of stocks\nT \u003c- 5  # number of samples\nmu \u003c- rep(0, N)\nSigma \u003c- diag(N)/1000\n\n# generate asset returns TxN data matrix\nX \u003c- xts(mvrnorm(T, mu, Sigma), order.by = as.Date('2017-04-15') + 1:T) \ncolnames(X) \u003c- c(\"A\", \"B\", \"C\")\n\n# generate K=2 macroeconomic factors\necon_fact \u003c- xts(mvrnorm(T, c(0, 0), diag(2)/1000), order.by = index(X))\ncolnames(econ_fact) \u003c- c(\"factor1\", \"factor2\")\n```\n\nWe first build a _macroeconomic factor model_, which fits the data to the given macroeconomic factors:\n```{r}\nmacro_econ_model \u003c- factorModel(X, type = \"Macro\", econ_fact = econ_fact)\n\n# sanity check\nX_ \u003c- with(macro_econ_model, \n           matrix(alpha, T, N, byrow = TRUE) + factors %*% t(beta) + residual)\nnorm(X - X_, \"F\")\n```\n\nNext, we build a _BARRA industry factor model_ (assuming assets A and C belong to sector 1 and asset B to sector 2):\n```{r}\nstock_sector_info \u003c- c(1, 2, 1)\nbarra_model \u003c- factorModel(X, type = \"Barra\", stock_sector_info = stock_sector_info)\n\n# sanity check\nX_ \u003c- with(barra_model, \n           matrix(alpha, T, N, byrow = TRUE) + factors %*% t(beta) + residual)\nnorm(X - X_, \"F\")\n```\n\nFinally, we build a _statistical factor model_, which is based on principal component analysis (PCA):\n```{r}\n# set factor dimension as K=2\nstat_model \u003c- factorModel(X, K = 2)\n\n# sanity check\nX_ \u003c- with(stat_model, \n           matrix(alpha, T, N, byrow = TRUE) + factors %*% t(beta) + residual)\nnorm(X - X_, \"F\")\n```\n\n## Usage of `covFactorModel()`\nThe function `covFactorModel()` estimates the covariance matrix of the data based on factor models. The user can choose not only the type of factor model (i.e., macroeconomic, BARRA, or statistical) but also the structure of the residual covariance matrix (i.e., scaled identity, diagonal, block diagonal, and full).\nWe start by preparing some synthetic data:\n```{r}\nlibrary(covFactorModel)\nlibrary(xts)\nlibrary(MASS)\n\n# generate synthetic data\nset.seed(234)\nK \u003c- 1   # number of factors\nN \u003c- 400  # number of stocks\nmu \u003c- rep(0, N)\nbeta \u003c- mvrnorm(N, rep(1, K), diag(K)/10)\nSigma \u003c- beta %*% t(beta) + diag(N)\nprint(eigen(Sigma)$values[1:10])\n```\n\nThen, we simply use function `covFactorModel()` (by default it uses a _statistical factor model_ and a diagonal structure for the residual covariance matrix). We show the average error w.r.t number of observations:\n```{r, warning = FALSE}\n# estimate error by loop\nerr_scm_vs_T \u003c- err_statPCA_diag_vs_T \u003c- c()\nindex_T \u003c- N*seq(5)\nfor (T in index_T) {\n  X \u003c- xts(mvrnorm(T, mu, Sigma), order.by = as.Date('1995-03-15') + 1:T)\n  # use statistical factor model\n  cov_statPCA_diag \u003c- covFactorModel(X, K = K, max_iter = 10)\n  err_statPCA_diag_vs_T \u003c- c(err_statPCA_diag_vs_T, norm(Sigma - cov_statPCA_diag, \"F\")^2)\n  # use sample covariance matrix\n  err_scm_vs_T \u003c- c(err_scm_vs_T, norm(Sigma - cov(X), \"F\")^2)\n}\nres \u003c- rbind(err_scm_vs_T, err_statPCA_diag_vs_T)\nrownames(res) \u003c- c(\"SCM\", \"stat + diag\")\ncolnames(res) \u003c- paste0(\"T/N=\", index_T/N)\nprint(res)\n```\n\n\n## Usage of `getSectorInfo()`\nThe function `getSectorInfo()` provides sector information for a given set of stock symbols. The usage is rather simple:\n```{r}\nlibrary(covFactorModel)\n\nmystocks \u003c- c(\"AAPL\",  \"ABBV\", \"AET\", \"AMD\", \"APD\", \"AA\",\"CF\", \"A\", \"ADI\", \"IBM\")\ngetSectorInfo(mystocks)\n```\n\nThe built-in sector database can be overidden by providing a stock-sector pairing:\n```{r}\nmy_stock_sector_database \u003c- cbind(mystocks, c(rep(\"sector1\", 3),\n                                              rep(\"sector2\", 4),\n                                              rep(\"sector3\", 3)))\ngetSectorInfo(mystocks, my_stock_sector_database)\n```\n\n\n## Links\nPackage: [GitHub](https://github.com/dppalomar/covFactorModel).\n\nREADME file: [GitHub-readme](https://rawgit.com/dppalomar/covFactorModel/master/README.html).\n\nVignette: [GitHub-html-vignette](https://rawgit.com/dppalomar/covFactorModel/master/vignettes/covFactorModel-vignette.html) and [GitHub-pdf-vignette](https://rawgit.com/dppalomar/covFactorModel/master/vignettes/covFactorModel-vignette.pdf).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdppalomar%2FcovFactorModel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdppalomar%2FcovFactorModel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdppalomar%2FcovFactorModel/lists"}