~ubuntu-branches/ubuntu/maverick/openturns/maverick

« back to all changes in this revision

Viewing changes to lib/src/Uncertainty/Algorithm/Simulation/ImportanceSampling/ImportanceSampling.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2010-05-10 17:27:55 UTC
  • mfrom: (1.1.4 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100510172755-cb5ynskknqqi5rhp
Tags: 0.13.2-2ubuntu1
* Merge with Debian testing. No changes left.
* ubuntu_fix-python-2.6.patch: fix detection of python 2.6 libs, to not use
  LOCALMODLIBS. This pulls a dependency on SSL and makes the package FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//                                               -*- C++ -*-
2
 
/**
3
 
 *  @file  ImportanceSampling.cxx
4
 
 *  @brief ImportanceSampling is an implementation of the importance sampling Monte Carlo simulation method
5
 
 *
6
 
 *  (C) Copyright 2005-2007 EDF-EADS-Phimeca
7
 
 *
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.
12
 
 *
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.
17
 
 *
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
21
 
 *
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 $
25
 
 */
26
 
#include "ImportanceSampling.hxx"
27
 
#include "ComparisonOperatorImplementation.hxx"
28
 
 
29
 
namespace OpenTURNS
30
 
{
31
 
 
32
 
  namespace Uncertainty 
33
 
  {
34
 
 
35
 
    namespace Algorithm
36
 
    {
37
 
 
38
 
      /*
39
 
       * @class ImportanceSampling
40
 
       */
41
 
 
42
 
      CLASSNAMEINIT(ImportanceSampling);
43
 
 
44
 
      /* Constructor with parameters */
45
 
      ImportanceSampling::ImportanceSampling(const Simulation::Event & event, const Distribution & importanceDistribution) throw(InvalidArgumentException):
46
 
        Simulation(event),
47
 
        importanceDistribution_(importanceDistribution)
48
 
      {
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";
51
 
      }
52
 
 
53
 
      /* Virtual constructor */
54
 
      ImportanceSampling * ImportanceSampling::clone() const
55
 
      {
56
 
        return new ImportanceSampling(*this);
57
 
      }
58
 
 
59
 
      /* Compute the block sample */
60
 
      ImportanceSampling::NumericalSample ImportanceSampling::computeBlockSample()
61
 
      {
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++)
69
 
          {
70
 
            inputStrategy_.store(inputSample[i]);
71
 
            outputStrategy_.store(blockSample[i]);
72
 
            if (getEvent().getOperator()(blockSample[i][0], getEvent().getThreshold()))
73
 
              {
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]);
77
 
              }
78
 
            else
79
 
              {
80
 
                blockSample[i][0] = 0.0;
81
 
              }
82
 
          }
83
 
        return blockSample;
84
 
      }
85
 
 
86
 
      /* Importance distribution accessor */
87
 
      ImportanceSampling::Distribution ImportanceSampling::getImportanceDistribution() const
88
 
      {
89
 
        return importanceDistribution_;
90
 
      }
91
 
 
92
 
      /* String converter */
93
 
      String ImportanceSampling::__repr__() const
94
 
      {
95
 
        OSS oss;
96
 
        oss << "class=" << ImportanceSampling::GetClassName()
97
 
            << " derived from " << Simulation::__repr__();
98
 
        return oss;
99
 
      }
100
 
 
101
 
    } /* namespace Algorithm */
102
 
  } /* namespace Uncertainty */
103
 
} /* namespace OpenTURNS */