<jupyter_start><jupyter_text>Sets<jupyter_code># A set of numbers
set([1,2,30])
# order is not presenrved
# When printing out a set, the elements will be listed 
# A set of strings
set(['this','that','the other'])
# each element can appear in a set only once.
set([1,2,3,2])
# Create an empty set
A = set()
# Add elements from a set
A = set()
A.update({0, 10})
print(A)
# Remove a random element from a set
A = {1, 2, 3}
A.pop()
print(A)
# Delete an element from a set
A = {1, 2, 3}
A.remove(2)       # Works only if the element to be deleted is present in the set
A.discard(4)      # Works even if the element to be deleted is not present in the set
A.discard(3)      
print(A)
A = {-1,2,1, 3}
A.pop()
print(A)
# Get a sorted list from a set
A = {1, 10, 4, -9, 7, 8, -6, 3, 2}
print(sorted(A))<jupyter_output>[-9, -6, 1, 2, 3, 4, 7, 8, 10]<jupyter_text>Operation on Sets<jupyter_code>A=set(range(0,3)) # all integers between 0 and 2
B=set(range(0,6,2)) # even integers between 0 and 2
C=set(range(0,6))   # all integers between 0 and 5
'A=',A,'B=',B,'C=',C
## Checking if an element is in a set:
1 in A, 1 in B, 3 not in B
A.issubset(C), A<=C
C.issuperset(B),C>=B
A.union(B),A | B
A.intersection(B), A&B
# The difference between A and B contains all elements that are in A but not in B
A.difference(B), A-B
# The symetric difference contains all elements that are in one of the two sets, but not in both
A.symmetric_difference(B), A^B<jupyter_output><empty_output><jupyter_text>Probability<jupyter_code>#Probability of getting two heads for a coin tossed twice 
S = {'HH', 'HT', 'TH', 'TT'}
E= {'HH'}
P = len(E)/len(S)
print(P)
#Probability of drawing aces from a deck of card
cards = 52
aces = 4

# Divide possible outcomes by the sample set
ace_probability = aces / cards

# Print probability rounded to two decimal places
print(round(ace_probability, 2))
# Sample Space
cards = 52

# Calculate the probability of drawing a heart or a club
hearts = 13
clubs = 13
heart_or_club = (hearts/cards) + (clubs/ cards)
print(heart_or_club)
# Sample Space
cards = 52

# Outcomes
aces = 4

# Probability of one ace
ace_probability = aces / cards

# Probability of two consecutive independant aces 
two_aces_probability = ace_probability * ace_probability

# Two Ace Probability Percent Code
two_ace_probability_percent = two_aces_probability * 100
print(round(two_ace_probability_percent, 1))
#Probability of getting 8 on sum of two dice thrown
count=0
total=0
for i in range(1,7):
  for j in range(1,7):
    a= i+j
    total= total+1
    if(a==8):
      count=count+1

Pr = count/total
print(Pr)<jupyter_output>0.1388888888888889<jupyter_text>Simulating Probablity of Coin Toss<jupyter_code>import random
def coin_trial():
  heads = 0
  for i in range(100):
    if random.random() <= 0.5:
      heads +=1
  return heads

def simulate(n):
  trials = []
  for i in range(n):
    trials.append(coin_trial())
  return(sum(trials)/n)
simulate(10)
simulate(1000)
simulate(1000000)<jupyter_output><empty_output><jupyter_text>Conditional ProbabilityIn a group of 100 sports car buyers, 40 bought alarm systems, 30 purchased bucket seats, and 20 purchased an alarm system and bucket seats. If a car buyer chosen at random bought an alarm system, what is the probability they also bought bucket seats?<jupyter_code>import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# Use the venn2 function
venn2(subsets = (40, 30, 20), set_labels = ('Alarm System', 'Bucket Seat'))
plt.show()
S=100
pr_AB= 20/S
pr_A= 40/S

Pr_B_given_A = pr_AB/pr_A
print(Pr_B_given_A)<jupyter_output><empty_output><jupyter_text>Probability Distribution Uniform Distribution<jupyter_code># import uniform distribution
from scipy.stats import uniform

# random numbers from uniform distribution
n = 10000
start = 10
width = 20
data_uniform = uniform.rvs(size=n, loc = start, scale=width)
import seaborn as sns
ax = sns.distplot(data_uniform,
                  bins=100,
                  kde=True,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Uniform Distribution ', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Normal Distribution<jupyter_code>from scipy.stats import norm
# generate random numbers from N(0,1)
data_normal = norm.rvs(size=10000,loc=0,scale=1)
ax = sns.distplot(data_normal,
                  bins=100,
                  kde=True,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Normal Distribution', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Gamma Distribution<jupyter_code>from scipy.stats import gamma
data_gamma = gamma.rvs(a=5, size=10000)
ax = sns.distplot(data_gamma,
                  kde=True,
                  bins=100,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Gamma Distribution', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Exponential Distribution<jupyter_code>from scipy.stats import expon
data_expon = expon.rvs(scale=1,loc=0,size=1000)
ax = sns.distplot(data_expon,
                  kde=True,
                  bins=100,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Exponential Distribution', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Poisson Distribution<jupyter_code>from scipy.stats import poisson
data_poisson = poisson.rvs(mu=3, size=10000)
ax = sns.distplot(data_poisson,
                  bins=30,
                  kde=False,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Poisson Distribution', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Binomial Distribution<jupyter_code>from scipy.stats import binom
data_binom = binom.rvs(n=10,p=0.8,size=10000)
ax = sns.distplot(data_binom,
                  kde=False,
                  color='skyblue',
                  hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Binomial Distribution', ylabel='Frequency')<jupyter_output>/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2557: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Bernoulli's Distribution<jupyter_code>from scipy.stats import bernoulli
data_bern = bernoulli.rvs(size=10000,p=0.6)
ax= sns.distplot(data_bern,
                 kde=False,
                 color="skyblue",
                 hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Bernoulli Distribution', ylabel='Frequency')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11130762881735644, 'fraction_numerical': 0.032071689659238296, 'mean_word_length': 4.226124460874923, 'pattern_counts': {'":': 7, '<': 43, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '172720', 'license': 'MIT', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2768, 'n_words': 829, 'path': 'maths/Probability_and_Statistics.ipynb', 'repo_name': 'vedantbahel/Python', 'size': '8481'}