wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

"Can Lisp do Machine Learning?"

profile
Abhinav Kumar
Jul 16, 2025
0 Likes
0 Discussions
11 Reads

Linear Regression is the simplest form of predictive modeling. It assumes a straight-line relationship between two variables. For example:

More study hours → Higher exam scores

This relationship can be modeled as

y=mx+c


Where:

m: Slope (how fast y changes when x changes)

c: Intercept (the starting point of the line)

Given some data, Linear Regression helps us find m and c automatically.


1)

First, we need to calculate the average of a list of numbers.

(defun mean (lst)

(/ (reduce #'+ lst) (length lst)))


2)

Variance measures how spread out a list of numbers is.

(defun variance (lst)

(let ((m (mean lst)))

(mean (mapcar (lambda (x) (expt (- x m) 2)) lst))))


3)

Covariance tells us how two variables change together.

(defun covariance (x y)

(let* ((mean-x (mean x))

(mean-y (mean y))

(n (length x))

(sum 0))

(dotimes (i n)

(incf sum (* (- (nth i x) mean-x)

(- (nth i y) mean-y))))

(/ sum n)))


4)

This is where we calculate our model’s intercept and slope (our c and c).

(defun linear-regression-coeffs (x y)

(let ((b1 (/ (covariance x y) (variance x))))

(let ((b0 (- (mean y) (* b1 (mean x)))))

(list b0 b1))))


5)

Finally, given x, this function predicts y using our model.

(defun predict (model x)

(+ (first model) (* (second model) x)))



6)

(defparameter *hours* '(1 2 3 4 5 6))

(defparameter *scores* '(50 55 65 70 75 85))


(defparameter *model* (linear-regression-coeffs *hours* *scores*))


(format t "Intercept: ~f, Slope: ~f~%" (first *model*) (second *model*))

(format t "Prediction for 7 hours: ~f~%" (predict *model* 7))


7) Output

Intercept: 42.666668, Slope: 6.857143

Prediction for 7 hours: 90.666668


Intercept: 42.66 → Predicted score if study hours were zero.

Slope: 6.85 → Each extra hour of study increases the score by ~6.85 points.

Prediction for 7 hours: Expected score ~90.66


Comments ()


Sign in

Read Next

What Function Does SEO Serve in Digital Marketing?

Blog banner

Functions Of Operating Systems

Blog banner

Blockchain in IoT Applications

Blog banner

Paging

Blog banner

Practical Implementation of Client Server model using TCP/IP.

Blog banner

COMFORT IS ALL ABOUT FASHION

Blog banner

Install Ubuntu Easily

Blog banner

Threads and concurrency

Blog banner

The Procedural Framework for Corporate High-Tech Investigations

Blog banner

Dal Bafla Recipe

Blog banner

Deadlock

Blog banner

File Organization and Access

Blog banner

?Your Guide to Great Flossing, by the best affordable dentist in Maroubra

Blog banner

Use It or Lose It: Why November Is the Best Time to Claim Your Dental Extras and Medicare CDBS Before Year-End

Blog banner

Raid and levels of raid.

Blog banner

Classification Algorithms (Decision trees, SVM, Logistic regreession)

Blog banner

Binary Search Tree (BST) in Data Structure

Blog banner

Sweet and Sour Mango Pickle (Gol Keri)

Blog banner

Secure Hypertext transfer protocol

Blog banner

Predictive Analysis - Ek Overview

Blog banner

Navigation With Indian Constellation(NavIC) by ISRO in Geographic Information Systems

Blog banner

What is Amazon?

Blog banner

Climate Anxiety: Understanding the Psychological Impact of a Changing World

Blog banner

Direct Memory Access

Blog banner

Footprinting

Blog banner

Starvation

Blog banner

How User Data Shapes Personalised Campaigns

Blog banner

What is service level Agreement?

Blog banner

Importance of Education

Blog banner

To travel is to live

Blog banner

History of Money

Blog banner

How Men and Women Process Pain Differently

Blog banner

File management

Blog banner

Apple

Blog banner

SESSION HIJACKING

Blog banner

Digital Forensics Challenges and Tools

Blog banner

What Are Electro-Forged Gratings? Benefits, Applications, and Why Industries Prefer Them

Blog banner

Cryptanalysis tool

Blog banner

Modern Operating System - Suren Kotian

Blog banner

How Traditional Indian Foods Can Support a Modern Lifestyle

Blog banner

Cyber-crime Investigation and Future Directions

Blog banner

The Bold Digital Marketing Moves That Made Durex India’s Second-Largest Condom Brand

Blog banner