File:Linear least squares2.svg

Original file (SVG file, nominally 154 × 179 pixels, file size: 80 KB)

Summary

Description
English: Illustration of linear least squares.
Date
Source File:Linear least squares2.png
Author Krishnavedala
SVG development
InfoField
 
The SVG code is valid.
 
This plot was created with Matplotlib.
Source code
InfoField

Python code

Source code
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
from scipy.special import erfinv

# Set up the grid and other parameters
M, N = 50, 100
A, B = -2.2, 7
C, D = -4, 4
a, b, c = 1., 3, 1
p, q, r, s = 1, 7.5, 1.3, 0.16
p = s*p
q = s*q
r = s*r

X = np.linspace(A, B, N, dtype=np.float)
XX = np.linspace(A, B, N 1, dtype=np.float)
YY = p   q * XX   r * (XX**2)

Xr = 7.0 * np.arange(N, dtype=np.float)
Yr = Xr.copy()
for i in range(N):
    rd = np.random.rand()
    Xr[i] = XX[i] * rd   XX[i 1] * (1 - rd)
    Yr[i] = p   q * XX[i]   r * (XX[i]**2)   0.4 * erfinv(2*np.random.rand() - 1)
    
# least squares fitting
Mat = np.array([(0*Xr 1), Xr, Xr**2]).T
V = np.matmul(Mat.T, Yr)
V = np.linalg.solve(np.matmul(Mat.T, Mat), V)
pe = V[0]; qe = V[1]; re = V[2];

fig, ax = plt.subplots(1, 1, figsize=(2,2), edgecolor='k')

ax.plot(X, pe   qe * X   re * (X**2), 'b', lw=2)
myrad = 0.05
for i in range(len(Xr)):
    sample = Circle((Xr[i], Yr[i]), myrad, color='r')
    ax.add_patch(sample)

ax.grid(True)
ax.set_xticks([-2, -1, 0, 1, 2])
ax.set_yticks([-1, 0, 1, 2, 3])
ax.axis('square')
ax.axis([-2, 2, -1.7, 3.5])
fig.savefig('least squares.svg', transparent=True, bbox_inches='tight')

Licensing

Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

13 July 2018

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current16:12, 13 July 2018Thumbnail for version as of 16:12, 13 July 2018154 × 179 (80 KB)KrishnavedalaUser created page with UploadWizard

Global file usage

The following other wikis use this file:

Metadata