# TweedieEstimated


Tweedie family with variance power estimated by profile likelihood.


Usage

``` python
TweedieEstimated(
    p_range=(1.01, 1.99),
    n_grid=20,
)
```


[TweedieEstimated](TweedieEstimated.md#whittaker.TweedieEstimated) behaves exactly like [Tweedie](Tweedie.md#whittaker.Tweedie) (log link, `V(mu) = mu^p`, compound Poisson-Gamma density for `1 < p < 2`) except that the variance power [p](Tweedie.md#whittaker.Tweedie.p) is not fixed at construction time. Instead, [GAM.fit()](GAM.md#whittaker.GAM.fit) performs a profile-likelihood grid search: the model is refit at each candidate [p](Tweedie.md#whittaker.Tweedie.p) in `p_range`, and the value minimizing AIC is retained. This is useful when the appropriate degree of "Poisson-ness" versus "Gamma-ness" in claims, rainfall, or other zero-inflated positive data is not known in advance. Users typically construct this family via the [tw()](tw.md#whittaker.tw) convenience function rather than instantiating [TweedieEstimated](TweedieEstimated.md#whittaker.TweedieEstimated) directly.


## Parameters


`p_range: tuple of float = (1.01, 1.99)`  
`(p_min, p_max)` range to search over. Both endpoints should lie strictly within `(1, 2)` for the compound Poisson-Gamma case (the typical use case for insurance-style data with structural zeros).

`n_grid: int = ``20`  
Number of candidate [p](Tweedie.md#whittaker.Tweedie.p) values in the initial grid search over `p_range`. A finer grid gives a more precise estimate of [p](Tweedie.md#whittaker.Tweedie.p) at the cost of additional model fits.


## Notes

Once fitted, the estimated value of [p](Tweedie.md#whittaker.Tweedie.p) is available via the inherited [p](Tweedie.md#whittaker.Tweedie.p) property, and [p_estimated](TweedieEstimated.md#whittaker.TweedieEstimated.p_estimated) reports whether estimation has completed. The link, variance function, and deviance are identical to those of [Tweedie](Tweedie.md#whittaker.Tweedie):

 g(\mu) = \log(\mu), \qquad V(\mu) = \mu^{p}. 

See [Tweedie](Tweedie.md#whittaker.Tweedie) for the full deviance and log-likelihood formulas.


## Examples

Fit a GAM letting Whittaker choose the Tweedie variance power automatically:


``` python
import numpy as np
import whittaker as wk

rng = np.random.default_rng(0)
n = 300
x = np.linspace(0, 2 * np.pi, n)
mu = np.exp(1.0 + 0.5 * np.sin(x))
y = np.array([rng.gamma(2.0, m / 2.0) if rng.random() > 0.3 else 0.0 for m in mu])

data = {"x": x, "y": y}

model = wk.GAM("y ~ s(x)", family=wk.tw())
model.fit(data, method="REML")
print(model.family)
print(model.summary())
```


    Tweedie(p=1.0100, link='log', estimated=True)
    GAM fit summary
    ============================================================
    Formula:    y ~ s(x)
    Family:     Tweedie(p=1.0100, link='log', estimated=True)
    Inference:  REML
    Observations: 300
    Coefficients: 10

    Parametric coefficients:
      Term                       Estimate    Std.Err    t value    p-value
      ------------------------ ---------- ---------- ---------- ----------
      (Intercept)                  0.6950     0.0587     11.847    < 1e-16

    Approximate significance of smooth terms:
      Term                        EDF Ref.df     Chi.sq    p-value
      ------------------------ ------ ------ ---------- ----------
      s(x)                       3.98      4     33.944   7.65e-07

    Total EDF:  4.98
    Scale est:  1.968344
    Deviance:   580.6941
    Null dev:   656.3635
    Dev. expl:  11.5%
    GCV score:  2.001593
    AIC:        1198.39
    BIC:        1216.84


## Attributes

| Name | Description |
|----|----|
| [p_estimated](#p_estimated) | Whether the variance power [p](Tweedie.md#whittaker.Tweedie.p) has completed profile-likelihood estimation. |

------------------------------------------------------------------------


### p_estimated


Whether the variance power [p](Tweedie.md#whittaker.Tweedie.p) has completed profile-likelihood estimation.


`p_estimated: bool`


`False` immediately after construction, when [p](Tweedie.md#whittaker.Tweedie.p) is only a provisional midpoint of `p_range`. Set to `True` by `_set_p` once [GAM.fit()](GAM.md#whittaker.GAM.fit) has run its grid search over `p_range` and selected the AIC-minimizing value, at which point the inherited [p](Tweedie.md#whittaker.Tweedie.p) property reports the estimated value rather than the placeholder.
