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
[email protected]
http://openturns.org/mailman/listinfo/users

Reply via email to