Skip to content

Commit

Permalink
FEAT: Add methods to apply a matrix index and combine it with other i…
Browse files Browse the repository at this point in the history
…ndexes.
  • Loading branch information
genedan committed Jan 20, 2025
1 parent 1ba911f commit cf0bcae
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions faslr/index/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,64 @@ def meta_dict(self) -> dict:
'Description': [self.description]
}

def apply_matrix(
self,
values: list[float]
) -> DataFrame:
"""
Applies the matrix to a list of values, for example trending a list of premiums.
:param values: The values you want adjusted by the matrix.
:type values: list
"""

func = lambda x: np.asarray(x) * np.asarray(values)

res = self.matrix.apply(func)

return res


def compose(
self,
findexes: list[FIndex],
name: str = None,
description: str = None,
) -> FIndex:
"""
Multiplicatively combines several indexes.
:param findexes: The indexes you want to combine with the parent index.
:type findexes: list of FIndexes
:param name: The resulting name of the new index.
:type name: str
:type description: The resulting description of the new index.
"""

# Gather all the changes as a list of lists.
all_changes = [self.changes] + [x.changes for x in findexes]

# Basically get a big list of 1 + change for all the changes.
intermediate_factors = []
for changes in all_changes:
intermediate_factors += [[1 + x for x in changes]]

# Multiply everything together pairwise.
combined_changes: list = np.multiply.reduce(intermediate_factors).tolist()

# Then subtract 1 from each element to get the consolidated index changes.
combined_changes = [x - 1 for x in combined_changes]

# Construct the new index using these changes.
return FIndex(
name=name,
description=description,
origin=self.origin,
changes=combined_changes
)




class FStandardIndexItem(QStandardItem):
def __init__(
Expand Down

0 comments on commit cf0bcae

Please sign in to comment.