The main point is: if you rely on the objects upon which the algorithm is
built, they are more strongly typed than the objects you retreive from the
algorithm. And you are right, the only way to downcast the C++ objects in
Python is to inspect their internal representation, either str(obj) or (more
likely) repr(obj). So it is the solution I was working on before to be
interrupted by a colleague!
Here is the solution I propose (see attached script).
Cheers
Régis
________________________________
De : BAUDIN Michael <[email protected]>
À : "[email protected]" <[email protected]>;
"[email protected]" <[email protected]>
Envoyé le : Mercredi 19 avril 2017 15h24
Objet : Re: [ot-users] Orthogonal basis of a polynomial chaos
That's true : not all distributions are normal distributions !
I would like to get the "type of" polynomial used for each variable. In the
example I used (Ishigami function), this would allow to print the following
table :
Variable Distribution Polynomial
X0 Uniform Legendre
X1 Uniform Legendre
X2 Uniform Legendre
Hence, the information I am looking for could be :
* a string, e.g. "Legendre",
* a class, e.g. "LegendreFactory".
Computing the string depending on the class would be a simple map (e.g.
LegendreFactory > Legendre). However, if that map was already available in OT,
that would make the process a little bit easier.
If I was able to get an access to the LegendreFactory used in the polynomial,
it would be easy to get the information :
>>> from openturns import LegendreFactory
>>> polyfamily = LegendreFactory()
>>> polyfamily.getClassName()
'LegendreFactory'
With this information, the table would be :
Variable Distribution Polynomial
X0 Uniform LegendreFactory
X1 Uniform LegendreFactory
X2 Uniform LegendreFactory
This table is already very informative for me : no need for a map in this case.
I answer to the other questions :
* The expression of these polynomials in the canonical basis in order to check
that they match your expectation? No : the expression itself does not interest
me at all.
* And to get this information, you want to rely only on the algorithm? Yes, I
would rather do this : this is because it makes things simpler, as a
post-processing of the chaos. However, that might be impossible : in this case,
using intermediate objects in the script would do the trick.
* If yes, is it before or after it has computed the approximation? I would
rather do it after the computation. But, since all the information is available
before the computation of the coefficients, I guess that it would make sense to
use the information available before.
I was thinking of a trick. The information is available inside the pretty-print
:
>>> poly0 = polyColl[0]
>>> poly0
class=OrthogonalUniVariatePolynomialFamily
implementation=class=StandardDistributionPolynomialFactory
hasSpecificFamily=true
specificFamily=class=OrthogonalUniVariatePolynomialFamily
implementation=class=LegendreFactory measure=class=Uniform name=Uniform
dimension=1 a=-1 b=1
So I could use the string generated with str(poly0) ans parse this string...
Ugly, isn'it ?
Regards,
Michaël
-----Message d'origine-----
De : [email protected]
[mailto:[email protected]]
Envoyé : mercredi 19 avril 2017 14:32
À : BAUDIN Michael; [email protected]
Objet : Re: [ot-users] Orthogonal basis of a polynomial chaos
Hi Michael,
First of all, thanks for using OpenTURNS ;-)! And a remark: in the case of
uniform distributions, you are more likely to get Legendre polynomials than
Hermite polynomials :-)
Something in your request is not clear for me. What kind of confirmation do you
want to get from openTURNS? A string containing the name of the univariate
families of polynomials which have been used? The expression of these
polynomials in the canonical basis in order to check that they match your
expectation? And to get this information, you want to rely only on the
algorithm? If yes, is it before or after it has computed the approximation?
Remember that the algorithm can work with ANY multivariate bases, not only
polynomial ones, not even tensorized one, so if you rely only on the algorithm
you will have to work a little bit to get the information you are looking for
(and I will help you if you answer the few questions above).
In your script, you name your MonteCarloeExperiment as 'sample', which is
misleading as it is NOT a sample, but rather a way to generate one. It is a
particular case of WeightedExperiment, ie an algorithm able to produce samples.
Cheers
Régis
________________________________
De : BAUDIN Michael <[email protected]> À : "[email protected]"
<[email protected]> Envoyé le : Mercredi 19 avril 2017 13h20 Objet : Re:
[ot-users] Orthogonal basis of a polynomial chaos
Hi again,
Here is a script in attachment to experiment with the objects.
Regards,
Michaël
De :BAUDIN Michael
Envoyé : mercredi 19 avril 2017 11:50
À : '[email protected]'
Objet : Orthogonal basis of a polynomial chaos
Hi,
After a (functionnal) polynomial chaos has been run, I am searching a way to
explore what basis was used. For example, if a Uniform distribution was used, I
would like to confirm that a Hermite polynomial was used. Of course, this
cannot always been done, especially if one of the input distributions was «
non-standard ».
However, even in the classical case, I was not able to find out what method of
the polynomialChaosAlgorithm could be used to retrieve the information. For
example :
[…]
# Create Polynomial Chaos
polynomialChaosAlgorithm = FunctionalChaosAlgorithm(myFunction, \
inputDistribution, fixedStrategy, evalStrategy)
# Compute expansion coefficients
polynomialChaosAlgorithm.run()
# Explore the result
myAdapStrat = polynomialChaosAlgorithm.getAdaptiveStrategy() # a
AdaptiveStrategy
mybasis = myAdapStrat.getBasis() # a OrthogonalBasis
# and after … ?
Afterwhile, I thought that this was not possible, because the basis was an
hidden internal object. This is why I tried a second solution : directly
explore the polynomial collection used to create the collection of univariate
polynomials :
# Create a coolection of standard univariate polynomials
polyColl = PolynomialFamilyCollection(dim)
for i in range(dim):
marginali=inputDistribution.getMarginal(i)
polyColl[i] = StandardDistributionPolynomialFactory(marginali)
# Explore the first polynomial
poly0 = polyColl[0]
# and after … ?
But I was not able to find a way to the Hermite polynomial neither.
Has anyone an idea ?
Regards,
Michaël
Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à
l'intention exclusive des destinataires et les informations qui y figurent sont
strictement confidentielles. Toute utilisation de ce Message non conforme à sa
destination, toute diffusion ou toute publication totale ou partielle, est
interdite sauf autorisation expresse.
Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le
copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si
vous avez reçu ce Message par erreur, merci de le supprimer de votre système,
ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support
que ce soit. Nous vous remercions également d'en avertir immédiatement
l'expéditeur par retour du message.
Il est impossible de garantir que les communications par messagerie
électronique arrivent en temps utile, sont sécurisées ou dénuées de toute
erreur ou virus.
____________________________________________________
This message and any attachments (the 'Message') are intended solely for the
addressees. The information contained in this Message is confidential. Any use
of information contained in this Message not in accord with its purpose, any
dissemination or disclosure, either whole or partial, is prohibited except
formal approval.
If you are not the addressee, you may not copy, forward, disclose or use any
part of it. If you have received this message in error, please delete it and
all copies from your system and notify the sender immediately by return message.
E-mail communication cannot be guaranteed to be timely secure, error or
virus-free.
_______________________________________________
OpenTURNS users mailing list
[email protected]
http://openturns.org/mailman/listinfo/users
Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à
l'intention exclusive des destinataires et les informations qui y figurent sont
strictement confidentielles. Toute utilisation de ce Message non conforme à sa
destination, toute diffusion ou toute publication totale ou partielle, est
interdite sauf autorisation expresse.
Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le
copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si
vous avez reçu ce Message par erreur, merci de le supprimer de votre système,
ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support
que ce soit. Nous vous remercions également d'en avertir immédiatement
l'expéditeur par retour du message.
Il est impossible de garantir que les communications par messagerie
électronique arrivent en temps utile, sont sécurisées ou dénuées de toute
erreur ou virus.
____________________________________________________
This message and any attachments (the 'Message') are intended solely for the
addressees. The information contained in this Message is confidential. Any use
of information contained in this Message not in accord with its purpose, any
dissemination or disclosure, either whole or partial, is prohibited except
formal approval.
If you are not the addressee, you may not copy, forward, disclose or use any
part of it. If you have received this message in error, please delete it and
all copies from your system and notify the sender immediately by return message.
E-mail communication cannot be guaranteed to be timely secure, error or
virus-free.
_______________________________________________
OpenTURNS users mailing list
[email protected]
http://openturns.org/mailman/listinfo/usersfrom math import pi, sin
from openturns import (
Uniform, PythonFunction, ComposedDistribution,
PolynomialFamilyCollection, LinearEnumerateFunction,
OrthogonalProductPolynomialFactory, OrthogonalBasis,
FixedStrategy, FunctionalChaosAlgorithm, MonteCarloExperiment,
LeastSquaresStrategy, StandardDistributionPolynomialFactory, Gumbel
)
def ishigamiG(x):
a = 7.0
b = 0.1
y=sin(x[0]) + a*sin(x[1])**2 + b*x[2]**4*sin(x[0])
return [y]
dim=3 # Number of input parameters
size = 500 # Size of the experimental design
totaldeg = 10 # Total degree
# Define the G function
myFunction = PythonFunction(dim,1,ishigamiG)
# Create the Input and Output random variables
# Create the marginal distriutions of the input random vector
distributionX0 = Uniform(-pi,pi)
distributionX1 = Uniform(-pi,pi)
distributionX2 = Gumbel()#Uniform(-pi,pi)
# Create the joint distribution
inputDistribution = ComposedDistribution( \
(distributionX0,distributionX1,distributionX2))
# Create a coolection of standard univariate polynomials
polyColl = PolynomialFamilyCollection(dim)
for i in range(dim):
marginali=inputDistribution.getMarginal(i)
polyColl[i] = StandardDistributionPolynomialFactory(marginali)
#
enumerateFunction = LinearEnumerateFunction(dim)
# Create the polynomial basis
basis = OrthogonalProductPolynomialFactory(polyColl,enumerateFunction)
# Create the DOE and the coefficient computation strategy
# Not a sample here, just a way to generate one
experiment = MonteCarloExperiment(size)
evalStrategy = LeastSquaresStrategy(experiment)
# Choose the truncation strategy
# Number of PC terms
P = enumerateFunction.getStrataCumulatedCardinal(totaldeg)
fixedStrategy = FixedStrategy(basis,P)
# Create Polynomial Chaos
polynomialChaosAlgorithm = FunctionalChaosAlgorithm(myFunction, \
inputDistribution, fixedStrategy, evalStrategy)
# Compute expansion coefficients
polynomialChaosAlgorithm.run()
# Get the result
chaosResult = polynomialChaosAlgorithm.getResult()
# Get the orthogonal basis as a string
basis_str = str(chaosResult.getOrthogonalBasis())
variables_names = chaosResult.getDistribution().getDescription()
# Check if the basis is a product
tab = 14
if "Product" in basis_str:
# Get the factors
tokens = basis_str.split("hasSpecificFamily")
print str("Variable").ljust(tab)+str("Distribution").ljust(tab)+str("Polynomial").ljust(tab)
for i in range(1, len(tokens)):
marginal_information = tokens[i].split("=")
has_specific_family = "true" in marginal_information[1]
if has_specific_family:
distribution_name = marginal_information[7].split(" ")[0]
family_name = marginal_information[5].split(" ")[0].split("Factory")[0]
else:
distribution_name = marginal_information[8].split(" ")[0]
family_name = "Adapted"
print variables_names[i-1].ljust(tab)+distribution_name.ljust(tab)+family_name.ljust(tab)
else:
print "Non-tensorized basis"
_______________________________________________
OpenTURNS users mailing list
[email protected]
http://openturns.org/mailman/listinfo/users