LinearRegressionEstimator#
- class LinearRegressionEstimator#
A class for fitting a Linear-Regression Model.
- decrypt_decode_phi(self: pyhelayers.LinearRegressionEstimator) List[float] #
Decrypts and decodes the model coefficients which were computed by fitMultipleLinearRegression() function. Returns the decrypted coefficients.
- encode_encrypt(self: pyhelayers.LinearRegressionEstimator, inputs: List[numpy.ndarray[numpy.float64]]) List[pyhelayers.CTileTensor] #
Encodes and encrypts the given inputs to be sent to fitMultipleLinearRegression() function.
- Parameters:
inputs – The plaintext inputs. This vector should contain two elements, representing xTrain and yTrain respectively.
- fit_multiple_linear_regression(self: pyhelayers.LinearRegressionEstimator, inputs: List[pyhelayers.CTileTensor]) None #
Fits this LinearRegression model on the given inputs. Use this function to fit a LinearRegression with more than one variable.
- Parameters:
inputs – The fit encrypted inputs (xTrain and yTrain). These inputs can be encrypted using encode_encrypt() function of this class.
- set_inverse_approximation_precision(self: pyhelayers.LinearRegressionEstimator, precision: int) None #
Controls the precision of the inverse approximation used by this LinearRegressionEstimator. A higher value increases accuracy on the account of increasing runtime.
- Parameters:
precision – The desired precision
-
class LinearRegressionEstimator : public helayers::SaveableBasic#
A class for fitting and/or predicting with a Linear-Regression (LR) model based on training or inference inputs.
In the methods below, x is the independent (a.k.a. explanatory) variable and y is the dependent (a.k.a. response) variable. In a simple LR model x has just one feature and y depends on x according to the formula: y = phi[0] + phi[1]*x In a multiple LR model x has multiple (F) features and y depends on x according to the formula: y = phi[0] + phi[1]*x0 + … + phi[F]x_{F-1},
Public Functions
-
inline LinearRegressionEstimator(const HeContext &he)#
Construct a new LinearRegressionEstimator object.
- Parameters:
he – The cryptographic context used to make the computations
-
inline void setEstimatedXDistributionNormal(double mean, double std)#
The user declares (via domain knowledge) that the model fitting operation may assume that the independent variable X distributes according to a normal distribution with given mean and standard deviation.
- Parameters:
mean – Mean of normal distribution of X
std – Standard-Deviation of normal distribution of X
-
inline void setEstimatedXDistributionUniform(double min, double max)#
The user declares (via domain knowledge) that the model fitting operation may assume that the independent variable X distributes according to a uniform distribution in some given range.
- Parameters:
min – Min of uniform distribution of X
max – Max of uniform distribution of X
-
inline void setEstimatedXDistributionUnknownInRange(double min, double max)#
The user declares (via domain knowledge) that the model fitting operation may assume that the independent variable X distributes according to some unknown distribution in the given range.
- Parameters:
min – Min possible value of X
max – Max possible value of X
-
inline void setInverseApproximationPrecision(int inverseApproximationPrecision)#
-
void addInput(CTileTensorCPtr input)#
Add an input training sample to the set of inputs used for training.
The user should add all the training samples prior to fitting the model.
- Parameters:
input – The input training sample
-
void encodeEncrypt(std::vector<CTileTensorCPtr> &res, const std::vector<DoubleTensorCPtr> &inputs) const#
Encodes and encrypts the given inputs to be sent to fitMultipleLinearRegression() function.
- Parameters:
res – An output parameter to store the encrypted inputs in
inputs – The plaintext inputs. This vector should contain two elements, representing xTrain and yTrain respectively.
-
void fitMultipleLinearRegression(const std::vector<CTileTensorCPtr> &inputs)#
Fit a multiple LR model on the given inputs.
In a multiple LR model x has multiple (F) features and y depends on x according to the formula: y = phi[0] + phi[1]*x0 + … + phi[F]x_{F-1},
- Parameters:
inputs – A vector whose first element is an encryption of xTrain and its second element is an encryption of yTrain. xTrain should be of the shape [numSamples/slotCount, numFeatures/1], while yTrain should be of the shape [numSamples/slotCount, 1/1].
- Throws:
invalid_argument – If the shapes of xTrain and yTrain don’t match the above description.
-
void fitSimpleLinearRegression()#
Fit a simple LR model on the given inputs.
In a simple LR model the dependent variable x has just one feature and y depends on x according to the formula: y = phi[0] + phi[1]*x The model is fit based on encrypted input samples that were previously added with the addInput method.
-
inline const CTileTensor &getPhi0()#
Get the Phi0 parameter of a simple LR model where y depends on x according to the formula y = phi[0] + phi[1]*x.
-
inline const CTileTensor &getPhi1()#
Get the Phi1 parameter of a simple LR model where y depends on x according to the formula y = phi[0] + phi[1]*x.
-
inline const CTileTensor &getPhi0() const#
Gets the fitted phi0 (intercept) Linear-Regression parameter.
-
inline const CTileTensor &getPhi1() const#
Gets the fitted phi1 (slope) Linear-Regression parameter.
-
inline const std::vector<CTileTensor> getPhi() const#
Returns the fitted phi coefficients computed by fitMultipleVariables() function.
-
std::vector<double> decryptDecodePhi() const#
Decrypts and decodes the model coefficients which were computed by fitMultipleLinearRegression() function.
Returns the decrypted coefficients.
-
inline void setVerbosity(Verbosity _verbosity)#
Sets the verbosity of the debug information logged by this class.
- Parameters:
_verbosity – The required verbosity level See the Verbosity enum for the various verbosity levels.
-
virtual void debugPrint(const std::string &title = "", Verbosity verbosity = VERBOSITY_REGULAR, std::ostream &out = std::cout) const override#
Prints the content of this object.
- Parameters:
title – Text to add to the print
verbosity – Verbosity level
out – Output stream
-
virtual std::streamoff save(std::ostream &stream) const override#
Saves this object to a stream in binary form.
Returns the number of bytes written to the output stream.
- Parameters:
stream – [in] output stream to write to
-
virtual std::streamoff load(std::istream &stream) override#
Loads this object from the given stream.
Returns the number of bytes read from the input stream.
- Parameters:
stream – [in] input stream to read from
Public Static Functions
-
static DoubleTensor getSimulatedInputsUniformSimpleLR(double phi0, double phi1, double minX, double maxX, int numSamples, int seed = 17)#
Get simulated (fabricated) random inputs nicely modeled by Linear-Regression where the X values are distributed uniformly in a given range.
This method is useful for testing the LR class.
- Parameters:
phi0 – The intercept of the Linear-Regression model
phi1 – The slope of the Linear-Regression model
minX – Bottom of range of X values to simulate
maxX – Top of range of X values to simulate
numSamples – Number of samples generated
seed – Random seed for consistent samples in test
-
static void getSimulatedInputsNormalMultipleLR(DoubleTensor &xTrain, DoubleTensor &yTrain, const std::vector<double> &phi, double meanX, double stdX, int numSamples, double stdErr = 0.05, int seed = 17)#
Get simulated (fabricated) random inputs nicely modeled by Linear-Regression where the X values are distributed normally with a given mean and standard-deviation.
This method is useful for testing the LR class.
- Parameters:
xTrain – The X values will be stored here
yTrain – The y values will be stored here
phi – The coefficients of the linear regression model. First coefficient is the constant term (which is not multiplied by any input variable).
meanX – Mean of normally distributed X values
stdX – Standard-deviation of normally distributed X values
numSamples – Number of samples generated
stdErr – The standard deviation of the error of y
seed – Random seed for consistent samples in test
-
static DoubleTensor getSimulatedInputsNormalSimpleLR(double phi0, double phi1, double meanX, double stdX, int numSamples, int seed = 17)#
Get simulated (fabricated) random inputs nicely modeled by Linear-Regression where the X values are distributed normally with a given mean and standard-deviation.
This method is useful for testing the LR class.
- Parameters:
phi0 – The intercept of the Linear-Regression model
phi1 – The slope of the Linear-Regression model
meanX – Mean of normally distributed X values
stdX – Standard-deviation of normally distributed X values
numSamples – Number of samples generated
seed – Random seed for consistent samples in test
-
static void packDoubleTensor(CTileTensor &out, const DoubleTensor &doubleTensor)#
Pack an input sample DoubleTensor into a CTileTensor in the correct packing for the estimator.
The result can then be added to the training set using addInput or to the sample vector param of fitMultipleLinearRegression.
- Parameters:
out – The output CTileTensor
doubleTensor – The input DoubleTensor
-
inline LinearRegressionEstimator(const HeContext &he)#