<commit_before><?php

namespace mcordingley\Regression\Algorithm;

use InvalidArgumentException;
use mcordingley\LinearAlgebra\Matrix;
use mcordingley\Regression\Observations;

final class LeastSquares implements Algorithm
{
    /** @var float */
    private $lambda;

    /**
     * @param float $lambda
     */
    public function __construct($lambda = 0.0)
    {
        $this->lambda = $lambda;
    }

    /**
     * @param Observations $observations
     * @return array
     * @throws InvalidArgumentException
     */
    public function regress(Observations $observations)
    {
        $design = new Matrix($observations->getFeatures());
        $observed = (new Matrix([$observations->getOutcomes()]))->transpose();

        if ($design->getRowCount() < $design->getColumnCount()) {
            throw new InvalidArgumentException('Not enough observations to perform regression. You need to have more observations than explanatory variables.');
        }

        $designTranspose = $design->transpose();

        $penalty = Matrix::identity($design->getColumnCount())->multiplyScalar($this->lambda);

        $prediction = $designTranspose
            ->multiplyMatrix($design)
            ->addMatrix($penalty)<commit_msg>Remove regularization from LeastSquares until an elegant way of doing it can be found.<commit_after><?php

namespace mcordingley\Regression\Algorithm;

use InvalidArgumentException;
use mcordingley\LinearAlgebra\Matrix;
use mcordingley\Regression\Observations;

final class LeastSquares implements Algorithm
{
    /**
     * @param Observations $observations
     * @return array
     * @throws InvalidArgumentException
     */
    public function regress(Observations $observations)
    {
        $design = new Matrix($observations->getFeatures());
        $observed = (new Matrix([$observations->getOutcomes()]))->transpose();

        if ($design->getRowCount() < $design->getColumnCount()) {
            throw new InvalidArgumentException('Not enough observations to perform regression. You need to have more observations than explanatory variables.');
        }

        $designTranspose = $design->transpose();

        $prediction = $designTranspose
            ->multiplyMatrix($design)
{'fraction_non_alphanumeric': 0.11230907457322552, 'fraction_numerical': 0.0008984725965858042, 'mean_word_length': 3.3838582677165356, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987027', 'n_tokens_mistral': 587, 'n_tokens_neox': 559, 'n_words': 140}