Skip to content

Commit

Permalink
fixed probabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ctab-0120 committed Jun 25, 2024
1 parent f8af1d8 commit 6a39e0b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
8 changes: 4 additions & 4 deletions build/default/Testing/20240619-1525/Test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
ProcessorClockFrequency="2600"
>
<Testing>
<StartDateTime>Jun 19 10:25 CDT</StartDateTime>
<StartTestTime>1718810754</StartTestTime>
<StartDateTime>Jun 19 11:23 CDT</StartDateTime>
<StartTestTime>1718814217</StartTestTime>
<TestList/>
<EndDateTime>Jun 19 10:25 CDT</EndDateTime>
<EndTestTime>1718810754</EndTestTime>
<EndDateTime>Jun 19 11:23 CDT</EndDateTime>
<EndTestTime>1718814217</EndTestTime>
<ElapsedMinutes>0</ElapsedMinutes>
</Testing>
</Site>
34 changes: 34 additions & 0 deletions build/default/Testing/20240625-1924/Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<Site BuildName="Linux-c++"
BuildStamp="20240625-1924-Experimental"
Name="CASLabLinux"
Generator="ctest-3.16.3"
CompilerName="/usr/bin/c++"
CompilerVersion="9.4.0"
OSName="Linux"
Hostname="CASLabLinux"
OSRelease="5.15.0-107-generic"
OSVersion="#117~20.04.1-Ubuntu SMP Tue Apr 30 10:35:57 UTC 2024"
OSPlatform="x86_64"
Is64Bits="1"
VendorString="GenuineIntel"
VendorID="Intel Corporation"
FamilyID="6"
ModelID="158"
ProcessorCacheSize="12288"
NumberOfLogicalCPU="12"
NumberOfPhysicalCPU="6"
TotalVirtualMemory="2047"
TotalPhysicalMemory="31732"
LogicalProcessorsPerPhysical="2"
ProcessorClockFrequency="3159.87"
>
<Testing>
<StartDateTime>Jun 25 14:24 CDT</StartDateTime>
<StartTestTime>1719343490</StartTestTime>
<TestList/>
<EndDateTime>Jun 25 14:24 CDT</EndDateTime>
<EndTestTime>1719343490</EndTestTime>
<ElapsedMinutes>0</ElapsedMinutes>
</Testing>
</Site>
2 changes: 1 addition & 1 deletion build/default/Testing/TAG
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
20240619-1525
20240625-1924
Experimental
Experimental
4 changes: 2 additions & 2 deletions build/default/Testing/Temporary/LastTest_20240619-1525.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: Jun 19 10:25 CDT
Start testing: Jun 19 11:23 CDT
----------------------------------------------------------
End testing: Jun 19 10:25 CDT
End testing: Jun 19 11:23 CDT
3 changes: 3 additions & 0 deletions build/default/Testing/Temporary/LastTest_20240625-1924.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Start testing: Jun 25 14:24 CDT
----------------------------------------------------------
End testing: Jun 25 14:24 CDT
4 changes: 4 additions & 0 deletions include/bernstein_probability.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "../../BeBOT_cpp/include/bernsteinpoly.h"
#include "../../BeBOT_cpp/include/bernsteinmatrix_a2b.h"

std::vector<std::vector<double>> convertTo2D(const std::vector<double>& flatMatrix,
size_t rows,
size_t cols);

std::vector<std::vector<double>> bernstein_probability(const std::vector<double>& x,
const std::vector<double>& x_cdf,
const std::vector<double>& P);
17 changes: 12 additions & 5 deletions src/bernstein_estimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
#include "../include/bernstein_estimator.h"

// Function to calculate ECDF (Empirical Cumulative Distribution Function)

std::pair<std::vector<double>, std::vector<double>> ecdf(const std::vector<double>& data) {
// Sort the data
std::vector<double> sorted_data = data;
std::sort(sorted_data.begin(), sorted_data.end());

std::vector<double> cdf(data.size());
std::iota(cdf.begin(), cdf.end(), 1);
for (auto& val : cdf) {
val /= data.size();
// Compute the ECDF values
std::vector<double> cdf(sorted_data.size() + 1);
cdf[0] = 0.0; // First element is 0
for (size_t i = 1; i < sorted_data.size(); ++i) {
cdf[i] = static_cast<double>(i) / sorted_data.size();
}

cdf.back() = 1.0; // Last element is 1

// Include the first element in sorted_data to match the cdf size
sorted_data.insert(sorted_data.begin(), sorted_data.front());

return {cdf, sorted_data};
}

Expand Down
27 changes: 18 additions & 9 deletions src/bernstein_probability.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#include "../include/bernstein_probability.h"
#include <vector>
#include <algorithm>
#include <cblas.h>

// Helper function to convert a 1D vector to a 2D vector
std::vector<std::vector<double>> convertTo2D(const std::vector<double>& flatMatrix, size_t rows, size_t cols) {
std::vector<std::vector<double>> matrix(rows, std::vector<double>(cols));
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
matrix[i][j] = flatMatrix[i * cols + j];
}
}
return matrix;
}

std::vector<std::vector<double>> bernstein_probability(const std::vector<double>& x,
const std::vector<double>& x_cdf,
Expand All @@ -10,8 +22,7 @@ std::vector<std::vector<double>> bernstein_probability(const std::vector<double>

for (size_t k = 0; k < n; ++k) {
// Find the last index in x_cdf where x_cdf[index] <= x[k]
auto it = std::find_if(x_cdf.rbegin(), x_cdf.rend(),
[&](double val) { return val <= x[k]; });
auto it = std::find_if(x_cdf.rbegin(), x_cdf.rend(), [&](double val) { return val <= x[k]; });

if (it == x_cdf.rend()) {
prob[k] = P[0];
Expand All @@ -24,16 +35,14 @@ std::vector<std::vector<double>> bernstein_probability(const std::vector<double>
// Assuming BeBOT class is defined and has methods `calculate()` and `getDifferentiationMatrix()`
Bebot bebot(n-1, 1);
bebot.calculate();
std::vector<std::vector<double>> Dm = bebot.getDifferentiationMatrix();
const auto& Dm = bebot.getDifferentiationMatrix();

// Output vector
std::vector<double> pdf(n, 0.0);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
pdf[i] += prob[j] * Dm[j][i];
}
}
cblas_dgemv(CblasColMajor, CblasTrans, n, n, 1.0, Dm.data(), n, prob.data(), 1, 0.0, pdf.data(), 1);

// Assuming BernsteinPoly function is defined
std::vector<std::vector<double>> BP_dot = BernsteinPoly({pdf}, x, 0, 1);
std::vector<std::vector<double>> pdf_2d(1, pdf);
std::vector<std::vector<double>> BP_dot = BernsteinPoly(pdf_2d, x, 0, 1);
return BP_dot;
}

0 comments on commit 6a39e0b

Please sign in to comment.