## models.ElasticNet


An elastic-net GLM: the lasso (`alpha=1`), ridge (`alpha=0`) and


Usage


``` python
models.ElasticNet()
```


everything between, minimizing glmnet's objective `sum(w * d) / (2 * sum(w)) + lam * sum(pf * ((1 - alpha) / 2 * b**2 + alpha * |b|))` over coefficients [b](pricing.Mbbefd.md#prospicio.pricing.Mbbefd.b) of standardized columns. The design's first all-ones column is the unpenalized intercept; coefficients are reported on the design's scale.


## Parameters


`family: str`  
As [Glm](models.Glm.md#prospicio.models.Glm).

`link: str`  
As [Glm](models.Glm.md#prospicio.models.Glm); the canonical link by default.

`alpha: float = ``1.0`  
Mixing between ridge (0) and the lasso (1).

`lam: float = ``0.0`  
Penalty strength (`lambda` in glmnet).

`standardize: bool = ``True`  
Penalize the coefficients of columns scaled to unit standard deviation.

`penalty_factor: list of float`  
One factor per design column (the intercept's is ignored); 0 leaves a column unpenalized.

`theta: float`  

`power: float`  

`link_power: float`  


## Examples

``` python
>>> from prospicio.models import Design, ElasticNet
>>> x = [float(i) for i in range(6)]
>>> d = Design([[1.0] * 6, x, [1.0, 0.0] * 3], ["(Intercept)", "x1", "x2"])
>>> y = [1.0, 3.1, 4.9, 7.2, 9.0, 10.8]
>>> net = ElasticNet("gaussian", alpha=1.0)
>>> top = net.lambda_max(d, y)
>>> net.with_lam(1.01 * top).fit(d, y).coefficients[1:]
```

\[0.0, 0.0\]


## Attributes

| Name | Description |
|----|----|
| [alpha](#alpha) | Mixing parameter. |
| [lam](#lam) | Penalty strength. |

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


#### alpha


Mixing parameter.


`alpha: float`


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


#### lam


Penalty strength.


`lam: float`


## Methods

| Name | Description |
|----|----|
| [cross_validate()](#cross_validate) | Cross-validates the path, like glmnet's `cv.glmnet`: on each |
| [fit()](#fit) | Fits at [lam](models.ElasticNet.md#prospicio.models.ElasticNet.lam). |
| [lambda_max()](#lambda_max) | The smallest [lam](models.ElasticNet.md#prospicio.models.ElasticNet.lam) at which every penalized coefficient is zero. |
| [lambda_path()](#lambda_path) | [n](distributions.Binomial.md#prospicio.distributions.Binomial.n) penalty strengths, log-spaced from [lambda_max](models.ElasticNet.md#prospicio.models.ElasticNet.lambda_max) down to |
| [path()](#path) | Fits at each of [lams](models.CvPath.md#prospicio.models.CvPath.lams) in turn, each from the previous solution. |
| [with_lam()](#with_lam) | The same spec at another penalty strength. |

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


#### cross_validate()


Cross-validates the path, like glmnet's `cv.glmnet`: on each


Usage


``` python
cross_validate(design, y, lams, splits)
```


split, fits [lams](models.CvPath.md#prospicio.models.CvPath.lams) (warm starts) to the training rows and scores the mean deviance on the test rows. Folds run in parallel.


##### Parameters


`design: Design`  

`y: list of float`  

`lams: list of float`  
Penalty strengths, largest first (from [lambda_path](models.ElasticNet.md#prospicio.models.ElasticNet.lambda_path)).

`splits: list of (list of int, list of int)`  
`(train, test)` rows, as [k_fold](models.k_fold.md#prospicio.models.k_fold) returns.


##### Returns


`CvPath`  


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


#### fit()


Fits at [lam](models.ElasticNet.md#prospicio.models.ElasticNet.lam).


Usage


``` python
fit(design, y)
```


##### Parameters


`design: Design`  

`y: list of float`  


##### Returns


`ElasticNetFit`  


##### Raises


`ValueError`  
If a parameter is out of range, a response is outside the family's range, or the fit does not converge.


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


#### lambda_max()


The smallest [lam](models.ElasticNet.md#prospicio.models.ElasticNet.lam) at which every penalized coefficient is zero.


Usage


``` python
lambda_max(design, y)
```


##### Parameters


`design: Design`  

`y: list of float`  


##### Returns


`float`  


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


#### lambda_path()


[n](distributions.Binomial.md#prospicio.distributions.Binomial.n) penalty strengths, log-spaced from [lambda_max](models.ElasticNet.md#prospicio.models.ElasticNet.lambda_max) down to


Usage


``` python
lambda_path(design, y, n=100, min_ratio=0.0001)
```


`min_ratio` times it.


##### Parameters


`design: Design`  

`y: list of float`  

`n: int = ``100`  

`min_ratio: float = ``1e-4`  


##### Returns


`list of float`  


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


#### path()


Fits at each of [lams](models.CvPath.md#prospicio.models.CvPath.lams) in turn, each from the previous solution.


Usage


``` python
path(design, y, lams)
```


##### Parameters


`design: Design`  

`y: list of float`  

`lams: list of float`  


##### Returns


`list of ElasticNetFit`  


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


#### with_lam()


The same spec at another penalty strength.


Usage


``` python
with_lam(lam)
```


##### Parameters


`lam: float`  


##### Returns


`ElasticNet`
