Browse Source

sampling from a distribution and from a choice

main
Sebastian Junges 5 years ago
parent
commit
aa6a3d2142
  1. 5
      src/storm/generator/Choice.cpp
  2. 11
      src/storm/generator/Choice.h
  3. 14
      src/storm/storage/Distribution.cpp
  4. 11
      src/storm/storage/Distribution.h

5
src/storm/generator/Choice.cpp

@ -45,6 +45,11 @@ namespace storm {
} }
} }
template<typename ValueType, typename StateType>
StateType Choice<ValueType, StateType>::sampleFromDistribution(ValueType const& quantile) const {
return distribution.sampleFromDistribution(quantile);
}
template<typename ValueType, typename StateType> template<typename ValueType, typename StateType>
typename storm::storage::Distribution<ValueType, StateType>::iterator Choice<ValueType, StateType>::begin() { typename storm::storage::Distribution<ValueType, StateType>::iterator Choice<ValueType, StateType>::begin() {
return distribution.begin(); return distribution.begin();

11
src/storm/generator/Choice.h

@ -29,6 +29,17 @@ namespace storm {
*/ */
void add(Choice const& other); void add(Choice const& other);
/**
* Given a value q, find the event in the ordered distribution that corresponds to this prob.
* Example: Given a (sub)distribution { x -> 0.4, y -> 0.3, z -> 0.2 },
* A value q in [0,0.4] yields x, q in [0.4, 0.7] yields y, and q in [0.7, 0.9] yields z.
* Any other value for q yields undefined behavior.
*
* @param quantile q, a value in the CDF.
* @return A state
*/
StateType sampleFromDistribution(ValueType const& quantile) const;
/*! /*!
* Returns an iterator to the distribution associated with this choice. * Returns an iterator to the distribution associated with this choice.
* *

14
src/storm/storage/Distribution.cpp

@ -177,6 +177,20 @@ namespace storm {
} }
} }
template<typename ValueType, typename StateType>
StateType Distribution<ValueType, StateType>::sampleFromDistribution(const ValueType &quantile) const {
ValueType sum = storm::utility::zero<ValueType>();
storm::utility::ConstantsComparator<ValueType> comp;
for (auto const& entry: distribution) {
sum += entry.second;
if (comp.isLess(quantile,sum)) {
return entry.first;
}
}
STORM_LOG_ASSERT(false,"This point should not be reached.");
return 0;
}
template class Distribution<double>; template class Distribution<double>;
template std::ostream& operator<<(std::ostream& out, Distribution<double> const& distribution); template std::ostream& operator<<(std::ostream& out, Distribution<double> const& distribution);

11
src/storm/storage/Distribution.h

@ -149,6 +149,17 @@ namespace storm {
*/ */
void normalize(); void normalize();
/**
* Given a value q, find the event in the ordered distribution that corresponds to this prob.
* Example: Given a (sub)distribution { x -> 0.4, y -> 0.3, z -> 0.2 },
* A value q in [0,0.4] yields x, q in [0.4, 0.7] yields y, and q in [0.7, 0.9] yields z.
* Any other value for q yields undefined behavior.
*
* @param quantile q, a value in the CDF.
* @return A state
*/
StateType sampleFromDistribution(ValueType const& quantile) const;
private: private:
// A list of states and the probabilities that are assigned to them. // A list of states and the probabilities that are assigned to them.
container_type distribution; container_type distribution;

Loading…
Cancel
Save