{"id":24894770,"url":"https://github.com/watanabe-j/qfratio","last_synced_at":"2026-02-09T10:35:54.012Z","repository":{"id":143163097,"uuid":"537609176","full_name":"watanabe-j/qfratio","owner":"watanabe-j","description":"R package for moments and distributions of ratios of quadratic forms","archived":false,"fork":false,"pushed_at":"2025-12-04T16:23:59.000Z","size":1469,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-08T00:41:40.457Z","etag":null,"topics":["quadratic-forms","r","r-package","rcpp","rcppeigen","zonal-polynomials"],"latest_commit_sha":null,"homepage":"https://CRAN.R-project.org/package=qfratio","language":"C","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/watanabe-j.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-09-16T20:25:07.000Z","updated_at":"2025-12-04T16:24:04.000Z","dependencies_parsed_at":"2023-10-02T12:19:02.600Z","dependency_job_id":"646e4cd4-2e90-4611-910d-025126294c38","html_url":"https://github.com/watanabe-j/qfratio","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/watanabe-j/qfratio","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watanabe-j%2Fqfratio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watanabe-j%2Fqfratio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watanabe-j%2Fqfratio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watanabe-j%2Fqfratio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/watanabe-j","download_url":"https://codeload.github.com/watanabe-j/qfratio/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watanabe-j%2Fqfratio/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29261986,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-09T04:11:57.159Z","status":"ssl_error","status_checked_at":"2026-02-09T04:11:56.117Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["quadratic-forms","r","r-package","rcpp","rcppeigen","zonal-polynomials"],"created_at":"2025-02-01T19:14:29.853Z","updated_at":"2026-02-09T10:35:53.992Z","avatar_url":"https://github.com/watanabe-j.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\nbibliography: vignettes/bibliography.bib\nlink-citations: TRUE\ncsl: vignettes/cran_style.csl\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n```{r setup, include = FALSE}\nrequire(qfratio)\nset.seed(64501)\n```\n\n# qfratio: R Package for Moments and Distributions of Ratios of Quadratic Forms\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/watanabe-j/qfratio/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/watanabe-j/qfratio/actions/workflows/R-CMD-check.yaml)\n\u003c!-- badges: end --\u003e\n\n\nThis package provides functions to evaluate moments of ratios\n(and products) of quadratic forms in normal variables, specifically using\nrecursive algorithms developed by @BaoKan2013 and @HillierEtAl2014.\nGenerating functions for these moments are closely related to the\ntop-order zonal and invariant polynomials of matrix arguments.\nIt also provides some functions to evaluate distribution and density\nfunctions of simple ratios of quadratic forms in normal variables\nusing several methods from @Imhof1961, @Hillier2001,\n@Forchini2002[@Forchini2005], @ButlerPaolella2008, and @BrodaPaolella2009.\n\nThere exist a couple of `Matlab` programs developed by Raymond Kan\n(available from \u003chttps://www-2.rotman.utoronto.ca/~kan/\u003e) for evaluating the\nmoments, but this `R` package is an independent project (not a fork or\ntranslation) and has different functionalities, including evaluation of\nmoments of multiple ratios of a particular form and scaling to avoid numerical\noverflow.\nThis has originally been developed for a biological application,\nspecifically for evaluating average evolvability measures in\nevolutionary quantitative genetics [@Watanabe2023cevo], but can be used\nfor a broader class of statistics.\n\n\n\n## Installation\n\n***WARNING*** Installation size of this package can be very large\n(\u003e100 MB on Linux and macOS; ~3 MB on Windows with a recent version (`\u003e= 4.2`)\nof `Rtools`), as it involves lots of `RcppEigen` functions.\n\n\n### From CRAN (stable version)\n```{r eval = FALSE}\ninstall.packages(\"qfratio\")\n```\n\n\n### From GitHub (development version)\n```{r eval = FALSE}\n## Install devtools first:\n# install.packages(\"devtools\")\n\n## Recommended installation (pandoc required):\ndevtools::install_github(\"watanabe-j/qfratio\", dependencies = TRUE, build_vignettes = TRUE)\n\n## Minimal installation:\n# devtools::install_github(\"watanabe-j/qfratio\")\n```\n\n### Dependencies\n\n    Imports: Rcpp, MASS, stats\n    LinkingTo: Rcpp, RcppEigen\n    Suggests: mvtnorm, CompQuadForm, graphics, testthat (\u003e= 3.0.0),\n              knitr, rmarkdown\n\nIf installing from source, you also need [`pandoc`](https://pandoc.org) for\ncorrectly building the vignette.\nFor `pandoc \u003c 2.11`, `pandoc-citeproc` is required as well.\n(Never mind if you use `RStudio`, which appears to have them bundled.)\n\n\n## Examples\n\nThis package has two major functionalities: evaluating moments and\ndistribution function of ratios of quadratic forms in normal variables.\n\n### Moments\n\nThis functionality concerns evaluation of the following moments:\n$\\mathrm{E} \\left( \\left( \\mathbf{x}^T \\mathbf{A} \\mathbf{x} \\right)^p /\n\\left( \\mathbf{x}^T \\mathbf{B} \\mathbf{x} \\right)^q \\right)$ and\n$\\mathrm{E} \\left( \\left( \\mathbf{x}^T \\mathbf{A} \\mathbf{x} \\right)^p /\n\\left( \\mathbf{x}^T \\mathbf{B} \\mathbf{x} \\right)^q\n\\left( \\mathbf{x}^T \\mathbf{D} \\mathbf{x} \\right)^r \\right)$,\nwhere $\\mathbf{x} \\sim N_n \\left(\\boldsymbol{\\mu}, \\boldsymbol{\\Sigma}\\right)$.\n\nThese quantities are evaluated by `qfrm(A, B, p, q, ...)` and\n`qfmrm(A, B, D, p, q, r, ...)`.\nBecause they are evaluated as partial sums of infinite series\n[@Smith1989; @Smith1993; @BaoKan2013; @HillierEtAl2009; @HillierEtAl2014],\nthe evaluation results come with an error bound (where available), and\na `plot` method is defined for inspecting numerical convergence.\n```{r, examples}\n## Simple matrices\nnv \u003c- 4\nA \u003c- diag(1:nv)\nB \u003c- diag(sqrt(nv:1))\n\n## Expectation of (x^T A x)^2 / (x^T x)^2 where x ~ N(0, I)\nqfrm(A, p = 2)\n\n## Compare with Monte Carlo mean\nmean(rqfr(1000, A = A, p = 2))\n\n## Expectation of (x^T A x)^1/2 / (x^T x)^1/2\n(mom_A0.5 \u003c- qfrm(A, p = 1/2))\n\n## Monte Carlo mean\nmean(rqfr(1000, A = A, p = 1/2))\n\nplot(mom_A0.5)\n\n## Expectation of (x^T x) / (x^T A^-1 x)\n##   = \"average conditional evolvability\"\n(avr_cevoA \u003c- qfrm(diag(nv), solve(A)))\n\nmean(rqfr(1000, A = diag(nv), B = solve(A), p = 1))\nplot(avr_cevoA)\n\n## Expectation of (x^T x)^2 / (x^T A x) (x^T A^-1 x)\n##   = \"average autonomy\"\n(avr_autoA \u003c- qfmrm(diag(nv), A, solve(A), p = 2, q = 1, r = 1))\n\nmean(rqfmr(1000, A = diag(nv), B = A, D = solve(A), p = 2, q = 1, r = 1))\nplot(avr_autoA)\n\n## Expectation of (x^T A B x) / ((x^T A^2 x) (x^T B^2 x))^1/2\n##   = \"average response correlation\"\n## whose Monte Carlo evaluation is called the \"random skewers\" analysis,\n## while this is essentially an analytic solution (with slight truncation error)\n(avr_rcorA \u003c- qfmrm(crossprod(A, B), crossprod(A), crossprod(B),\n                    p = 1, q = 1/2, r = 1/2))\n\nmean(rqfmr(1000, A = crossprod(A, B), B = crossprod(A), D = crossprod(B),\n           p = 1, q = 1/2, r = 1/2))\nplot(avr_rcorA)\n\n\n## More complex (but arbitrary) example\n## Expectation of (x^T A x)^2 / (x^T B x)^3 where x ~ N(mu, Sigma)\nmu \u003c- 1:nv / nv\nSigma \u003c- diag(runif(nv) * 3)\n(mom_A2B3 \u003c- qfrm(A, B, p = 2, q = 3, mu = mu, Sigma = Sigma,\n                  m = 500, use_cpp = TRUE))\nplot(mom_A2B3)\n```\n\n### Distributions\n\nThis functionality concerns evaluation of the (cumulative) distribution\nfunction, probability density, and quantiles of\n$\\left( \\mathbf{x}^T \\mathbf{A} \\mathbf{x} /\n        \\mathbf{x}^T \\mathbf{B} \\mathbf{x} \\right) ^ p$,\nwhere $\\mathbf{x} \\sim N_n \\left(\\boldsymbol{\\mu}, \\boldsymbol{\\Sigma}\\right)$.\n\nThese are implemented in `pqfr(quantile, A, B, p, ...)`,\n`dqfr(quantile, A, B, p, ...)`, and `qqfr(probability, A, B, p, ...)`,\n whose usage mimics that of regular distribution-related functions.\n```{r, examples_distr}\n## Example parameters\nnv \u003c- 4\nA \u003c- diag(1:nv)\nB \u003c- diag(sqrt(nv:1))\nmu \u003c- 1:nv * 0.2\nquantiles \u003c- 0:nv + 0.5\n\n## Distribution function and density of\n## (x^T A x) / (x^T B x) where x ~ N(0, I)\npqfr(quantiles, A, B)\ndqfr(quantiles, A, B)\n\n## 95, 99, and 99.9 percentiles of the same\nqqfr(c(0.05, 0.01, 0.001), A, B, lower.tail = FALSE)\n\n## Comparing profiles\nqseq \u003c- seq.int(1 / sqrt(nv) - 0.2, nv + 0.2, length.out = 100)\n\n## Generate p-value sequences for\n## (x^T A x) / (x^T B x) where x ~ N(0, I) vs\n## (x^T A x) / (x^T B x) where x ~ N(mu, I)\npseq_central \u003c- pqfr(qseq, A, B)\npseq_noncent \u003c- pqfr(qseq, A, B, mu = mu)\n\n## Graphical comparison\nplot(qseq, type = \"n\", xlim = c(1 / sqrt(nv), nv), ylim = c(0, 1),\n     xlab = \"q\", ylab = \"F(q)\")\nlines(qseq, pseq_central, col = \"royalblue4\", lty = 1)\nlines(qseq, pseq_noncent, col = \"tomato\", lty = 2)\nlegend(\"topleft\", legend = c(\"central\", \"noncentral\"),\n       col = c(\"royalblue4\", \"tomato\"), lty = 1:2)\n\n## Generate density sequences for\n## (x^T A x) / (x^T B x) where x ~ N(0, I) vs\n## (x^T A x) / (x^T B x) where x ~ N(mu, I)\ndseq_central \u003c- dqfr(qseq, A, B)\ndseq_noncent \u003c- dqfr(qseq, A, B, mu = mu)\n\n## Graphical comparison\nplot(qseq, type = \"n\", xlim = c(1 / sqrt(nv), nv), ylim = c(0, 0.7),\n     xlab = \"q\", ylab = \"f(q)\")\nlines(qseq, dseq_central, col = \"royalblue4\", lty = 1)\nlines(qseq, dseq_noncent, col = \"tomato\", lty = 2)\nlegend(\"topright\", legend = c(\"central\", \"noncentral\"),\n       col = c(\"royalblue4\", \"tomato\"), lty = 1:2)\n```\n\n\n## Copyright notice\n\nThis package bundles selected `C` codes and part of `config.ac` from the\n[GNU Scientific Library](https://www.gnu.org/software/gsl/),\nwhose copyright belongs to the original authors.\nSee `DESCRIPTION` and individual code files in `src/gsl` for details.\nThe redistribution complies with the GNU General Public License version 3.\n\n\n## References\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatanabe-j%2Fqfratio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwatanabe-j%2Fqfratio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatanabe-j%2Fqfratio/lists"}