Skip to content

Instantly share code, notes, and snippets.

@zindmax
Created January 9, 2022 06:11
Show Gist options
  • Save zindmax/473d2bc26f42ef3660e228357224d723 to your computer and use it in GitHub Desktop.
Save zindmax/473d2bc26f42ef3660e228357224d723 to your computer and use it in GitHub Desktop.
lab7
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double calcHi(vector<double> P_i) {
double h_i = 0;
for (double i : P_i) {
if (i == 0) {
continue;
}
h_i += i * log2(i);
}
return -h_i;
}
double calcEntrophy(vector<double> prob_vect, vector<vector<double>> P) {
double h = 0.0;
double h_i = 0.0;
for (int i = 0; i < prob_vect.size(); i++) {
h_i = calcHi(P[i]);
h += prob_vect[i] * h_i;
}
return h;
}
int main()
{
vector<vector<double>> P = {\
{0.0, 0.5, 0.1, 0.0, 0.4},
{0.0, 0.0, 0.0, 1, 0.0},
{0.5, 0.0, 0.0, 0.0, 0.5},
{0.6, 0.0, 0.0, 0.0, 0.4},
{0.2, 0.0, 0.0, 0.0, 0.8}
};
vector<double> prob_vect = {20.0/107, 10.0/107, 2.0/107, 10.0/107, 65.0/107};
double H = calcEntrophy(prob_vect, P);
cout << H << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double calcHi(vector<double> P_i) {
double h_i = 0;
for (double i : P_i) {
if (i == 0) {
continue;
}
h_i += i * log2(i);
}
return -h_i;
}
double calcEntrophy(vector<double> prob_vect, vector<vector<double>> P) {
double h = 0.0;
double h_i = 0.0;
for (int i = 0; i < prob_vect.size(); i++) {
h_i = calcHi(P[i]);
h += prob_vect[i] * h_i;
}
return h;
}
int main()
{
vector<vector<double>> P = {\
{0.125, 0.125, 0.25, 0.5},
{0.5, 0.5, 0.0, 0.0},
{0.25, 0.25, 0.25, 0.25},
{0.25, 0.0, 0.25, 0.5},
};
vector<double> prob_vect = {20.0/77, 13.0/77, 16.0/77, 28.0/77};
double H = calcEntrophy(prob_vect, P);
cout << H << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double calcHi(vector<double> P_i) {
double h_i = 0;
for (double i : P_i) {
if (i == 0) {
continue;
}
h_i += i * log2(i);
}
return -h_i;
}
double calcEntrophy(vector<double> prob_vect, vector<vector<double>> P) {
double h = 0.0;
double h_i = 0.0;
for (int i = 0; i < prob_vect.size(); i++) {
h_i = calcHi(P[i]);
h += prob_vect[i] * h_i;
}
return h;
}
int main()
{
vector<vector<double>> P = {\
{0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5},
{0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0},
{0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5}
};
vector<double> prob_vect = {\
10.0/80, 5.0/80, 8.0/80, 12.0/80, 10.0/80, 11.0/80, 12.0/80, 12.0/80};
double H = calcEntrophy(prob_vect, P);
cout << H << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment