Hi Sofiane,

Thanks for the trick : I did not know this constructor.

However, my particular need was really to get over the linear model. My 
particular applications are the calibration of a model based on the BLUE or 
ridge regression. In these cases, we really need to solve the basic linear 
least squares problem, without any link to a functional basis.

How would you do that ?

Best regards,

Michaël


Michaël BAUDIN
Ingénieur - Chercheur
EDF – R&D
Département PRISME
6, quai Watier
78401 CHATOU
michael.bau...@edf.fr<mailto:michael.bau...@edf.fr>
Tél. : 01 30 87 81 82
Fax : 01 30 87 82 13

De : sofiane_had...@yahoo.fr [mailto:sofiane_had...@yahoo.fr]
Envoyé : mardi 2 avril 2019 09:00
À : users@openturns.org; BAUDIN Michael <michael.bau...@edf.fr>
Objet : Re: [ot-users] Linear least squares always insert a constant

Hi Michael,

The LinearModelAlgorithm indeed relies on a linear basis (if no one is 
provided).

However you can define your own basis and provide it to the algorithm class :

"""
import openturns as ot

ot.RandomGenerator.SetSeed(0)
g = ot.SymbolicFunction(['x', 'y'], ['0.5+sin(x)-2*y'])

npoints = 50

x = ot.Uniform(-2,2).getSample(npoints)
x.stack(ot.Normal(0, 3).getSample(npoints))
x.setDescription(["x", "y"])
y = g(x)

 # Create basis B = ( [x,y]-->x, [x,y]-->y )
input_description = x.getDescription()
basis = ot.Basis([ot.SymbolicFunction(input_description, [description_i])  for 
description_i in input_description])

algo = ot.LinearModelAlgorithm(x, basis, y)
algo.run()
...
"""

The trend coefficients's size is 2 corresponding to the basis size

Sofiane

Le jeudi 28 mars 2019 à 20:09:52 UTC+1, BAUDIN Michael 
<michael.bau...@edf.fr<mailto:michael.bau...@edf.fr>> a écrit :



Dear all !



The LinearLeastSquares class in OT 1.12 always inserts a constant in the model, 
be it wanted by the user or not:



https://github.com/openturns/openturns/blob/d0802a1b17b60bd86afa234662a047bc4f04492f/lib/src/Base/MetaModel/LinearLeastSquares.cxx#L105



In the API, this term corresponds to the getConstant() method.



The same is true for LinearModelFactory.



In the demo script in PS, I use linear least squares to approximate the sine 
function with the polynomial basis 1, x, x^2, x^3. The linear model involves 4 
coefficients. An intercept is always added leading to 5 estimated coefficients, 
that I do not want.



g = ot.SymbolicFunction(['x'], ['0.5+sin(x)'])

npoints = 50

x=ot.Uniform(-2,2).getSample(npoints).sort()

y = g(x)



# Create input

basis = ot.SymbolicFunction(['x'], ['1','x','x^2','x^3'])

inputData = basis(x)



With LinearLeastSquares, I get a constant:



myLeastSquares = ot.LinearLeastSquares(inputData, y)

myLeastSquares.run()

beta0 = myLeastSquares.getConstant()[0]



With LinearModelFactory, I get 5 coefficicents instead of 4:



LMF = ot.LinearModelFactory()

linearModel = LMF.build(inputData, y)

beta = linearModel.getRegression()



As far as I can see, the LinearModelAlgorithm in OT 1.13 has the same behaviour:



https://github.com/openturns/openturns/blob/ce1bc890a907faeecde495f5528ed42e401153c7/lib/src/Uncertainty/Algorithm/MetaModel/LinearModel/LinearModelAlgorithm.cxx#L65



I assume that the constant is always there, so that the method prevents from 
having a bias in the estimate. But in cases where you want really to perform 
linear least squares, then there is an issue.



As far as I can see, the LeastSquaresMethod is the right tool. Unfortunately, 
this cannot be used from the Python API.



Am I correct ?



Best regards,



Michaël



PS



import openturns as ot

from openturns.viewer import View



g = ot.SymbolicFunction(['x'], ['0.5+sin(x)'])

npoints = 50

x=ot.Uniform(-2,2).getSample(npoints).sort()

y = g(x)



# Create input

basis = ot.SymbolicFunction(['x'], ['1','x','x^2','x^3'])

inputData = basis(x)



# Solve

myLeastSquares = ot.LinearLeastSquares(inputData, y)

myLeastSquares.run()

beta0 = myLeastSquares.getConstant()[0]

print("beta0=%s" % (beta0))

beta = myLeastSquares.getLinear()

print("beta=%s" % (beta))

# Check

responseSurface = myLeastSquares.getResponseSurface()

ypredicted = responseSurface(inputData)

#

graph = ot.Graph("Linear Model","x","y",True,"topleft")

curve = ot.Curve(x,ypredicted)

curve.setLegend("Linear Model")

graph.add(curve)

cloud = ot.Cloud(x,y)

cloud.setColor("red")

cloud.setLegend("Data")

graph.add(cloud)

View(graph)



#

ot.ResourceMap.SetAsString('R-executable-command','bla\\bla\\R.exe')

LMF = ot.LinearModelFactory()

linearModel = LMF.build(inputData, y)

beta = linearModel.getRegression()

print(beta)



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
users@openturns.org<mailto:users@openturns.org>
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
users@openturns.org
http://openturns.org/mailman/listinfo/users

Reply via email to