diff --git a/math/Linear_Regression.cpp b/math/Linear_Regression.cpp new file mode 100644 index 0000000..7059590 --- /dev/null +++ b/math/Linear_Regression.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + +/*pilihan pertama buat dalam 1 void function +pilihan kedua adalah buat dalam beberapa void + function di ikuti dengan penggunaaan struct +*/ +// sumber soal: https://mathcyber1997.com/regresi-linear-sederhana/ - > nomor 1 +struct DataStatistic{ + float SumX; + float SumY; + float Xsquared; + float Ysquared; + float multiply; +}; +void Squared(std::vector& independen, std::vector& dependen,DataStatistic &stats){ + // bisa menggunakan pow bawaan STL cmath atau langsung dengan metode manual + //contoh menggunakan pow + float SumSquared = 0; + float XSquared; + for(int i = 0; i <= independen.size() - 1; i++){ + XSquared = pow(independen[i],2); // variabel ,pangkat + SumSquared += XSquared; + } + stats.Xsquared = SumSquared; + std::cout << "Hasil X squared " << stats.Xsquared << std::endl; + //contoh metode manual + float SumYSquared = 0; + float Ysquared; + for(int i = 0; i <= dependen.size() - 1;i++){ + Ysquared = dependen[i] * dependen[i]; + SumYSquared = SumYSquared + Ysquared; // bisa juga dengan += + } + stats.Ysquared = SumYSquared; + std::cout << "Hasil Y squared " << stats.Ysquared << std::endl; + +} +void sum(std::vector& independen, std::vector& dependen,DataStatistic &stats){ + float sumx = 0; + float sumy = 0; + for(int i = 0; i <= independen.size() - 1;i++){ + // karena panjang data sama maka cukup 1 for loop + sumx += independen[i]; + sumy += dependen[i]; + } + stats.SumX = sumx; + stats.SumY = sumy; + std::cout << "SumX: " << stats.SumX << std::endl; + std::cout << "SumY: " << stats.SumY << std::endl; +} +void multiple(std::vector& independen, std::vector& dependen,DataStatistic &stats){ + float multiply; + float SumMultiply = 0; + for(int i = 0; i <= independen.size() - 1; i++){ + multiply = independen[i] * dependen[i]; + SumMultiply += multiply; + } + stats.multiply = SumMultiply; + std::cout << "Hasil Kali: " << stats.multiply << std::endl; +} + + +void LinearRegression(std::vector& independen, std::vector& dependen,DataStatistic &stats){ + double SumSquareXX; + double SumSquareXY; + + + SumSquareXY = stats.multiply - ((stats.SumX * stats.SumY)/ independen.size()); + SumSquareXX = stats.Xsquared - (pow(stats.SumX,2)/independen.size()); + // print untuk memastikan hasil nya benar + std::cout << "Hasil SumSquareXY: " << SumSquareXY << std::endl; + std::cout << "Hasil SumSquareXX: " << SumSquareXX << std::endl; + + + // b0 = intercept + // b1 = gradient atau slope + + double Slope,Intercept; + + Slope = SumSquareXY / SumSquareXX; + + Intercept = (stats.SumY - (Slope * stats.SumX)) / independen.size(); + + std::cout << "Model Regresi Linear = " << Intercept << " + " << Slope << "x "; +} + +int main(){ + DataStatistic stats; + std::vector independen = {1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2}; + std::vector dependen = {8.1,7.8,8.5,9.8,9.5,8.9,8.6,10.2,9.3,9.2,10.5}; + int count,IndependenValue,DependenValue; + + //karena tahap production kita akan memakai data yg sudah ada saja + //anda dapat men uncomment code dibawah dan menghapus data pada vector dependen dan dependen + + + + // std::cout << "Masukkan panjang data: "; + // std::cin >> count; + // for(int i = 0; i <= count - 1; i++){ + // std::cout << "Masukkan data x: "; + // std::cin >> IndependenValue; + // independen.push_back(IndependenValue); + // std::cout << "Masukkan data y: "; + // std::cin >> DependenValue; + // dependen.push_back(DependenValue); + // } + float sum_x = independen.size(); + float sum_y = dependen.size(); + sum(independen,dependen,stats); + Squared(independen,dependen,stats); + multiple(independen,dependen,stats); + LinearRegression(independen,dependen,stats); + std::cin.get(); +} \ No newline at end of file diff --git a/math/z_score.cpp b/math/z_score.cpp index f74f406..e69de29 100644 --- a/math/z_score.cpp +++ b/math/z_score.cpp @@ -1,69 +0,0 @@ -#include -#include -#include - - -bool Verification(std::vector& data,int &x){ - for(int i = 0;i <= data.size() - 1; i++){ - if(x == data[i]){ - return true; - } - } - return false; -} - -void Z_Score(std::vector& data,int &x){ - //mean - int resultMean; - int Sum = 0; - for(int i = 0; i <= data.size() - 1;i++){ - Sum += data[i]; - } - resultMean = Sum / data.size(); - std::cout << "Nilai rata-rata: " << resultMean << std::endl; - - //standart deviation - int denominator = data.size() - 1; - int numerator; - float Standar_Dev; - float sum_numerator = 0; - for(int i = 0; i <= data.size() - 1;i++){ - int numerator = sqrt((data[i] - resultMean) * (data[i] - resultMean)); //sqrt(pow((data[i] - mean))); - sum_numerator += numerator; - } - - Standar_Dev = static_cast(sum_numerator) / denominator; - std::cout << "nilai Standar Deviation: " << Standar_Dev << std::endl; - - - - // Z score - double score; - score = (x - resultMean) / Standar_Dev; - std::cout << "Nilai Z-Score: " << score << std::endl; -} -int main(){ - int value,count,x; - std::vector data; - std::cout << "Masukkan panjang data: "; //masukkan panjang data - std::cin >> count; - - for(int i = 0; i <= count - 1; i++){ // for untuk memasukkan data satu per satu - std::cout << "Masukkan data ke " << i + 1 << ": "; - std::cin >> value; - - data.push_back(value); //masukkan data yang di input di variabel value; - } - std::cout << "Masukkan nilai data(x): "; - std::cin >> x; - - if(Verification(data,x)){ - std::cout << "Nilai yang anda masukkan sudah benar! " << std::endl; - }else{ - std::cout << "data yang anda masukkan salah! \n"; - std::cout << "Masukkan nilai kembali: "; - std::cin >> x; - } - Z_Score(data,x); - -} \ No newline at end of file