TEST(discrete_distribution, sampling) { typedef Eigen::Vector3d Variate; typedef Eigen::Matrix3d Covariance; // pick some mean and covariance Covariance covariance; covariance << 4.4, 2.1, -1.3, 2.2, 5.6, 1.2, -1.2, 1.9, 3.9; covariance = covariance * covariance.transpose(); Variate mean; mean << 2.1, 50.2, 20.1; // create gaussian fl::Gaussian<Variate> gaussian; gaussian.mean(mean); gaussian.covariance(covariance); // generate a sum of delta from gaussian fl::SumOfDeltas<Variate> sum_of_delta; sum_of_delta.resize(100000); for(size_t i = 0; i < sum_of_delta.size(); i++) { sum_of_delta.location(i) = gaussian.sample(); sum_of_delta.probability(i) = 1. / sum_of_delta.size(); } // compare mean and covariance Covariance covariance_delta = sum_of_delta.covariance().inverse() * gaussian.covariance(); EXPECT_TRUE(covariance_delta.isApprox(Covariance::Identity(), 0.1)); EXPECT_TRUE((gaussian.square_root().inverse() * (sum_of_delta.mean()-mean)).norm() < 0.1); // check entropy of uniform distribution for(size_t i = 0; i < sum_of_delta.size(); i++) { sum_of_delta.probability(i) = 1. / sum_of_delta.size(); } EXPECT_TRUE(fabs(std::log(double(sum_of_delta.size())) - sum_of_delta.entropy()) < 0.0000001); // check entropy of certain distribution for(size_t i = 0; i < sum_of_delta.size(); i++) { sum_of_delta.probability(i) = 0; } sum_of_delta.probability(0) = 1; EXPECT_TRUE(fabs(sum_of_delta.entropy()) < 0.0000001); }
Rgrid_ellips_neighborhood:: Rgrid_ellips_neighborhood( RGrid* grid, Grid_continuous_property* property, GsTLInt max_radius, GsTLInt mid_radius, GsTLInt min_radius, double x_angle, double y_angle, double z_angle, int max_neighbors, const Covariance<GsTLPoint>* cov, const Grid_region* region) : Neighborhood(), grid_( grid ), property_( property ), max_neighbors_( max_neighbors ) { region_ = region; appli_assert( grid_ ); cursor_ = *( grid_->cursor() ); // use a geobody approach to find all the cells inside the ellipsoid. // Then sort them according to covariance. Ellipsoid_rasterizer initializer( 2*grid->nx()+1, 2*grid->ny()+1, 2*grid->nz()+1, max_radius, mid_radius, min_radius, x_angle, y_angle, z_angle ); std::vector< Ellipsoid_rasterizer::EuclideanVector >& templ = initializer.rasterize(); // We now sort the template according to covariance cov. // If no covariance was specified, create a default one if( !cov ) { Covariance<GsTLPoint> covar; int id = covar.add_structure( "Spherical" ); covar.set_geometry( id, max_radius, mid_radius, min_radius, x_angle, y_angle, z_angle ); std::sort( templ.begin(), templ.end(), Evector_greater_than( covar ) ); } else std::sort( templ.begin(), templ.end(), Evector_greater_than( *cov ) ); geom_.init( templ.begin(), templ.end() ); }
float Interpolate1D::predict(const float & x) const { DenseMatrix<float > xTest(1,1); xTest.column(0)[0] = x - m_xMean->v()[0]; Covariance<float, RbfKernel<float> > covTest; covTest.create(xTest, *m_xTrain, *m_rbf); DenseMatrix<float> KxKtraininv(covTest.K().numRows(), m_covTrain->Kinv().numCols() ); covTest.K().mult(KxKtraininv, m_covTrain->Kinv() ); /// yPred = Ktest * inv(Ktrain) * yTrain DenseMatrix<float> yPred(1,1); KxKtraininv.mult(yPred, *m_yTrain); return (yPred.column(0)[0] + m_yMean->v()[0]); }
//------------------------------------------------------------------------------ void ExtendedKalmanInv::Symmetrize(Covariance& mat) { Integer size = mat.GetDimension(); for (Integer i = 0; i < size; ++i) { for (Integer j = i+1; j < size; ++j) { mat(i,j) = 0.5 * (mat(i,j) + mat(j,i)); mat(j,i) = mat(i,j); } } }
TEST(enchilada, some_test) { typedef Eigen::Vector3d Variate; typedef Eigen::Matrix3d Covariance; // pick some mean and covariance Covariance covariance; covariance << 4.4, 2.1, -1.3, 2.2, 5.6, 1.2, -1.2, 1.9, 3.9; covariance = covariance * covariance.transpose(); Variate mean; mean << 2.1, 50.2, 20.1; // create gaussian fl::Gaussian<Variate> gaussian; gaussian.mean(mean); gaussian.covariance(covariance); // generate a sum of delta from gaussian fl::SumOfDeltas<Variate> sum_of_delta; sum_of_delta.resize(100000); for(size_t i = 0; i < sum_of_delta.size(); i++) { sum_of_delta.location(i) = gaussian.sample(); sum_of_delta.weight(i) = 1. / sum_of_delta.size(); } // compare mean and covariance Covariance covariance_delta = sum_of_delta.covariance().inverse() * gaussian.covariance(); EXPECT_TRUE(covariance_delta.isApprox(Covariance::Identity(), 0.1)); EXPECT_TRUE((gaussian.square_root().inverse() * (sum_of_delta.mean()-mean)).norm() < 0.1); }
TestKernelWindow::TestKernelWindow() { std::cout<<" test kernel "; std::cout<<"\n build X"; DenseMatrix<float> X(30,1); linspace<float>(X.column(0), -1.f, 1.f, 30); std::cout<<"\n build kernel"; RbfKernel<float> rbf(0.33); Covariance<float, RbfKernel<float> > cov; cov.create(X, rbf); m_kernView = new KernelWidget(this); m_kernView->plotK(&cov.K()); setCentralWidget(m_kernView); QDateTime local(QDateTime::currentDateTime()); qDebug() << "Local time is:" << local; srand (local.toTime_t() ); m_smpdlg = new SampleKernelDialog(cov, this); setWindowTitle(tr("RBF Kernel")); createActions(); createMenus(); qRegisterMetaType<QImage>("QImage"); //connect(m_thread, SIGNAL(sendInitialDictionary(QImage)), // this, SLOT(recvInitialDictionary(QImage))); m_smpdlg->show(); m_smpdlg->move(0,0); }
int main() { const int grid_size = 10; // Build grid with locally varying mean Cartesian_grid* lvm_grid = new Cartesian_grid( grid_size, grid_size, 1 ); GsTLGridProperty* prop = lvm_grid->add_property( "trend", typeid( float ) ); for( int i=0; i< grid_size*grid_size/2 ; i++ ) { prop->set_value( 0.0, i ); } for( int i=grid_size*grid_size/2; i< grid_size*grid_size ; i++ ) { prop->set_value( 10.0, i ); } Colocated_neighborhood* coloc_neigh = dynamic_cast<Colocated_neighborhood*>( lvm_grid->colocated_neighborhood( "trend" ) ); // Build kriging grid Cartesian_grid* krig_grid = new Cartesian_grid( grid_size, grid_size, 1 ); GsTLGridProperty* krig_prop = krig_grid->add_property( string("krig"), typeid( float ) ); krig_grid->select_property( "krig"); // Build harddata grid const int pointset_size = 4; Point_set* harddata = new Point_set( pointset_size ); std::vector<GsTLPoint> locations; locations.push_back( GsTLPoint( 0,0,0 ) ); locations.push_back( GsTLPoint( 1,5,0 ) ); locations.push_back( GsTLPoint( 8,8,0 ) ); locations.push_back( GsTLPoint( 5,2,0 ) ); harddata->point_locations( locations ); GsTLGridProperty* hard_prop = harddata->add_property( "poro" ); for( int i=0; i<pointset_size; i++ ) { hard_prop->set_value( i, i ); } harddata->select_property( "poro" ); // Set up covariance Covariance<GsTLPoint> cov; cov.nugget(0.1); cov.add_structure( "Spherical" ); cov.sill( 0, 0.9 ); cov.set_geometry( 0, 10,10,10, 0,0,0 ); Grid_initializer initializer; initializer.assign( krig_grid, krig_prop, harddata, "poro" ); for( int i=0; i< krig_prop->size(); i++ ) { if( krig_prop->is_harddata( i ) ) cout << "value at " << i << ": " << krig_prop->get_value( i ) << endl; } krig_grid->select_property( "krig"); Neighborhood* neighborhood = krig_grid->neighborhood( 20, 20, 1, 0,0,0, &cov, true ); typedef GsTLPoint Location; typedef std::vector<double>::const_iterator weight_iterator; typedef SKConstraints_impl< Neighborhood, Location > SKConstraints; typedef SK_local_mean_combiner<weight_iterator, Neighborhood, Colocated_neighborhood> LVM_combiner; typedef Kriging_constraints< Neighborhood, Location > KrigingConstraints; typedef Kriging_combiner< weight_iterator, Neighborhood > KrigingCombiner; LVM_combiner combiner( *coloc_neigh ); SKConstraints constraints; // initialize the algo Kriging algo; algo.simul_grid_ = krig_grid; algo.property_name_ = "krig"; algo.harddata_grid_ = 0; algo.neighborhood_ = neighborhood; algo.covar_ = cov; algo.combiner_ = new KrigingCombiner( &combiner ); algo.Kconstraints_ = new KrigingConstraints( &constraints ); // Run and output the results algo.execute(); ofstream ofile( "result.out" ); if( !ofile ) { cerr << "can't create file result.out" << endl; return 1; } GsTLGridProperty* prop1 = krig_grid->select_property( "krig" ); ofile << "kriging" << endl << "1" << endl << "krig" << endl ; for( int i=0; i< prop1->size(); i++ ) { if( prop1->is_informed( i ) ) ofile << prop1->get_value( i ) << endl; else ofile << "-99" << endl; } }
StandardBase() { P.setIdentity(); }
//------------------------------------------------------------------------------ bool EstimationStateManager::BuildState() { bool retval = false; #ifdef DEBUG_STATE_CONSTRUCTION MessageInterface::ShowMessage("Entered BuildState()\n"); #endif // Determine the size of the propagation state vector stateSize = SortVector(); if (stateSize <= 0) { std::stringstream sizeVal; sizeVal << stateSize; throw EstimatorException("Estimation state vector size is " + sizeVal.str() + "; estimation is not possible."); } std::map<std::string,Integer> associateMap; // Build the associate map std::string name; for (Integer index = 0; index < stateSize; ++index) { name = stateMap[index]->objectName; if (associateMap.find(name) == associateMap.end()) associateMap[name] = index; } state.SetSize(stateSize); // Build the data structures for the STM and covariance matrix stm.SetSize(stateSize, stateSize); covariance.SetDimension(stateSize); stmColCount = stateSize; covColCount = stateSize; for (Integer index = 0; index < stateSize; ++index) { name = stateMap[index]->objectName; std::stringstream sel(""); sel << stateMap[index]->subelement; state.SetElementProperties(index, stateMap[index]->elementID, name + "." + stateMap[index]->elementName + "." + sel.str(), associateMap[name]); } // Initialize the STM matrix for (Integer i = 0; i < stateSize; ++i) for (Integer j = 0; j < stateSize; ++j) if (i == j) stm(i,j) = 1.0; else stm(i,j) = 0.0; // Now build the covariance, using the elements the user has set and defaults // for the rest for (Integer i = 0; i < stateSize;) { Integer size = stateMap[i]->length; Integer id = stateMap[i]->parameterID; Covariance *cov = stateMap[i]->object->GetCovariance(); #ifdef DEBUG_COVARIANCE MessageInterface::ShowMessage("Found length = %d for id = %d\n", size, id); #endif Rmatrix *subcov = cov->GetCovariance(id); if (subcov) { #ifdef DEBUG_COVARIANCE MessageInterface::ShowMessage("Found %d by %d subcovariance\n", subcov->GetNumRows(), subcov->GetNumColumns()); #endif for (Integer j = 0; j < size; ++j) for (Integer k = 0; k < size; ++k) covariance(i+j,i+k) = (*subcov)(j,k); } else { #ifdef DEBUG_COVARIANCE MessageInterface::ShowMessage("Found no subcovariance\n"); #endif if (stateMap[i]->elementName == "CartesianState") { for (Integer j = 0; j < size; ++j) { for (Integer k = 0; k < size; ++k) { if (j < 3) covariance(i+j,i+k) = (j == k ? 1.0e12 : 0.0); else covariance(i+j,i+k) = (j == k ? 1.0e6 : 0.0); } } } else // Other defaults are set to the identity { for (Integer j = 0; j < size; ++j) { for (Integer k = 0; k < size; ++k) { covariance(i+j,i+k) = (j == k ? 1.0e0 : 0.0); } } } } i += size; } #ifdef DEBUG_STATE_CONSTRUCTION MessageInterface::ShowMessage( "Estimation state vector has %d elements:\n", stateSize); StringArray props = state.GetElementDescriptions(); for (Integer index = 0; index < stateSize; ++index) MessageInterface::ShowMessage(" %d: %s\n", index, props[index].c_str()); #endif #ifdef DUMP_STATE MapObjectsToVector(); for (Integer i = 0; i < stateSize; ++i) MessageInterface::ShowMessage("State[%02d] = %.12lf, %s %d\n", i, state[i], "RefState start =", state.GetAssociateIndex(i)); MessageInterface::ShowMessage("State Transition Matrix = %s\n", stm.ToString().c_str()); MessageInterface::ShowMessage("Covariance Matrix = %s\n", covariance.ToString().c_str()); #endif return retval; }
/** @warning This method is computationally expensive. Use with care! */ bool hasValidCovariance() const { return !cov.hasNaN(); }