Mark Richardson - 10 months ago 59

Python Question

- I have a Dataframe with
`df`

columns. The index is a DatetimeIndex. Given a reference column`n`

, I wish to compute the`ref_col`

one-dimensional linear regressions of the remaining columns against this reference column. The following does not achieve this, but rather computes a single`n-1`

-dimensional regression:`n-1`

`pd.ols(y=df[ref_col], x=df.drop(ref_col, axis=1))`

- Suppose now I wish to compute all possible pairwise regressions in order to produce an matrix of betas with unit diagonal.
`nxn`

One can do both of the above relatively easily using loops. Is there a "vectorised" way?

Answer Source

You can get the list of the pairwise regressions to the reference column like this:

```
models=[pd.ols(y=df[ref_col],x=df[col]) for col in df if col<>ref_col]
```

To get the matrix of models over all possible reference columns, the next step would be

```
models_matrix=[[pd.ols(y=df[ref_col],x=df[col]) for col in df if col<>ref_col] for ref_col in df]
```

Finally, the matrix of betas can be achieved like this

```
betas=[[pd.ols(y=df[ref_col],x=df[col]).beta.x for col in df if col<>ref_col] for ref_col in df]
```