AI Obse-2
AI Obse-2
DATE FACTORIAL
AIM:
To implement the Python program to find the factorial of number using the
three methods such that :-
Looping method
Recursion method
Stiriling’s approximation
ALGORITHM:
# method 1: Looping :
# method 2 : Recursion :
AIM:
To implement the Python program to get the fibonacci sequence using recursion
and iteration also check Golden ratio with the fibonacci sequence.
ALGORITHM:
Iterative Approach
Initialize variables a,b to 1
Initialize for loop in range[1,n)
Compute next number in series; total = a+b
Store previous value in b
Store total in a
Recursive Approach
If n equals 1 or 0; return 1
Else return fib(n-1) + fib(n-2)
Golden ratio (golden number = 1.618033)
Golden ratio will divide the (fn+1)/f to get the golden ratio number in
Fibonacci
The return result of the calculation will be same or nearer to the golden ratio
RESULT:
The Python program for the fibonacci is sequence and Golden Ratio from
the Fibonacci sequence has been implemented.
EX NO 02
QUADRATIC EQUATION
DATE
AIM:
The Python program to find the quadratic roots of the given equation
ALGORITHM:
Start
Read the values of a, b, c
Compute d = b2 – 4ac
If d > 0
calculate root1 = {-b + ?(b2 – 4ac)}/2a
calculate root2 = {-b – ?(b2 – 4ac)}/2a
else If d = 0
calculate root1 = root2 = (-b/2a)
else
calculate root1 = {-b + i?-(b2 – 4ac)}/2a
calculate root2 = {-b + i?-(b2 – 4ac)}/2a
print root1 and root2
End
RESULT:
The quadratic roots using Python program has been implemented and verified the equation X^2-X-1 = 0 provide
the golden ratio as positive roots
while pq:
(f, g, current, path) = heapq.heappop(pq)
# Goal check
if current == goal:
return path, g
visited.add(current)
# Expand neighbors
for cost, neighbor in self.edges[current]:
if neighbor not in visited:
new_g = g + cost
new_f = new_g + self.h[neighbor]
#Sample input:
RESULT:
The A* search algorithm successfully found the shortest path from node 'A' to node
'G' in the given graph by intelligently combining actual path costs with heuristic
estimates. This example demonstrates the effectiveness of A* in navigating complex
graphs efficiently.
EX NO-
03 DATE EXPLORE THE BASIC PROBABILITY FUNCTIONS
AIM:
The aim of this program is to generate random samples from different statistical
distributions (Normal, Bernoulli, Discrete Uniform, and Continuous Uniform) and
visualize their properties by plotting histograms of the generated samples.
ALGORITHM:
1. Normal Distribution
Input Parameters:
Input Parameters:
PROGRAM:
1. Normal Distribution
import numpy as np
import numpy as np
RESULT:
The generated distributions illustrate different probability scenarios: Normal shows
a bell curve where values cluster around the mean; Bernoulli models binary
outcomes based on a success probability. Discrete Uniform gives equal likelihood to
integers within a range, while Continuous Uniform evenly spreads probability across
a continuous interval.
EX NO-04
AIM:
The aim of calculating the posterior probability in the given problem is to
determine the probability that a person actually has the rare disease given that they
have tested positive for it.
ALGORITHM:
1. Define Prior Probabilities:
o Establish the initial probabilities of the events of interest, in this case,
the probability of having the disease (p_disease) and not having the
disease (p_no_disease).
6. Plot:
o Visualize the joint probability distribution using a bar plot to gain a better
understanding of the relationships between the events and their
probabilities.
PROGRAM:
p_disease = float(input("Enter the overall probability of having the disease : "))
p_pos_given_disease = float(input("Enter the probability of testing positive given
having the disease : "))
p_neg_given_no_disease = float(input("Enter the probability of testing negative given
not having the disease : "))
p_no_disease = 1 - p_disease
OUTPUT:
import matplotlib.pyplot as plt
df.plot(kind='bar', figsize=(8, 6))
plt.title('Joint Probability Distribution')
plt.ylabel('Probability')
plt.xlabel('(Test Result, Disease Status)')
plt.xticks(rotation=0)
plt.tight_layout()
plt.show()
RESULT:
The posterior probability provides the updated likelihood of having the disease given
a positive test result. It combines prior knowledge about the disease prevalence with
the test's accuracy.
EX NO- BAYESIAN NETWORK: IMPLEMENTING CONDITIONAL
PROBABILITY DISTRIBUTIONS
05 DATE
AIM:
To implement a Bayesian Network using pgmpy for modeling dependencies
between random variables, define Conditional Probability Distributions (CPDs),
check model consistency, and visualize the network.
ALGORITHM:
1. Define the Bayesian Network Structure:
• Create a Bayesian Network and add nodes representing variables
like Low CW, Unknown SR, RT-too high, P too high, and Alarm.
• Add directed edges between nodes to indicate dependencies.
2. Define Conditional Probability Distributions (CPDs):
• Define CPDs for each variable:
o Low CW: Represents the probability of Low CW being true or false.
o Unknown SR: Represents the probability of Unknown SR being true or
false.
o RT-too high: Probability based on Low CW and Unknown SR.
o P too high: Probability based on Blockage.
o Alarm: Probability based on RT-too high and P too high.
3. Add CPDs to the Model:
• Add each CPD to the Bayesian Network model.
4. Check Model Consistency:
• Validate the model to ensure the network is correctly structured
with consistent CPDs.
5. Visualize the Bayesian Network:
• Use NetworkX to visualize the Bayesian Network.
6. Print the CPDs:
• Output the CPDs for each variable to understand the defined probabilities.
PROGRAM:
import networkx as nx
import matplotlib.pyplot as plt
OUTPUT:
from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
RESULT:
The Bayesian Network was successfully implemented, with defined
CPDs and verified model consistency. The network's structure and CPDs were
visualized, demonstrating the dependencies between variables effectively.