# sims1253/bayesfam architecture

> Architecture diagram of the GitHub repository sims1253/bayesfam, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sims1253/bayesfam
- Repository: https://github.com/sims1253/bayesfam

## Overview

##### Main Components
a. Core Distribution Implementations
- Probability distributions (Beta, Gamma, Weibull, etc.) in a central component
- Each distribution should have its own sub-component showing the four essential functions:
- Density (d*)
- Random number generation (r*)
- Quantile function (q*)
- Distribution function (p*)

b. BRMS Integration Layer
- Connection to brms package
- Stanvars integration point
- Family constructors

c. Testing Framework
- Unit tests for each distribution
- Helper functions for testing
- Coverage reporting integration

##### Component Relationships
- Use a layered architecture approach with three main layers:
1. Distribution Implementation Layer (bottom)
2. BRMS Integration Layer (middle)
3. User Interface Layer (top)

- [User Interface Layer]
- ┌─────────────────────────┐
- Public API           │
- (Family Constructors)  │
- ┬──────────────┘
- ↓
- [BRMS Integration Layer]
- ┌─────────────────────────┐
- Stan Variables        │
- Parameter Links       │
- Family Definitions    │
- ┬──────────────┘
- ↓
- [Distribution Implementation Layer]
- ┌─────────────────────────┐
- Distribution Functions  │
- ┌─────┐ ┌─────┐ ┌─────┐│
- Beta │ │Gamma│ │Other││
- ┘ └─────┘ └─────┘│
- ┘

##### Important Details
- Stan integration points
- Distribution parameter relationships
- Error handling and validation flows
- Testing coverage paths

##### Implementation Notes
- How the family constructors (like betaprime()) connect to the Stan implementation
- Flow of stanvars from construction to model fitting
- Demonstrate how custom families extend brms' capabilities

##### External Dependencies
- Clear integration points with:
- brms package
- Stan framework
- R's base statistics functions

Keep the focus on the architectural components rather than individual function implementations, but maintain enough detail to show how the custom families are constructed and integrated with brms.

This design reflects the package's primary purpose of extending brms with additional probability distributions while maintaining a consistent interface for Bayesian modeling.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph User_Interface
        API[Public API - Family Constructors]
        Docs[Distribution Documentation]
    end

    subgraph BRMS_Integration
        Stan[Stan Variables & Parameters]
        Family[Family Definitions]
        Helper[Helper Functions]
        ModelVal[Model Validation]
    end

    subgraph Distribution_Implementation
        Beta[Beta Distribution]
        Gamma[Gamma Distribution]
        LogitNormal[Logit Normal]
        LogNormal[Log Normal]
        GenGamma[Generalized Gamma]
        GenNormal[Generalized Normal]
        BetaPrime[Beta Prime]
    end

    subgraph Testing_Framework
        Tests[Unit Tests]
        TestHelpers[Test Helpers]
        Coverage[Coverage Reporting]
    end

    API --> Stan
    API --> Family
    Stan --> Distribution_Implementation
    Family --> Distribution_Implementation
    Helper --> Distribution_Implementation
    ModelVal --> Tests
    Distribution_Implementation --> Tests
    Tests --> TestHelpers
    Tests --> Coverage

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef distribution fill:#ffedcc,stroke:#d68a00
    classDef integration fill:#d9f7be,stroke:#389e0d
    classDef testing fill:#f0f5ff,stroke:#1890ff
    classDef interface fill:#efdbff,stroke:#722ed1

    class Beta,Gamma,LogitNormal,LogNormal,GenGamma,GenNormal,BetaPrime distribution
    class Stan,Family,Helper,ModelVal integration
    class Tests,TestHelpers,Coverage testing
    class API,Docs interface

    click Beta "https://github.com/sims1253/bayesfam/blob/master/R/beta_mean.R"
    click BetaPrime "https://github.com/sims1253/bayesfam/blob/master/R/beta_prime.R"
    click Gamma "https://github.com/sims1253/bayesfam/blob/master/R/gamma_mean.R"
    click GenGamma "https://github.com/sims1253/bayesfam/blob/master/R/generalized_gamma.R"
    click GenNormal "https://github.com/sims1253/bayesfam/blob/master/R/generalized_normal.R"
    click LogitNormal "https://github.com/sims1253/bayesfam/blob/master/R/logitnormal.R"
    click LogNormal "https://github.com/sims1253/bayesfam/blob/master/R/lognormal.R"
    click Helper "https://github.com/sims1253/bayesfam/blob/master/R/misc.R"
    click Tests "https://github.com/sims1253/bayesfam/tree/master/tests/testthat"
    click TestHelpers "https://github.com/sims1253/bayesfam/blob/master/R/test-helper.R"
    click Coverage "https://github.com/sims1253/bayesfam/blob/master/.github/workflows/test-coverage.yaml"
    click Docs "https://github.com/sims1253/bayesfam/tree/master/man"
    click ModelVal "https://github.com/sims1253/bayesfam/blob/master/man/expect_brms_family.Rd"
```
