RiskQuantLib.Operation package¶
Submodules¶
RiskQuantLib.Operation.operation module¶
- class operation[source]¶
Bases:
objectoperation() is the function class that makes all RiskQuantLib list object functional. Any RiskQuantLib list class should inherit from this class to own certain functions, such as groupBy, execFunc, etc.
Users can define their own functions here to set new attribute function to RiskQuantLib list class.
- add(other, useObj=True)[source]¶
This function uses deep copy. It returns the addition of copies of two RiskQuantLib lists.
- addFromDF(df, code='', name='')[source]¶
This function will add elements from a dataframe. If there is already elements in present list, all elements will be kept, while new elements will be added.
The column name of dataframe should be in English, and this function will try to set it to element attribute if there exists the attribute with the same name. That is, this function will only set value of registered attribute. If not registered in current list class, it will be skipped.
Parameters¶
- dfpd.DataFrame
The dataframe you want to pull data from.
- codestr
The column name that you want to mark as ‘code’ attribute.
- namestr
The column name that you want to mark as ‘name’ attribute.
Returns¶
RiskQuantLib List
- addFromIterable(iterable, code='', name='')[source]¶
This function will add elements from an iterable object. If there is already elements in present list, all elements will be kept, while new elements will be added.
This function will only set value of registered attribute. If not registered in current list class, it will be skipped.
Parameters¶
- iterablelist
The iterable object you want to pull data from.
- codestr
The attribute name of iterable that you want to mark as ‘code’ in RiskQuantLib List Object.
- namestr
The attribute name of iterable that you want to mark as ‘name’ in RiskQuantLib List Object.
Returns¶
RiskQuantLib List
- apply(applyFunction, *args)[source]¶
This function will apply the given function to each element of RiskQuantLib list.
- connect(anotherList, targetAttrNameOnLeft, targetAttrNameOnRight, filterFunction=<function operation.<lambda>>, unsymmetrical=False, filterFunctionOnLeft=<function operation.<lambda>>, filterFunctionOnRight=<function operation.<lambda>>)[source]¶
For each element, find all elements that meet requirements from another RiskQuantLib list object. The elements meeting requirement will be set as an attribute of present list.
After this is down, for each element of another RiskQuantLib, find all elements that meet requirements from present RiskQuantLib list object. The elements meeting requirement will be set as an attribute of another list.
This function actually calls join twice, but it switch the position of two RiskQuantLib list in the second call.
Parameters¶
- anotherListRiskQuantLib list or list
Another list object, holding elements waiting to be selected.
- targetAttrNameOnLeftstr
The attribute name of present list that you want to use to mark collected elements from another list.
- filterFunctionfunction
This function has two parameter, left and right, and must return a bool value. If true is returned, the element will be added to each other. False means not added.
- unsymmetricalbool
If true, different rules will be used when join two list, where filterFunctionOnLeft will be used when present list join another list, and filterFunctionOnRight will be used when another list join present list.
- filterFunctionOnLeftfunction
This function has two parameter, presentList and anotherList, and must return a bool value. If true is returned, the element from another list will be added to present list. False means not added.
- targetAttrNameOnRightstr
The attribute name of another list that you want to use to mark collected elements from present list.
- filterFunctionOnRightfunction
This function has two parameter, presentList and anotherList, and must return a bool value. If true is returned, the element from present list will be added to another list. False means not added.
Returns¶
None
- execFunc(functionName, *args, **kwargs)[source]¶
This function will execute instance function for every element in present RiskQuantLib list object, given the function name. If some elements don’t have the function, a Null function will be used, and the result will skip the execution for that element.
Parameters¶
- functionNamestr
The function that element has, and you want to call.
- fillna(propertyList, value, inplace=False, useObj=True)[source]¶
Fill the nan value or blank string will the given value. If attribute doesn’t exist, nothing will be done. You can fill the nan value by single attribute or a list of attributes.
- filter(filterFunction, useObj=True)[source]¶
Return a RiskQuantLib list object given the fiter function. This is used to choose some elements which meet your requirements.
- fromDF(df, code='', name='')[source]¶
This function will convert a dataframe to RiskQuantLib list object. If there is already elements in present list, all elements will be deleted.
The column name of dataframe should be in English, and this function will try to set it to element attribute if there exists the attribute with the same name. That is, this function will only set value of registered attribute. If not registered in current list class, it will be skipped.
Parameters¶
- dfpd.DataFrame
The dataframe you want to pull data from.
- codestr
The column name that you want to mark as ‘code’ attribute.
- namestr
The column name that you want to mark as ‘name’ attribute.
Returns¶
RiskQuantLib List
- fromIterable(iterable, code='', name='')[source]¶
This function will convert an iterable to RiskQuantLib list object. If there is already elements in present list, all elements will be deleted.
This function will only set value of registered attribute. If not registered in current list class, it will be skipped.
Parameters¶
- iterablelist
The iterable object you want to pull data from.
- codestr
The attribute name of iterable that you want to mark as ‘code’ in RiskQuantLib List Object.
- namestr
The attribute name of iterable that you want to mark as ‘name’ in RiskQuantLib List Object.
Returns¶
RiskQuantLib List
- groupBy(attrName, useObj=True, inplace=True)[source]¶
This function use pandas.DataFrame.groupby as engine. Its behavior is totally the same with pandas.
Parameters¶
- attrNamestr
The attribute that you want to group data by.
- useObjbool
If true, return a RiskQuantLib list object, each element of which is also a RiskQuantLib list object. Each element will be marked by setting the attribute as the common value.
- inplacebool
If true, operation will be done in present RiskQuantLib list object.
- groupByFunc(func, useObj=True, inplace=True)[source]¶
This function use pandas.DataFrame.groupby as engine. Its behavior is totally the same with pandas.
Parameters¶
- funcfunction
Elements will be divided into groups by the return values of this function.
- useObjbool
If true, return a RiskQuantLib list object, each element of which is also a RiskQuantLib list object. Each element will be marked by setting the attribute as the common value.
- inplacebool
If true, operation will be done in present RiskQuantLib list object.
- haveAttr(attrName)[source]¶
Get the elements which have certain attributes, if passed a list, the elements which have all attributes in the list will be returned, otherwise an empty list will be returned.
- isIn(anotherList)[source]¶
Find the elements which are also in the given list. Use these elements to generate a new RiskQuantLib list and return it. anotherList can be any iterable object.
- isNotIn(anotherList)[source]¶
Find the elements which are not in the given list. Use these elements to generate a new RiskQuantLib list and return it. anotherList can be any iterable object.
- join(anotherList, targetAttrName, filterFunction=<function operation.<lambda>>)[source]¶
For each element, find all elements that meet requirements from another RiskQuantLib list object. The elements meeting requirement will be set as an attribute of present list.
This function is very like pandas.DataFrame.merge.
Parameters¶
- anotherListRiskQuantLib list or list
Another list object, holding elements waiting to be selected.
- targetAttrNamestr
The attribute name that you want to use to mark collected elements from another list.
- filterFunctionfunction
This function has two parameter, left and right, and must return a bool value. If true is returned, the element will be added. False means not added.
- link(anotherList, targetAttrNameOnLeft, targetAttrNameOnRight, matchFunctionOnLeft=<function operation.<lambda>>, matchFunctionOnRight=<function operation.<lambda>>)[source]¶
For each element in current rqlList, find all elements that meet requirement matchFunctionOnRight(element_in_another_list) == matchFunctionOnLeft(element_in_this_list) from another RiskQuantLib list object. The elements meeting requirement will be set as an attribute of element_in_this_list.
After this is done, for each element in another rqlList, find all elements that meet requirement matchFunctionOnLeft(element_in_this_list) == matchFunctionOnRight(element_in_another_list) from current RiskQuantLib list object. The elements meeting requirement will be set as an attribute of element_in_another_list.
Parameters¶
- anotherListRiskQuantLib list or list
Another list object, holding elements waiting to be selected.
- targetAttrNameOnLeftstr
The attribute name of present list that you want to use to mark collected elements from another list.
- targetAttrNameOnRightstr
The attribute name of another list that you want to use to mark collected elements from present list.
- matchFunctionOnLeftfunction
This function has and only has one parameter, which stands for element in current list.
- matchFunctionOnRightfunction
This function has and only has one parameter, which stands for element in another list.
- match(anotherList, targetAttrName, matchFunctionOnLeft=<function operation.<lambda>>, matchFunctionOnRight=<function operation.<lambda>>)[source]¶
For each element in current rqlList, find all elements that meet requirement matchFunctionOnRight(element_in_another_list) == matchFunctionOnLeft(element_in_this_list) from another RiskQuantLib list object. The elements meeting requirement will be set as an attribute of element_in_this_list.
This function is very like rqlList.join, the only difference is that this function is vectorized, thus run with faster speed. Of cause, this function has some drawbacks, it can not deal with complicated relation which is described as coupled equation, while rqlList.join can do it.
Parameters¶
- anotherListRiskQuantLib list or list
Another list object, holding elements waiting to be selected.
- targetAttrNamestr
The attribute name that you want to use to mark collected elements from another list.
- matchFunctionOnLeftfunction
This function has and only has one parameter, which stands for element in current list.
- matchFunctionOnRightfunction
This function has and only has one parameter, which stands for element in another list.
- mean(attrName, inplace=False)[source]¶
This function will average the value of each element, given the attribute name. If some elements don’t have the attribute, an numpy.nan will be used, and the result will skip this value when calculating mean.
Parameters¶
- attrNamestr
The attribute name whose value you want to average.
- inplacebool
If true, the ‘attributeMean’ property will be added to present object.
- merge(anotherList, how, on=<function operation.<lambda>>, inplace=False)[source]¶
This function will merge two RiskQuantLib list object. This function should only be used when washing data. After merge, any change to source list will or will not influence merge result, depending on python copy mechanism. This won’t be a problem if data flows by single direction. However, troubles will come when you wish to change the origin to influence the merge result list, after merge action.
In short, do not change the origin list once you merge them, any change should be done by merged result.
If ‘on’ function can not identify the only element to merge, all unique attributes of the elements that meet requirement will be added to present list element. (Unique means this attribute only appears once in the list element, the others don’t have it.) If the attribute appears more than one times, only the first will be added.
It’s like pandas.DataFrame.merge, but not totally the same. In RiskQuantLib, listA.merge(listB) is not the same from listB.merge(listA), even if you specify the same value of parameter ‘how’ and ‘on’.
No matter what value parameter ‘how’ is given, attributes of elements in anotherList that meet requirement of ‘on’ will be added as attributes of elements in present list. The attributes who have the same name in both lists won’t be copied, which means you can’t merge two lists twice. (In the second time you merge them, the first list object has all attributes that anotherList has, so all attributes will be skipped, no attribute will be copied.)
If how == ‘inner’, only the elements in the present list who have relative elements in anotherList will be returned.
If how == ‘outer’, all elements in presentList, plus elements from anotherList that don’t have relative elements in present list will be returned.
If how == ‘left’, all elements in presentList will be returned.
if how == ‘right’, the elements in the present list who have relative elements in anotherList, plus elements from anotherList that don’t have relative elements in present list will be returned.
- paraFunc(functionName, *args, **kwargs)[source]¶
This function will execute instance function for every element in present RiskQuantLib list object by parallel, which means elements will be serialized and sent to different processes.
If some elements don’t have the function which named as you specify, a Null function will be used, and the result will skip the execution for that element.
The call will be delayed until you run currentRiskQuantLibList.paraRun().
Parameters¶
- functionNamestr
The function that element has, and you want to call.
Returns¶
operation
- paraRun(nJobs=-1)[source]¶
This is the trigger of run paralleled function for every element in this RiskQuantLib list. Before you use this function, you should call someList.paraFunc(‘functionName’) for more than one time to tell RiskQuantLib what function you want to parallel.
Returns¶
operation
- reIndex(attrName, indexList, useObj=True, inplace=False)[source]¶
Return a new RiskQuantLib list object whose elements are chosen according to the index, the index of new list element must be in th indexList to be included. If more than one elements meet requirement, only the first one will be kept.
- rolling(windowNumber, useObj=True)[source]¶
For each element, This function will collect the n elements before that element, create a new RiskQuantLib list object to holding it. The new list will be set as an attribute of ‘rolling’, thus the present element can reach to the information of its former elements.
- scale(attrName, targetAttrName='', filterFunction=<function operation.<lambda>>, inplace=False)[source]¶
For each element, find the attribute value and calculate the portion of each element to the sum, given attribute name.
After calling this function, a ‘scaled_attrName’ attribute will be added to each element.
This function is very like pandas.DataFrame.groupby.agg.
Parameters¶
- attrName :str
The attribute which you want to scale.
- targetAttrNamestr
The attribute name that you want to use to mark the portion of element.
- filterFunctionfunction
This function has one parameter, element, and must return a bool value. If true is returned, the element will be include when process scale. False means not included.
- setAll(List)[source]¶
Update all elements by the list of new elements. Old elements will be deleted.
Parameters¶
- Listlist
A collection of RiskQuantLib instrument objects.
Returns¶
None
- sort(propertyList, reverse=False, inplace=False, useObj=True)[source]¶
Sort the present RiskQuantLib list by attributes. You can also sort it by a single attribute.
- std(attrName, inplace=False)[source]¶
This function will calculate the standard deviation of value of each element, given the attribute name. If some elements don’t have the attribute, an numpy.nan will be used, and the result will skip this value.
Parameters¶
- attrNamestr
The attribute name whose value you want to calculate standard deviation.
- inplacebool
If true, the ‘attributeStd’ property will be added to present object.
- sub(other, useObj=True)[source]¶
This function uses deep copy. It returns the subtraction of copies of two RiskQuantLib lists.
- sum(attrName, inplace=False)[source]¶
This function will sum the value of each element, given the attribute name. If some elements don’t have the attribute, an numpy.nan will be used, and the result will skip this value.
Parameters¶
- attrNamestr
The attribute name whose value you want to sum.
- inplacebool
If true, the ‘attributeSum’ property will be added to present object.
- toArray(attrNameList='')[source]¶
This function will return a numpy.ndarray object, given the attribute name whose values are used as array value.
If you don’t pass any attribute name, this function will use all elements themselves in this list to form an array.
Parameters¶
- attrNameListstr or list
The attribute name whose values you want to use as array value.
Returns¶
np.ndarray
- toDF(attrNameList='', attrNameAsIndex='', index=None)[source]¶
This function will return a pandas.DataFrame object, given the attribute name whose values are used as dataframe value, and attribute name whose values are used as dataframe index.
If you don’t pass any attribute name, this function will use all elements themselves in this list to form a dataframe.
Parameters¶
- attrNameListstr or list
The attribute name whose values you want to use as dataframe value.
- attrNameAsIndexstr
The attribute name whose values you want to use as dataframe index.
- indexlist or operation
The index list. If you do not specify the attribute name used as index, you can pass a list as index manually.
Returns¶
pd.DataFrame
- toDict(attrNameAsKey, attrNameAsValue)[source]¶
This function will return a dict, given the attribute name whose values are used as dict key, and attribute name whose values are used as dict value.
Parameters¶
- attrNameAsKeystr
The attribute name whose values you want to use as dict keys.
- attrNameAsValuestr
The attribute name whose values you want to use as dict values.
Returns¶
dict
- toList(attrNameList='')[source]¶
This function will return a list object, given the attribute name whose values are used as list value.
If you don’t pass any attribute name, this function will use all elements themselves in this list to form a new list.
Parameters¶
- attrNameListstr or list
The attribute name whose values you want to use as list value.
Returns¶
list
- toSeries(attrNameAsValue='', attrNameAsIndex='', nameString='', index=None)[source]¶
This function will return a pandas.Series object, given the attribute name whose values are used as series value, and attribute name whose values are used as series index. You can also pass a string to identify the name of series.
If you don’t pass any attribute name, this function will use all elements themselves in this list to form a Series.
Parameters¶
- attrNameAsValuestr
The attribute name whose values you want to use as series value.
- attrNameAsIndexstr
The attribute name whose values you want to use as series index.
- nameStringstr
The name of series.
- indexlist or operation
The index list. If you do not specify the attribute name used as index, you can pass a list as index manually.
Returns¶
pd.Series
- union(anotherList)[source]¶
Find the elements which are in the given list or in the current list. Use these elements to generate a new RiskQuantLib list and return it. anotherList can be any iterable object.
- uniqueAttr(attrNameString, inplace=False, keep='First')[source]¶
This function returns a RiskQuantLib list object, where the attribute value is unique.
Parameters¶
- attrNameString :str
The attribute name that you want to get the unique value list from.
- inplacebool
If true, operation will done in present list object. If false, operation will done in a copy of present list object.
- keepstr
Only support ‘First’ or ‘Last’. If multiple elements have the same attribute value. The first or last element will be kept, given your choice.
Returns¶
RiskQuantLib list object
- uniqueCode(inplace=False, keep='First')[source]¶
This function returns a RiskQuantLib list object, where the code value is unique.
Parameters¶
- inplacebool
If true, operation will done in present list object. If false, operation will done in a copy of present list object.
- keepstr
Only support ‘First’ or ‘Last’. If multiple elements have the same code value. The first or last element will be kept, given your choice.
Returns¶
RiskQuantLib list object
- updateAttr(attrName, codeSeries, valueSeries, byAttr='code')[source]¶
This function will update the attribute value of elements whose code appears in given codeSeries. If code of element doesn’t show in codeSeries, these elements will be skipped and not changed.
If the attribute is not registered, it will be created.
Parameters¶
- attrNamestr
The attribute whose value is exactly what you want to update.
- codeSerieslist
An iterable object that contains codes of elements whose attribute need to be updated.
- valueSerieslist
An iterable object that contains new values of attributes.
- byAttrstr
The attribute that is used to index element in this list. By default, this attribute is code. But you can specify another attribute A, if you do it, this function will update the attribute value of elements whose attribute A appears in given codeSeries.
Returns¶
None
- updateAttrFromArray(array, codeList, attributeNameList, byAttr='code')[source]¶
This function will update attribute value of current list by passed array. You have to identify that each column of array belongs to which attribute, and each row of array represents which element.
The row number of passed array must equal to length of codeList, and the column number of passed array must equal to length of attributeNameList.
If the attribute are not registered, the attribute will be created.
Parameters¶
- arraynp.ndarray
The array you which want to update this list from.
- codeListlist
The code of element whose attribute will be updated by this function. Those whose code is not specified in this list will not be influenced. The length of this list must equal to the row number of passed array.
- attributeNameListstr or list
The attribute that you want to update. Those attribute in this list will be updated, and those are not in this list will not be influenced.
- byAttrstr
The attribute that is used to index element in this list. By default, this attribute is code. But you can specify another attribute A, if you do it, this function will update the attribute value of elements whose attribute A appears in given codeList.
Returns¶
None
- updateAttrFromDF(df, code='', byAttr='code')[source]¶
This function will update attribute value of current list by passed dataframe. The column name of passes dataframe should be in English, and if current list has registered attribute whose name is the same with column name, it will be updated. If the attribute is not registered, it will be created.
Parameters¶
- dfpd.DataFrame
The dataframe that you want to update data from.
- codestr
A column name of df that you used to mark rows. Elements whose code is in this column will be updated, and those not in this column will not be influenced. If blank, the index of df will be used as code.
- byAttrstr
The attribute that is used to index element in this list. By default, this attribute is code. But you can specify another attribute A, if you do it, this function will update the attribute value of elements whose attribute A appears in df[code] or df.index.
Returns¶
None
- updateAttrFromDict(attrName, codeValueDict, byAttr='code')[source]¶
This function will update attribute value of current list by passed dict. The key of passes dict should be code string. If current list has registered attribute whose name is in keys of codeValueDict, it will be updated. If the attribute is not registered, it will be created.
Parameters¶
- attrNamestr
The attribute whose value is exactly what you want to update.
- codeValueDictdict
A dict that maps code to value.
- byAttrstr
The attribute that is used to index element in this list. By default, this attribute is code. But you can specify another attribute A, if you do it, this function will update the attribute value of elements whose attribute A appears in given codeValueDict.
Returns¶
None
- updateAttrFromSeries(sr, attrName='', byAttr='code')[source]¶
This function will update attribute value of current list by passed series. The name of passes series should be in English, and if current list has registered attribute whose name is the same with column name, it will be updated. If the attribute is not registered, it will be created.
Parameters¶
- srpd.Series
The series that you want to update data from.
- attrNamestr
The attribute whose value is exactly what you want to update.
- byAttrstr
The attribute that is used to index element in this list. By default, this attribute is code. But you can specify another attribute A, if you do it, this function will update the attribute value of elements whose attribute A appears in df[code] or df.index.
Returns¶
None
- vecApply(lambdaFunction=<function operation.<lambda>>)[source]¶
This function is like operation.apply, it will apply the given function to each element of RiskQuantLib list. The passed function should be one-parameter function, whose parameter represents the element of current RiskQuantLib list.
The difference is this function will try to vectorize the calculation to speed up. It will convert the attribute to np.array and operate mathematical calculation.
Notice: Only Pure Mathematic Function can be vectorized, this is to say, any logic key word of python, like ‘in’, ‘if’, ‘for’, etc, can Not be used in passed function. If must be pure math formula. Luckily, all numpy function can be used. If you want to do some logistic calculation, you should make sure you write function as logic-matrix-like.
Returns¶
operation
- vecFunc(functionName, *args, **kwargs)[source]¶
This function is like operation.execFunc, it will call the given function of each element of RiskQuantLib list. The passed function should be the attribute function of element of current RiskQuantLib List.
The difference is this function will try to vectorize the calculation to speed up. It will convert the attribute to np.array and operate mathematical calculation.
Notice: Only Pure Mathematic Function can be vectorized, this is to say, any logic key word of python, like ‘in’, ‘if’, ‘for’, etc, can Not be used in passed function. If must be pure math formula. Luckily, all numpy function can be used. If you want to do some logistic calculation, you should make sure you write function as logic-matrix-like.
Returns¶
operation
- zip(attrNameList, *args)[source]¶
This function will convert the values of given attribute into a zip object. If you also pass an external list or other iterable object, it will zip that list with the value of given internal attribute.
Parameters¶
- attrNameListstr or list
The attribute name whose values you want to zip.
- argslist or iterable
External iterable object, whose value is zipped with internal attribute values.
Returns¶
zip object