wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

"Can Lisp do Machine Learning?"

profile
Abhinav Kumar
Jul 16, 2025
0 Likes
0 Discussions
1 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

The Rise of Evil Twin Attacks: A New Kind Of Spoofing Cyberattack

Blog banner

OPERATING SYSTEM

Blog banner

Explain DBMS in Brief

Blog banner

Artical on FreshBooks

Blog banner

Multiprocessor and Multicore Organization

Blog banner

ITIL Version 3 and 4 differenciation?

Blog banner

Why Moms Are Choosing Customised School Accessories Over Generic Products?

Blog banner

?What Children Learn Between Activities: The Hidden Learning Moments in a Preschool Day

Blog banner

Introduction to GIS

Blog banner

Memory Management

Blog banner

What is Packet Filtering?

Blog banner

Ghee vs. Coconut Oil vs. Mustard Oil: Which Cooking Fat Wins for Indian Food?

Blog banner

Memory management and virtual memory

Blog banner

The Dark Web: A Breeding Ground for Cybercriminals – How to Guard Against Threats

Blog banner

File sharing

Blog banner

Why Data Privacy Is Changing Online Advertising

Blog banner

File and File System Structure

Blog banner

Fashion marketing in india

Blog banner

Fault Tolerance in an Operating System

Blog banner

Uniprocessor Scheduling

Blog banner

Are Social Media Paid Campaigns Worth It?

Blog banner

ahh wait a min

Blog banner

A Complete Guide to Satvik Indian Powder Masalas and Their Everyday Uses

Blog banner

What is OS Fingerprinting?

Blog banner

File Management In OS

Blog banner

Interrupts

Blog banner

Hubspot

Blog banner

Mesh Topology

Blog banner

operating system

Blog banner

10 Amazing facts about Tokyo Ghoul

Blog banner

DIGITAL ECONOMY

Blog banner

Development Of Modern Operating System

Blog banner

OS ASSIGNMENT

Blog banner

Memory Management

Blog banner

Install Ubuntu Easily

Blog banner

LiquidPlanner

Blog banner

Save Environment

Blog banner

Instagram Features in 2023 That Will Leave You Stunned!

Blog banner

Uniprocessor and Types

Blog banner

Memory input output management

Blog banner

Understanding Loneliness: Why We Feel Disconnected in a Connected World (Part -1)

Blog banner

The Power of Cyber Forensic in Solving Crimes

Blog banner