I have attached the necessary files including the drv.py as a basis for the code itself. The code should be in .py format but also include a png or jpeg of the distribution plot in the solution! Thank you!
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Mar 5 13:50:40 2021 @author: rachlin """ import random as rnd import matplotlib.pyplot as plt from collections import Counter import seaborn as sns class DRV: def __init__(self, dist = {}, trials=10000): """ Constructor """ self.dist = dist DRV.trials = trials def add_value(self, x, p): """ Add a value to the DRV with probability p """ self.dist[x] = p def P(self, x): """ Get the probability associated with the value x """ return self.dist.get(x, 0) def random(self): """ return a random value in accordance with the DRV probability distribution """ return rnd.choices(list(self.dist.keys()), list(self.dist.values()))[0] def E(self): """ Expected value """ ev = 0.0 for x,p in self.dist.items(): ev += x * p return ev @staticmethod def toDRV(vals): """ Convert a series of values to the corresponding discrete random variable """ c = Counter(vals) total = sum(c.values()) dist = {k:v/total for k, v in c.items()} return DRV(dist) def __add__(self, other): """ Add two discrete random variables """ return DRV.toDRV([self.random() + other.random() for i in range(DRV.trials)]) def __radd__(self, a): """ Add a scalar, a, by the DRV """ return DRV.toDRV([a + self.random() for i in range(DRV.trials)]) def __sub__(self, other): """ Subtract two discrete random variables """ return DRV.toDRV([self.random() - other.random() for i in range(DRV.trials)]) def __rsub__(self, a): """ Subtract discrete random variable from scalar """ return DRV.toDRV([a - self.random() for i in range(DRV.trials)]) def __mul__(self, other): """ Multiply two discrete random variables """ return DRV.toDRV([self.random() * other.random() for i in range(DRV.trials)]) def __rmul__(self, a): """ Multiply a scalar, a, by the DRV """ return DRV.toDRV([a * self.random() for i in range(DRV.trials)]) def __truediv__(self, other): """ Divide two discrete random variables (TODO: Support scalar) """ return DRV.toDRV([self.random() / other.random() for i in range(DRV.trials)]) def __pow__(self, other): """ Multiply two discrete random variables (TODO: Support scalar) """ return DRV.toDRV([self.random() ** other.random() for i in range(DRV.trials)]) def __repr__(self): """ String representation of the DRV """ xp = sorted(list(self.dist.items())) rslt = '' for x,p in xp: rslt += str(round(x)) + ' : '+ str(round(p,5)) + '\n' return rslt def plot(self, title='', xscale='', yscale='', trials=10000, bins=20, show_cumulative = True): """ Display the DRV distribution """ sample = [self.random() for i in range(trials)] sns.displot(sample, kind='hist', stat='probability', bins=bins) plt.title(title) plt.xlabel('value') plt.grid() if xscale == 'log': plt.xscale(xscale) if yscale == 'log': plt.yscale(yscale) if show_cumulative: plt.yticks([0.0, 0.25, 0.50, 0.75, 1.00]) xp = sorted(list(self.dist.items())) xval = [t[0] for t in xp] pval = [t[1] for t in xp] totalp = 0.0 pcumul = [] for p in pval: totalp += p pcumul.append(totalp) sns.lineplot(x=xval, y=pcumul) plt.show() Microsoft Word - ds2500_lab_drake.docx DS 2501: Intermediate Programming with Data / Lab Practicum Prof. Rachlin Northeastern University The Drake Equation Are we alone in the universe? Or are there other intelligent alien civilizations within our galaxy with whom we might someday communicate, and if so – how many? The Drake Equation, due to the American astronomer and astrophysicist Frank Drake, is an equation written in terms of probabilities. It is used to estimate how many actively communicating extraterrestrial civilizations there are in the Milky Way galaxy. As you will see, some of the terms in the equation can be estimated based on known physical properties of our galaxy while other factors are purely guesswork – and YOU will be doing the guessing! The drake equation is: The outcome, N, is the estimated number of civilizations in our galaxy capable of interstellar communication. The other terms are: Term Meaning Estimate, if possible R* star formation rate 1.5 – 3.0 fp fraction of stars that have planets Around 1.0 ne among stars having planets, how many of those planets can support life 1 – 5 f1 fraction of life-supporting planets that actually develop life ??? fi fraction of planets with life that develop intelligent life ??? fc fraction of intelligent life-bearing planets that develop technology that releases detectable signals (such as radio waves) into space ??? L the length of time (years) during which such civilizations release detectable signals into space. In other words, how many years do advanced civilizations last? ??? Step 1. Guess + Plot. The starter code is the Discrete Random Variable (DRV) class we put together in lecture on Tuesday. Each term listed above is a DRV, and has some probability associated with each numerical outcome. Create a separate file called drake.py which imports the DRV class. You perform the import by placing drv.py and drake.py in the same folder and then adding: from drv import DRV at the top of your drake.py program. Create one DRV object for each of the Drake equation terms. For each discrete random variable, define possible outcome values and their associated probability. Although we have some estimates for certain values, we are just guessing on many of these terms - just like Drake did himself! Calculate the Drake equation estimate by multiplying all your factors together, and then calling DRV’s plot method to visualize the whole thing. Be sure to give your output plot a title. This is an optional named parameter that you can specify when you call the plot method. In addition, you might get better results by: • Turning off the cumulative plot feature (show_cumulative=False) • Increasing the number of random trials to 100,000: (trials=100000) • Changing the probability scaling to logarithmic (yscale=’log’) Step 2. How Many Advanced Civilizations in our Galaxy? Use the E method to compute the expected value of the random variable resulting from multiplying all of the Drake equation factors together. Report this result as a triple-quoted comment in your code. What to Submit 1. drake.py containing a triple-quoted comment of your best-guess for the expected number of intelligent civilizations in the milky-way galaxy. You do not need to submit drv.py. 2. Your distribution plot in .png, .jpg, or .pdf format.