I am new to Python and try to modify a pair trading script that I found here:
https://github.com/quantopian/zipline/blob/master/zipline/examples/pairtrade.py
The original script is designed to use only prices. I would like to use returns to fit my models and price for invested quantity but I don't see how do it.
I have tried:
to define a data frame of returns in the main and call it in run
to define a data frame of returns in the main as a global object and use where needed in the 'handle data'
to define a data frame of retuns directly in the handle data
I assume the last option to be the most appropriate but then I have an error with panda 'shift' attribute.
More specifically I try to define 'DataRegression' as follow:
DataRegression = data.copy()
DataRegression[Stock1]=DataRegression[Stock1]/DataRegression[Stock1].shift(1)-1
DataRegression[Stock2]=DataRegression[Stock2]/DataRegression[Stock2].shift(1)-1
DataRegression[Stock3]=DataRegression[Stock3]/DataRegression[Stock3].shift(1)-1
DataRegression = DataRegression.dropna(axis=0)
where 'data' is a data frame which contains prices, stock1, stock2 and stock3 column names defined globally. Those lines in the handle data return the error:
File "A:\Apps\Python\Python.2.7.3.x86\lib\site-packages\zipline-0.5.6-py2.7.egg\zipline\utils\protocol_utils.py", line 85, in __getattr__
return self.__internal[key]
KeyError: 'shift'
Would anyone know why and how to do that correctly?
Many Thanks,
Vincent
This is an interesting idea. The easiest way to do this in zipline is to use the Returns transform which adds a returns field to the event-frame (which is an ndict, not a pandas DataFrame as someone pointed out).
For this you have to add the transform to the initialize method:
self.add_transform(Returns, 'returns', window_length=1)
(make sure to add from zipline.transforms import Returns at the beginning).
Then, inside the batch_transform you can access returns instead of prices:
#batch_transform
def ols_transform(data, sid1, sid2):
"""Computes regression coefficient (slope and intercept)
via Ordinary Least Squares between two SIDs.
"""
p0 = data.returns[sid1]
p1 = sm.add_constant(data.returns[sid2])
slope, intercept = sm.OLS(p0, p1).fit().params
return slope, intercept
Alternatively, you could also create a batch_transform to convert prices to returns like you wanted to do.
#batch_transform
def returns(data):
return data.price / data.price.shift(1) - 1
And then pass that to the OLS transform. Or do this computation inside of the OLS transform itself.
HTH,
Thomas
Related
In a jupyter notebook, I have a function which prepares the input features and targets matrices for a tensorflow model.
Inside this function, I would like to display a correlation matrix with a background gradient to better see the strongly correlated features.
This answer shows how to do that exactly how I want to do it. The problem is that from the inside of a function I cannot get any output, i.e. this:
def display_corr_matrix_custom():
rs = np.random.RandomState(0)
df = pd.DataFrame(rs.rand(10, 10))
corr = df.corr()
corr.style.background_gradient(cmap='coolwarm')
display_corr_matrix_custom()
clearly does not show anything. Normally, I use IPython's display.display() function. In this case, however, I cannot use it since I want to retain my custom background.
Is there another way to display this matrix (if possible, without matplotlib) without returning it?
EDIT: Inside my real function, I also display other stuff (as data description) and I would like to display the correlation matrix at a precise location. Furthermore, my function returns many dataframes, so returning the matrix as proposed by #brentertainer does not directly display the matrix.
You mostly have it. Two changes:
Get the Styler object based from corr.
Display the styler in the function using IPython's display.display()
def display_corr_matrix_custom():
rs = np.random.RandomState(0)
df = pd.DataFrame(rs.rand(10, 10))
corr = df.corr() # corr is a DataFrame
styler = corr.style.background_gradient(cmap='coolwarm') # styler is a Styler
display(styler) # using Jupyter's display() function
display_corr_matrix_custom()
Using the Maya Python API 2.0, I'm trying to make a callback that changes the value of a plug. However, none of the methods I've tried are working.
I've tried using the MPlug.setFloat() method, but this didn't lead to expected results; I found no change in the plug's value. I figured this hadn't worked because I needed to clean the plug after changing its value. So, I then tried getting the plug's data handle using the MPlug.asDataHandle() method, then using the data handle's datablock() method in order to use the data handle and datablock to set the plug's value and clean it. However, I got an error saying "RuntimeError: (kFailure): Unexpected Internal Failure" upon using MDataHandle.datablock().
Now I'm trying the following, which uses the data handle to set the plug's value and clean it:
def setPlugFloatValue(node, plugName, val):
fnSet = OpenMaya.MFnDependencyNode(node)
plug = fnSet.findPlug(plugName,True)
handle = plug.asMDataHandle()
handle.setFloat(val)
handle.setClean()
The above function is intended to find a certain plug in a node, then use its data handle to set its value and clean it. In my program, the callback uses this function to change the translateX, translateY and translateZ plugs of a node's child nodes. The callback runs when the translate value of the node it's applied to changes. In a scene I'm using to test this callback, I apply the callback to a polygon mesh object, with one child which is also a polygon mesh object. So, as I translate the parent object, I expect the translate values of its child to change. But when I select the child object after translating its parent, its translate values haven't changed.
Tried your example and used setFloat() on the plug, which appears to work fine.
import maya.api.OpenMaya as OpenMaya
def setPlugFloatValue(node, plugName, val):
fnSet = OpenMaya.MFnDependencyNode(node)
plug = fnSet.findPlug(plugName,True)
plug.setFloat(val)
def applyToSelectedObjects():
sl_list = OpenMaya.MGlobal.getActiveSelectionList()
iterator = OpenMaya.MItSelectionList(sl_list)
while not iterator.isDone():
obj = iterator.getDependNode()
setPlugFloatValue(obj, "translateX", -2.0)
iterator.next()
applyToSelectedObjects()
Perhaps your issue is something else? You can also try to use setMDistance() instead, but it didn't make any difference in my testing.
distance = OpenMaya.MDistance(val)
plug.setMDistance(distance)
I'm repeatedly trying to get similar data from time series dataframes. For example, the monthly (annualized) standard deviation of the series:
any_df.resample('M').std().mean()*12**(1/2)
It would save typing and would probably limit errors if these methods could be assigned to a variable, so they can be re-used - I guess this would look something like
my_stdev = .resample('M').std().mean()*12**(1/2)
result = any_df.my_stdev()
Is this possible and if so is it sensible?
Thanks in advance!
Why not just make your own function?
def my_stdev(df):
return df.resample('M').std().mean()*12**(1/2)
result = my_stdev(any_df)
I am currently trying to fit some data with python using scipy.optimize.leastsq. The data that I want to fit is of the form:
Mag(H,F,L) = F*sigmap(H) - sigman(H,L)
The Sigmap is a numeric integral which is a function of H and takes quite a while to calculate. I do not wish to include the integral as part of the fitting routine as otherwise the integral will be performed repeatedly and increase the time of the fitting routine significantly. As such I want to look up the values of the integral from elsewhere. The code I have used to implement this is:
integral = np.loadtxt(text file of form: H_Value Integral_Value)
lookupintegral = dict(integral)
sigmap = F*lookupintegral[H]
This is then included within the function which I am fitting to.
When I try to execute the code I generate an error: TypeError: unhashable type: 'numpy.ndarray'
Does anyone have any ideas as to how to implement a fitting routine that looks up data rather than calculating it every time?
That error suggests that the variable H is a numpy array, which can't be used as a dictionary key. Make sure that the variable H is an integer or float value.
I am trying to animate an existing model I have made in Maya by using a Python script. However, I can't figure out how to access it or its polygons in order to animate them in the script. I know how to select objects beforehand but I just want to write things like this
cmds.setKeyframe( objectName, time = startTime, attribute ='rotateY', value = 0 )
where objectName is either my entire model or a specific polygon in the model
If you want to set attribute values inside your setKeyFrame call like you have shown in your code, you will have to set the attribute string appropriately. Egs. To set the y transform of a vertex attribute and keyframe it you'd do:
objectName = 'pSphere1.vtx[297]'
cmds.setKeyFrame(objectName, attribute='pnty', value=0.7)
# Where 'pnty' stands for Point Y, which is the y transform value for vertices.
Another way would be to perform all the transforms before the call to cmds.setKeyFrame() and call it with the controlPoints=True so it catches vertex and control point changes, as #theodox suggested.