-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update to version 0.0.8 info in changelog
- Loading branch information
Showing
12 changed files
with
329 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ __pycache__/ | |
*.so | ||
|
||
# Distribution / packaging | ||
test.py | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## [0.0.8] - 2023-09-26 | ||
### Added | ||
- **Vector Mean Calculation**: Added a new method to calculate the mean (average) of the vector's values. You can now use the `mean` method to obtain the mean value of a Vector. | ||
|
||
- **Vector Variance Calculation**: Implemented a method to compute the variance of the vector's values. You can now use the `variance` method to calculate the variance of a Vector. | ||
|
||
- **Vector Standard Deviation Calculation**: Introduced a method for calculating the standard deviation of the vector's values. You can now use the `std_deviation` method to find the standard deviation of a Vector. | ||
|
||
- **Linear Regression**: Added support for linear regression with the `LinearRegression` class. You can use the `fit` method to train a linear regression model and the `predict` method to make predictions based on the trained model. | ||
|
||
- **Time Series Analysis**: Introduced a new class, `TimeSeriesAnalysis`, for analyzing time series data. | ||
- `moving_average(self, window_size: int) -> Vector`: Added a method for calculating the moving average of a time series with a specified window size. | ||
- `exponential_smoothing(self, alpha: float) -> Vector`: Added a method for applying exponential smoothing to a time series with a given smoothing parameter. | ||
|
||
These new features provide additional statistical analysis capabilities for Vector objects, as well as the ability to perform linear regression on your data using the `LinearRegression` class. | ||
These new features enable users to perform time series analysis, including moving average and exponential smoothing, using the `TimeSeriesAnalysis` class. This enhances the capabilities of the library for working with time series data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
from .MathFunc import * | ||
|
||
from .Linear import * | ||
from .timeSeriesAnalys import * | ||
from .fin import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .linreg import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from ..MathFunc import Matrix, Vector | ||
import sys | ||
import io | ||
class LinearRegression: | ||
""" | ||
Linear Regression Model. | ||
This class implements a simple linear regression model using the method of least squares. | ||
Attributes: | ||
coefficients (Vector): The coefficients of the linear regression model, including the intercept. | ||
Methods: | ||
- __init__(self): Initialize an instance of the LinearRegression class. | ||
- fit(self, X: Matrix, y: Matrix): Fit the linear regression model to the training data. | ||
- predict(self, X: Matrix): Predict target values for new data. | ||
Example: | ||
# Create a LinearRegression instance | ||
model = LinearRegression() | ||
# Fit the model to training data | ||
X_train = Matrix([[1], [2], [3], [4]]) | ||
y_train = Matrix([2, 4, 6, 8]) | ||
model.fit(X_train, y_train) | ||
# Make predictions | ||
X_test = Matrix([[5], [6]]) | ||
predictions = model.predict(X_test) | ||
""" | ||
def __init__(self): | ||
""" | ||
Initialize an instance of the LinearRegression class. | ||
The coefficients attribute is set to None initially. | ||
""" | ||
self.coefficients = None | ||
|
||
def fit(self, X:Matrix, y:Matrix): | ||
""" | ||
Fit the linear regression model to the training data. | ||
Args: | ||
X (Matrix): The feature matrix. Each row represents an observation. | ||
y (Matrix): The target values. | ||
Raises: | ||
ValueError: If the lengths of X and y do not match. | ||
Returns: | ||
None | ||
""" | ||
if not isinstance(X, Matrix) or not isinstance(y, Matrix): | ||
X = Matrix(X) | ||
y = Matrix(y) | ||
original_stdout = sys.stdout | ||
sys.stdout = io.StringIO() | ||
if len(X) != len(y): | ||
raise ValueError("len(X) must be equal len(y)") | ||
|
||
X_reg = Matrix(X.shape[0],X.shape[1]+1) | ||
for i in range(len(X)): | ||
n_r = Vector(X.shape[1]+1) | ||
n_r[0], n_r[1:] = 1, X[i] | ||
X_reg[i] = n_r | ||
X_transpose = X_reg.transpose() | ||
XTX = X_transpose*X_reg | ||
XTY = X_transpose*y | ||
self.coefficients = XTX.LU_solver(XTY) | ||
sys.stdout = original_stdout | ||
|
||
def predict(self, X): | ||
""" | ||
Predict target values for new data. | ||
Args: | ||
X (Matrix): The feature matrix for prediction. Each row represents an observation. | ||
Raises: | ||
ValueError: If the model has not been trained (fit) yet. | ||
Returns: | ||
Vector: Predicted target values. | ||
""" | ||
if self.coefficients is None: | ||
raise ValueError("Use fit for model teach.") | ||
if not isinstance(X, Matrix): | ||
X = Matrix(X) | ||
X_reg = Matrix(X.shape[0],X.shape[1]+1) | ||
for i in range(len(X)): | ||
n_r = Vector(X.shape[1]+1) | ||
n_r[0], n_r[1:] = 1, X[i] | ||
X_reg[i] = n_r | ||
return Vector([Vector(x) * Vector([el[0] for el in self.coefficients.matrix]) for x in X_reg]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .finance import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from ..MathFunc import Vector | ||
|
||
class FinancialAnalysis: | ||
""" | ||
Financial Analysis Class. | ||
This class provides methods for financial analysis, including NPV and IRR calculations. | ||
Methods: | ||
- __init__(self): Initialize an instance of the FinancialAnalysis class. | ||
- npv(self, cash_flows: Vector), discount_rate: float) -> float: Calculate the Net Present Value (NPV) of cash flows. | ||
- irr(self, cash_flows: Vector) -> float: Calculate the Internal Rate of Return (IRR) of cash flows. | ||
Example: | ||
# Create a FinancialAnalysis instance | ||
analyzer = FinancialAnalysis() | ||
# Calculate NPV for cash flows with a discount rate of 0.1 | ||
cash_flows = [-100, 50, 60, 70] | ||
discount_rate = 0.1 | ||
npv_result = analyzer.npv(cash_flows, discount_rate) | ||
# Calculate IRR for cash flows | ||
irr_result = analyzer.irr(cash_flows) | ||
""" | ||
|
||
def __init__(self): | ||
""" | ||
Initialize an instance of the FinancialAnalysis class. | ||
""" | ||
pass | ||
|
||
def npv(self, cash_flows, discount_rate: float) -> float: | ||
""" | ||
Calculate the Net Present Value (NPV) of cash flows. | ||
Args: | ||
cash_flows (List[float]): List of cash flows over time. | ||
discount_rate (float): The discount rate. | ||
Returns: | ||
float: The NPV value. | ||
""" | ||
if not isinstance(cash_flows, Vector): | ||
cash_flows = Vector(cash_flows) | ||
npv_value = 0 | ||
for i, cash_flow in enumerate(cash_flows): | ||
npv_value += cash_flow / (1 + discount_rate) ** i | ||
return npv_value | ||
|
||
def irr(self, cash_flows, max_iterations:int=1000, irr_guess:float = 0.1, tolerance:float=1e-6) -> float: | ||
""" | ||
Calculate the Internal Rate of Return (IRR) of cash flows. | ||
Args: | ||
cash_flows (Vector): List of cash flows over time. | ||
max_iterations (int, optional): Maximum number of iterations for IRR calculation. Defaults to 1000. | ||
irr_guess (float, optional): Initial guess for IRR. Defaults to 0.1. | ||
tolerance (float, optional): Tolerance level for convergence. Defaults to 1e-6. | ||
Raises: | ||
ValueError: Raised if the IRR calculation does not converge within the specified parameters. | ||
Returns: | ||
float: The calculated IRR value. | ||
""" | ||
if not isinstance(cash_flows, Vector): | ||
cash_flows = Vector(cash_flows) | ||
for _ in range(max_iterations): | ||
npv = self.npv(cash_flows, irr_guess) | ||
npv_derivative = 0 | ||
for t, cash_flow in enumerate(cash_flows): | ||
npv_derivative -= t * cash_flow / ((1 + irr_guess) ** (t + 1)) | ||
|
||
# Newton-Raphson step | ||
irr_guess -= npv / npv_derivative | ||
|
||
# Check for convergence | ||
if abs(npv) < tolerance: | ||
return irr_guess | ||
|
||
# If no convergence is reached, return None or raise an exception | ||
raise ValueError("IRR calculation did not converge") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .tsy import * |
Oops, something went wrong.