-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a sampling module, currently containing only weighted_sample().
Jira: MADLIB-584 Sampling module: - Added a data-parallel UDA for weighted sampling of a single row C++ AL: - Added a boost/TR1-style front-end to PostgreSQL's random number generator.
- Loading branch information
Florian Schoppmann
authored and
Florian Schoppmann
committed
Jul 19, 2012
1 parent
5ef0cd5
commit 1afb10f
Showing
16 changed files
with
385 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* ----------------------------------------------------------------------------- | ||
* | ||
* @file sample.hpp | ||
* | ||
* @brief Umbrella header that includes all sampling headers | ||
* | ||
* -------------------------------------------------------------------------- */ | ||
|
||
#include "weighted_sample.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* ----------------------------------------------------------------------- *//** | ||
* | ||
* @file weighted_sample.cpp | ||
* | ||
* @brief Generate a single weighted random sample | ||
* | ||
*//* ----------------------------------------------------------------------- */ | ||
|
||
#include <dbconnector/dbconnector.hpp> | ||
#include <modules/shared/HandleTraits.hpp> | ||
|
||
#include <boost/tr1/random.hpp> | ||
|
||
#include "weighted_sample.hpp" | ||
|
||
// Import TR1 names (currently used from boost). This can go away once we make | ||
// the switch to C++11. | ||
namespace std { | ||
using tr1::bernoulli_distribution; | ||
} | ||
|
||
namespace madlib { | ||
|
||
namespace modules { | ||
|
||
namespace sample { | ||
|
||
/** | ||
* @brief Transition state for weighted sample | ||
* | ||
* Note: We assume that the DOUBLE PRECISION array is initialized by the | ||
* database with length 2, and all elemenets are 0. | ||
*/ | ||
template <class Handle> | ||
class WeightedSampleTransitionState { | ||
public: | ||
WeightedSampleTransitionState(const AnyType &inArray) | ||
: mStorage(inArray.getAs<Handle>()), | ||
sample_id(&mStorage[1]), | ||
weight_sum(&mStorage[0]) { } | ||
|
||
inline operator AnyType() const { | ||
return mStorage; | ||
} | ||
|
||
private: | ||
Handle mStorage; | ||
|
||
public: | ||
typename HandleTraits<Handle>::ReferenceToInt64 sample_id; | ||
typename HandleTraits<Handle>::ReferenceToDouble weight_sum; | ||
}; | ||
|
||
/** | ||
* @brief Perform the weighted-sample transition step | ||
*/ | ||
AnyType | ||
weighted_sample_transition::run(AnyType& args) { | ||
WeightedSampleTransitionState<MutableArrayHandle<double> > state = args[0]; | ||
uint64_t identifier = args[1].getAs<int64_t>(); | ||
double weight = args[2].getAs<double>(); | ||
|
||
// Instead of throwing an error, we will just ignore rows with a negative | ||
// weight | ||
if (weight > 0.) { | ||
state.weight_sum += weight; | ||
std::bernoulli_distribution success(weight / state.weight_sum); | ||
// Note that a NativeRandomNumberGenerator object is stateless, so it | ||
// is not a problem to instantiate an object for each RN generation... | ||
NativeRandomNumberGenerator generator; | ||
if (success(generator)) | ||
state.sample_id = identifier; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
/** | ||
* @brief Perform the merging of two transition states | ||
*/ | ||
AnyType | ||
weighted_sample_merge::run(AnyType &args) { | ||
WeightedSampleTransitionState<MutableArrayHandle<double> > stateLeft | ||
= args[0]; | ||
WeightedSampleTransitionState<ArrayHandle<double> > stateRight = args[1]; | ||
|
||
// FIXME: Once we have more modular states (independent of transition/merge | ||
// function), implement using the logic from the transition function | ||
stateLeft.weight_sum += stateRight.weight_sum; | ||
std::bernoulli_distribution success( | ||
stateRight.weight_sum / stateLeft.weight_sum); | ||
// Note that a NativeRandomNumberGenerator object is stateless, so it | ||
// is not a problem to instantiate an object for each RN generation... | ||
NativeRandomNumberGenerator generator; | ||
if (success(generator)) | ||
stateLeft.sample_id = stateRight.sample_id; | ||
|
||
return stateLeft; | ||
} | ||
|
||
/** | ||
* @brief Perform the weighted-sample final step | ||
*/ | ||
AnyType | ||
weighted_sample_final::run(AnyType &args) { | ||
WeightedSampleTransitionState<ArrayHandle<double> > state = args[0]; | ||
|
||
return static_cast<int64_t>(state.sample_id); | ||
} | ||
|
||
} // namespace stats | ||
|
||
} // namespace modules | ||
|
||
} // namespace madlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* ----------------------------------------------------------------------- *//** | ||
* | ||
* @file weighted_sample.hpp | ||
* | ||
*//* ----------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @brief Weighted random sample: Transition function | ||
*/ | ||
DECLARE_UDF(sample, weighted_sample_transition) | ||
|
||
/** | ||
* @brief Weighted random sample: State merge function | ||
*/ | ||
DECLARE_UDF(sample, weighted_sample_merge) | ||
|
||
/** | ||
* @brief Weighted random sample: Final function | ||
*/ | ||
DECLARE_UDF(sample, weighted_sample_final) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/ports/postgres/dbconnector/NativeRandomNumberGenerator_impl.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* ----------------------------------------------------------------------- *//** | ||
* | ||
* @file NativeRandomNumberGenerator_impl.hpp | ||
* | ||
*//* ----------------------------------------------------------------------- */ | ||
|
||
#ifndef MADLIB_POSTGRES_NATIVERANDOMNUMBERGENERATOR_IMPL_HPP | ||
#define MADLIB_POSTGRES_NATIVERANDOMNUMBERGENERATOR_IMPL_HPP | ||
|
||
namespace madlib { | ||
|
||
namespace dbconnector { | ||
|
||
namespace postgres { | ||
|
||
inline | ||
NativeRandomNumberGenerator::NativeRandomNumberGenerator() { } | ||
|
||
/** | ||
* @brief Sets the current state of the engine | ||
* | ||
* All this function does is calling the backend's seed functions. | ||
*/ | ||
inline | ||
void | ||
NativeRandomNumberGenerator::seed(result_type inSeed) { | ||
DirectFunctionCall1(setseed, Float8GetDatum(inSeed)); | ||
} | ||
|
||
/** | ||
* @brief Advances the engine's state and returns the generated value | ||
*/ | ||
inline | ||
NativeRandomNumberGenerator::result_type | ||
NativeRandomNumberGenerator::operator()() { | ||
// There seems to be no DirectFunctionCall0(). | ||
return DatumGetFloat8(DirectFunctionCall1(drandom, Datum(0))); | ||
} | ||
|
||
/** | ||
* @brief Return tight lower bound on the set of all values returned by | ||
* <tt>operator()</tt> | ||
*/ | ||
inline | ||
NativeRandomNumberGenerator::result_type | ||
NativeRandomNumberGenerator::min() { | ||
return 0.0; | ||
} | ||
|
||
/** | ||
* @brief Return smallest representable number larger than maximum of all values | ||
* returned by <tt>operator()</tt> | ||
* | ||
* Boost specifies, if \c result_type is not integer, then return "the smallest | ||
* representable number larger than the tight upper bound on the set of all | ||
* values returned by operator(). In any case, the return value of this function | ||
* shall not change during the lifetime of the object." | ||
* http://www.boost.org/doc/libs/1_50_0/doc/html/boost_random/reference.html | ||
*/ | ||
inline | ||
NativeRandomNumberGenerator::result_type | ||
NativeRandomNumberGenerator::max() { | ||
return 1.0; | ||
} | ||
|
||
} // namespace postgres | ||
|
||
} // namespace dbconnector | ||
|
||
} // namespace madlib | ||
|
||
#endif // defined(MADLIB_POSTGRES_NATIVERANDOMNUMBERGENERATOR_IMPL_HPP) |
Oops, something went wrong.