I am trying to run a Winsorized regression in pandas for Python. The very helpful user manual offers this example code:
winz = rets.copy()
std_1year = rolling_std(rets, 250, min_periods=20)
cap_level = 3 * np.sign(winz) * std_1year
winz[np.abs(winz) > 3 * std_1year] = cap_level
winz_model = ols(y=winz['AAPL'], x=winz.ix[:, ['GOOG']],window=250)
The fourth line looks wrong to me: shouldn't the RHS be cap_level[np.abs(winz) > 3 * std_1year]?
Thanks for the help! I'm still new to using the Pandas dataframe and want to make sure I'm understanding right.
Edit: sorry, misunderstood the question!
You're correct that this would be wrong for most types; however pandas.DataFrame has special support for setting values using a Boolean mask; it will select the corresponding values from the RHS with the corresponding time value. Under the hood it's using np.putmask.
You can check this for yourself:
>>> df = pandas.DataFrame(np.linspace(0.0, 1.0, 10).reshape(5, 2))
>>> df[df > 0.5] = -df
>>> df
0 1
0 0.000000 0.111111
1 0.222222 0.333333
2 0.444444 -0.555556
3 -0.666667 -0.777778
4 -0.888889 -1.000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With