Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: menambahkan fungsi gaussian #49

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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
18 changes: 18 additions & 0 deletions OpenSeries/matematika.py
Original file line number Diff line number Diff line change
@@ -504,3 +504,21 @@ def distribusi_binomial(
koefisien = float(math.factorial(percobaan))
koefisien /= math.factorial(keberhasilan) * math.factorial(percobaan - keberhasilan)
return probabilitas_kejadian * koefisien


def gaussian(
x: int, mu: Union[float, int] = 0.0, sigma: Union[float, int] = 1.0
) -> Union[float, error.ErrorTipeData]:
"""
fungsi gaussian, yang biasa disebut fungsi kurva lonceng adalah fungsi
untuk mendeskripsikan probabilitas distribusi data yang normal
"""
if not all(
isinstance(data, (float, int)) for data in [sigma, mu]
) and not isinstance(x, int):
return error.ErrorTipeData(["float", "int"])
return (
1
/ np.sqrt(2 * constant.PI * sigma**2)
* np.exp(-((x - mu) ** 2) / (2 * sigma**2))
)
7 changes: 7 additions & 0 deletions example/matematika/fungsi_gaussian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import OpenSeries.matematika as matematika
import numpy as np

print(matematika.gaussian(15))

nilai_x = np.arange(15)
print(matematika.gaussian(nilai_x))
3 changes: 3 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

from testing.matematika_test import (
TestKonversi,
TestKelilingLingkaran,
@@ -14,6 +15,7 @@
TestFungsiEuler,
TestSigmoid,
TestDistribusiBinomial,
TestGaussian,
)

from testing.fisika_test import (
@@ -46,6 +48,7 @@
TestFungsiEuler,
TestSigmoid,
TestDistribusiBinomial,
TestGaussian,
]

testing_fisika: list = [
10 changes: 10 additions & 0 deletions testing/matematika_test.py
Original file line number Diff line number Diff line change
@@ -242,3 +242,13 @@ def test_input_angka_tidak_integer(self):
hasil = matematika.distribusi_binomial(2.4, 4.2, 0.5)
with self.assertRaises(error.ErrorTipeData):
raise hasil


class TestGaussian(unittest.TestCase):
def test_data_valid_input(self):
x = 5
mu = 3
sigma = 2
ekspetasi_nilai = 0.12098536225957168
hasil = matematika.gaussian(x, mu, sigma)
self.assertAlmostEqual(hasil, ekspetasi_nilai, places=10)