Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ venv.bak/
docs/.ipynb_checkpoints/*


/tst.py
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/piecewise-regression.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions piecewise_regression/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,18 @@ def calculate_all_standard_errors(self):
Save to the self.estimates dictionary
"""
const_ses = self.get_const_standard_error()
self.estimates["const"]["se"] = const_ses
self.estimates["const"]["standard_error"] = const_ses

beta_ses = self.get_beta_standard_errors()
bp_ses = self.get_bp_standard_errors()
for bp_i in range(self.n_breakpoints):
self.estimates["beta{}".format(bp_i + 1)]["se"] = beta_ses[bp_i]
self.estimates["beta{}".format(bp_i + 1)]["standard_error"] = beta_ses[bp_i]
self.estimates["breakpoint{}".format(
bp_i + 1)]["se"] = bp_ses[bp_i]
bp_i + 1)]["standard_error"] = bp_ses[bp_i]
alpha_ses = self.get_alpha_standard_errors()
for alpha_i in range(self.n_breakpoints + 1):
self.estimates["alpha{}".format(
alpha_i + 1)]["se"] = alpha_ses[alpha_i]
alpha_i + 1)]["standard_error"] = alpha_ses[alpha_i]

def calculate_all_confidence_intervals(self):
"""
Expand All @@ -253,8 +253,8 @@ def calculate_all_confidence_intervals(self):
# to all estimators
for estimator_name, details in self.estimates.items():
confidence_interval = (
details["estimate"] - t_const * details["se"],
details["estimate"] + t_const * details["se"])
details["estimate"] - t_const * details["standard_error"],
details["estimate"] + t_const * details["standard_error"])
details["confidence_interval"] = confidence_interval

def calculate_all_t_stats(self):
Expand All @@ -268,15 +268,15 @@ def calculate_all_t_stats(self):
# H_0 isn't bp=0, it's that bp doesn't exist
if "breakpoint" in estimator_name:
details["t_stat"] = "-"
details["p_t"] = "-"
details["p_t_stat"] = "-"
else:
t_stat = details["estimate"] / details["se"]
t_stat = details["estimate"] / details["standard_error"]
p_t = scipy.stats.t.sf(np.abs(t_stat), dof) * 2
details["t_stat"] = t_stat
if "beta" in estimator_name:
details["p_t"] = "-"
details["p_t_stat"] = "-"
else:
details["p_t"] = p_t
details["p_t_stat"] = p_t

def get_predicted_yy(self):
"""
Expand Down Expand Up @@ -724,14 +724,14 @@ def get_results(self):

if self.best_muggeo:
results["estimates"] = self.best_muggeo.best_fit.estimates
results["bic"] = self.best_muggeo.best_fit.bic
results["rss"] = self.best_muggeo.best_fit.residual_sum_squares
results["bayesian_information_criterion"] = self.best_muggeo.best_fit.bic
results["residual_sum_of_squares"] = self.best_muggeo.best_fit.residual_sum_squares
results["converged"] = True
else:
results["converged"] = False
results["estimates"] = None
results["bic"] = None
results["rss"] = None
results["bayesian_information_criterion"] = None
results["residual_sum_of_squares"] = None
return results

def get_params(self):
Expand Down Expand Up @@ -1115,9 +1115,9 @@ def summary(self):
estimator_row = table_row_template.format(
est_name,
estimates[est_name]["estimate"],
estimates[est_name]["se"],
estimates[est_name]["standard_error"],
estimates[est_name]["t_stat"],
estimates[est_name]["p_t"],
estimates[est_name]["p_t_stat"],
estimates[est_name]["confidence_interval"][0],
estimates[est_name]["confidence_interval"][1])
table_contents += estimator_row
Expand All @@ -1136,8 +1136,8 @@ def summary(self):
for est_name in alpha_names:
estimator_row = table_row_template.format(
est_name, estimates[est_name]["estimate"],
estimates[est_name]["se"],
estimates[est_name]["t_stat"], estimates[est_name]["p_t"],
estimates[est_name]["standard_error"],
estimates[est_name]["t_stat"], estimates[est_name]["p_t_stat"],
estimates[est_name]["confidence_interval"][0],
estimates[est_name]["confidence_interval"][1])
table_contents += estimator_row
Expand Down
10 changes: 6 additions & 4 deletions piecewise_regression/model_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ def summary(self):
for model_summary in self.model_summaries:

if model_summary["converged"]:
print("=" * 200)
print(model_summary)
model_row = table_row_template.format(
model_summary["n_breakpoints"],
model_summary["bic"],
model_summary["bayesian_information_criterion"],
str(model_summary["converged"]),
model_summary["rss"])
model_summary["residual_sum_of_squares"])
else:
model_row = table_row_template.format(
model_summary["n_breakpoints"], "",
Expand Down Expand Up @@ -110,11 +112,11 @@ def no_breakpoint_fit(self, xx, yy):
bic = n * np.log(rss / n) + k * np.log(n)

fit_data = {
"bic": bic,
"bayesian_information_criterion": bic,
"n_breakpoints": 0,
"estimates": {},
"converged": True,
"rss": rss
"residual_sum_of_squares": rss
}

fit_data["estimates"]["const"] = results.params[0]
Expand Down
44 changes: 22 additions & 22 deletions tests/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ def test_against_muggeo_r_package_data_1(self):
self.assertAlmostEqual(
muggeo_bp2, estimates["breakpoint2"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta2_se, estimates["beta2"]["se"], places=1)
muggeo_beta2_se, estimates["beta2"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp2_se, estimates["breakpoint2"]["se"], places=1)
muggeo_bp2_se, estimates["breakpoint2"]["standard_error"], places=1)

self.assertAlmostEqual(
muggeo_c_t, estimates["const"]["t_stat"], delta=1)
Expand Down Expand Up @@ -138,17 +138,17 @@ def test_against_muggeo_r_package_data_1(self):
self.assertAlmostEqual(
muggeo_bp2, estimates["breakpoint2"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta2_se, estimates["beta2"]["se"], places=1)
muggeo_beta2_se, estimates["beta2"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp2_se, estimates["breakpoint2"]["se"], places=1)
muggeo_bp2_se, estimates["breakpoint2"]["standard_error"], places=1)

self.assertAlmostEqual(
muggeo_c_t, estimates["const"]["t_stat"], delta=1)
Expand Down Expand Up @@ -214,13 +214,13 @@ def test_against_muggeo_r_package_data_2(self):
self.assertAlmostEqual(
muggeo_bp1, estimates["breakpoint1"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)

self.assertAlmostEqual(
muggeo_c_t, estimates["const"]["t_stat"], delta=1)
Expand Down Expand Up @@ -289,13 +289,13 @@ def test_against_muggeo_r_package_data_3(self):
self.assertAlmostEqual(
muggeo_bp1, estimates["breakpoint1"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)

self.assertAlmostEqual(
muggeo_c_t, estimates["const"]["t_stat"], delta=1)
Expand Down Expand Up @@ -393,8 +393,8 @@ def test_non_converging(self):

self.assertEqual(results["converged"], False)
self.assertEqual(results["estimates"], None)
self.assertEqual(results["bic"], None)
self.assertEqual(results["rss"], None)
self.assertEqual(results["bayesian_information_criterion"], None)
self.assertEqual(results["residual_sum_of_squares"], None)


class TestPlots(unittest.TestCase):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_model_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import sys

sys.path.insert(1, os.path.join(sys.path[0], '..'))

DATA_SOURCE = "tests/data/data.txt"
Expand All @@ -32,15 +33,15 @@ def test_it_works_with_differnet_options(self):
ms = ModelSelection(xx, yy, n_boot=20)

# For each n_breakpoints, chekc the ModelSelection vs fit results
for n_breakpoints in range(1,10):
for n_breakpoints in range(1, 10):
fit = Fit(xx, yy, n_breakpoints=n_breakpoints, verbose=False, n_boot=20)

fit_converged = fit.get_results()["converged"]
ms_converged = ms.model_summaries[n_breakpoints]["converged"]
print(n_breakpoints, fit_converged, ms_converged)


self.assertEqual(fit_converged, ms_converged)


if __name__ == '__main__':
unittest.main()
20 changes: 10 additions & 10 deletions tests/test_muggeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ def test_against_muggeo_r_package_data_1(self):
self.assertAlmostEqual(
muggeo_bp2, estimates["breakpoint2"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta2_se, estimates["beta2"]["se"], places=1)
muggeo_beta2_se, estimates["beta2"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp2_se, estimates["breakpoint2"]["se"], places=1)
muggeo_bp2_se, estimates["breakpoint2"]["standard_error"], places=1)

print(estimates)

Expand Down Expand Up @@ -163,13 +163,13 @@ def test_against_muggeo_r_package_data_2(self):
self.assertAlmostEqual(
muggeo_bp1, estimates["breakpoint1"]["estimate"], places=1)

self.assertAlmostEqual(muggeo_c_se, estimates["const"]["se"], places=1)
self.assertAlmostEqual(muggeo_c_se, estimates["const"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_alpha_se, estimates["alpha1"]["se"], places=1)
muggeo_alpha_se, estimates["alpha1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_beta1_se, estimates["beta1"]["se"], places=1)
muggeo_beta1_se, estimates["beta1"]["standard_error"], places=1)
self.assertAlmostEqual(
muggeo_bp1_se, estimates["breakpoint1"]["se"], places=1)
muggeo_bp1_se, estimates["breakpoint1"]["standard_error"], places=1)

self.assertAlmostEqual(
muggeo_c_t, estimates["const"]["t_stat"], delta=1)
Expand Down
Loading