3
* @file ImportanceSampling.cxx
4
* @brief ImportanceSampling is an implementation of the importance sampling Monte Carlo simulation method
6
* (C) Copyright 2005-2007 EDF-EADS-Phimeca
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License.
13
* This library is distributed in the hope that it will be useful
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
* @author: $LastChangedBy: dutka $
23
* @date: $LastChangedDate: 2009-05-28 14:47:53 +0200 (jeu. 28 mai 2009) $
24
* Id: $Id: ImportanceSampling.cxx 1262 2009-05-28 12:47:53Z dutka $
26
#include "ImportanceSampling.hxx"
27
#include "ComparisonOperatorImplementation.hxx"
39
* @class ImportanceSampling
42
CLASSNAMEINIT(ImportanceSampling);
44
/* Constructor with parameters */
45
ImportanceSampling::ImportanceSampling(const Simulation::Event & event, const Distribution & importanceDistribution) throw(InvalidArgumentException):
47
importanceDistribution_(importanceDistribution)
49
// Check if the importance distribution dimension is compatible with the event
50
if (importanceDistribution.getDimension() != event.getImplementation()->getAntecedent()->getDimension()) throw InvalidArgumentException(HERE) << "The importance distribution must have the same dimension as the event";
53
/* Virtual constructor */
54
ImportanceSampling * ImportanceSampling::clone() const
56
return new ImportanceSampling(*this);
59
/* Compute the block sample */
60
ImportanceSampling::NumericalSample ImportanceSampling::computeBlockSample()
62
UnsignedLong blockSize(getBlockSize());
63
// First, compute a sample of the importance distribution
64
NumericalSample inputSample(importanceDistribution_.getNumericalSample(blockSize));
65
// Then, evaluate the function on this sample
66
NumericalSample blockSample(getEvent().getImplementation()->getFunction()(inputSample));
67
// Then, modify in place this sample to take into account the change in the input distribution
68
for (UnsignedLong i = 0; i < blockSize; i++)
70
inputStrategy_.store(inputSample[i]);
71
outputStrategy_.store(blockSample[i]);
72
if (getEvent().getOperator()(blockSample[i][0], getEvent().getThreshold()))
74
// If the event occured, the value is p_initial(x[i]) / p_importance(x[i])
75
// Having access to p_initial is a long trip...
76
blockSample[i][0] = getEvent().getImplementation()->getAntecedent()->getDistribution().computePDF(inputSample[i]) / importanceDistribution_.computePDF(inputSample[i]);
80
blockSample[i][0] = 0.0;
86
/* Importance distribution accessor */
87
ImportanceSampling::Distribution ImportanceSampling::getImportanceDistribution() const
89
return importanceDistribution_;
92
/* String converter */
93
String ImportanceSampling::__repr__() const
96
oss << "class=" << ImportanceSampling::GetClassName()
97
<< " derived from " << Simulation::__repr__();
101
} /* namespace Algorithm */
102
} /* namespace Uncertainty */
103
} /* namespace OpenTURNS */