import Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()HW 1 Solutions
If you are enrolled in the course, make sure that you use the GitHub Classroom link provided in Ed Discussion, or you may not be able to get help if you run into problems.
Otherwise, you can find the Github repository here.
Load Environment
The following code loads the environment and makes sure all needed packages are installed. This should be at the start of most Julia scripts.
Standard Julia practice is to load all needed packages at the top of a file. If you need to load any additional packages in any assignments beyond those which are loaded by default, feel free to add a using statement, though you may need to install the package.
using Random
using Plots
using GraphRecipes
using LaTeXStrings
using Distributions# this sets a random seed, which ensures reproducibility of random number generation. You should always set a seed when working with random numbers.
Random.seed!(1)TaskLocalRNG()
Problems (Total: 30 Points)
Problem 1 (12 points)
Problem 1.1
function minimum(array)
# initialize the minimum value counter
min_value = 0
# update minimum values
for i in 1:length(array)
if array[i] < min_value
min_value = array[i]
end
end
# return found minimum
return min_value
end
array_values = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
@show minimum(array_values);minimum(array_values) = 0
Solution:
The code initializes the minimum value (min_value) at 0, which is below any of the values in array_values. Instead, if we initialize min_value = array[1], any smaller values will be identified by the loop.
A correct solution might look like:
function minimum(array)
# initialize the minimum value counter
min_value = array[1]
# update minimum values
for i in 2:length(array)
if array[i] < min_value
min_value = array[i]
end
end
# return found minimum
return min_value
end
array_values = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
@show minimum(array_values);minimum(array_values) = 78
Problem 1.2
Your team is trying to compute the average grade for your class, but the following code produces an UndefVarError.
# enter student grade vector
student_grades = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
# compute class average
function class_average(grades)
average_grade = mean(student_grades)
return average_grade
end
@show average_grade;UndefVarError: `average_grade` not defined in `Main.Notebook` Suggestion: check for spelling errors or missing imports. Stacktrace: [1] macro expansion @ show.jl:1232 [inlined] [2] top-level scope @ ~/Teaching/BEE4750/website/solutions/hw01/hw01.qmd:142
Solution:
The UndefVarError is due to a problem with scope. The function class_average() is defined with an input grades, while student_grades is a global variable defined outside the function, and therefore is not available to the function. Additionally, average_grade is a variable defined within the class_average() function, and therefore is not accessible outside of its operation. If we replace student_grades inside the class_average() function with grades, and replace average_grade with the output of the function, the code works as intended:
# enter student grade vector
student_grades = [89, 90, 95, 100, 100, 78, 99, 98, 100, 95]
# compute class average
function class_average(grades)
average_grade = mean(grades)
return average_grade
end
@show class_average(student_grades);class_average(student_grades) = 94.4
Problem 1.3
Your team wants to know the expected payout of an old Italian dice game called passadieci (which was analyzed by Galileo as one of the first examples of a rigorous study of probability). The goal of passadieci is to get at least an 11 from rolling three fair, six-sided dice. Your strategy is to compute the average wins from 1,000 trials, but the code you’ve written below produces a MethodError.
# function to simulate a passadieci roll: 3 6-sided dice
function passadieci()
# this rand() call samples 3 values from the vector [1, 6]
roll = rand(1:6, 3)
return roll
end
# set number of trials and initialize outcome vector
n_trials = 1_000
outcomes = zero(n_trials)
# simulate number of passadieci rolls and count wins
for i = 1:n_trials
outcomes[i] = (sum(passadieci()) > 11)
end
win_prob = sum(outcomes) / n_trials # compute average number of wins
@show win_prob;MethodError: no method matching setindex!(::Int64, ::Bool, ::Int64) The function `setindex!` exists, but no method is defined for this combination of argument types. Stacktrace: [1] top-level scope @ ~/Teaching/BEE4750/website/solutions/hw01/hw01.qmd:178
Solution:
The line producing the MethodError is outcomes[i] = (sum(passadieci()) > 11). The problem is actually with a prior line, outcomes = zero(n_trials), which creates outcomes.
zero() is the Julia function to get a zero element (the additive identity) based on the type of the input. For example:
zero(3)0
zero(3.0)0.0
zero(false)false
Since zero() returns a scalar element (as seen in the examples), trying to index its output with outcomes[i] results in the error, as Julia does not know how to index into a scalar (in this case, an Int).
However, the goal of the relevant line in the script is to initialize a vector consisting of zeros with length n_trials. What we actually want is the function zeros(). Fixing this gets rid of the error (I’ve also fixed another error with the code, namely the threshold for a “win”, but this isn’t essential for this problem):
# function to simulate a passadieci roll: 3 6-sided dice
function passadieci()
# this rand() call samples 3 values from the vector [1, 6]
roll = rand(1:6, 3)
return roll
end
# set number of trials and initialize outcome vector
n_trials = 1_000
outcomes = zeros(n_trials)
# simulate number of passadieci rolls and count wins
for i = 1:n_trials
outcomes[i] = (sum(passadieci()) >= 11)
end
win_prob = sum(outcomes) / n_trials # compute average number of wins
@show win_prob;win_prob = 0.502
Problem 1.4
You’re interested in writing some code to remove the mean of a vector from all of its components. You’ve written the following code and tried to test it on a random vector, but your code returns a MethodError.
# function to remove mean from a vector
function remove_mean(vect)
# fucntion to compute the mean
function compute_mean(vect)
element_sum = 0 # initialize sum
# compute mean and return
for v in vect
element_sum += v
end
return element_sum / length(vect)
end
m = compute_mean(vect) # compute mean
# return demeaned vector
return vect - m
end
random_vect = rand(1_000)
@show remove_mean(random_vect)MethodError: no method matching -(::Vector{Float64}, ::Float64)
For element-wise subtraction, use broadcasting with dot syntax: array .- scalar
The function `-` exists, but no method is defined for this combination of argument types.
Closest candidates are:
-(::Missing, ::Number)
@ Base missing.jl:123
-(::ChainRulesCore.NoTangent, ::Any)
@ ChainRulesCore ~/.julia/packages/ChainRulesCore/Vsbj9/src/tangent_arithmetic.jl:61
-(::Any, ::ChainRulesCore.NotImplemented)
@ ChainRulesCore ~/.julia/packages/ChainRulesCore/Vsbj9/src/tangent_arithmetic.jl:50
...
Stacktrace:
[1] remove_mean(vect::Vector{Float64})
@ Main.Notebook ~/Teaching/BEE4750/website/solutions/hw01/hw01.qmd:243
[2] macro expansion
@ show.jl:1232 [inlined]
[3] top-level scope
@ ~/Teaching/BEE4750/website/solutions/hw01/hw01.qmd:247
Solution:
The line that causes the error is return vect - m. The problem is that Julia does not make assumptions about how to handle ambiguous operations like adding or subtracting a scalar (m) from a vector (vect): is this intended and should be done element-wise, or is it a sign that the wrong type was passed (e.g. the other input was intended to be a vector)? To make the intent of the coder explicit, Julia asks that you broadcast an operation that is intended to be applied to a scalar (such as addition or subtraction of another scalar) to be applied element-wise to a vector, instead of assuming that this is the intended behavior.
To broadcast a function f() over a vector v, you use a period, as in f.(v). In this case, we want to change the problematic line to return vect .- m:
# function to remove mean from a vector
function remove_mean(vect)
# fucntion to compute the mean
function compute_mean(vect)
element_sum = 0 # initialize sum
# compute mean and return
for v in vect
element_sum += v
end
return element_sum / length(vect)
end
m = compute_mean(vect) # compute mean
# return demeaned vector
return vect .- m
end
random_vect = rand(1_000)
@show remove_mean(random_vect)remove_mean(random_vect) = [-0.402858193077954, 0.17163216613218168, -0.18169592947668356, 0.060514380686886415, -0.2591266252711959, -0.01737820236308285, 0.37319528099391985, 0.23362003830781186, -0.3090151132440434, -0.06824664473090836, 0.3127573063344131, -0.39730315107992675, 0.07450943943251365, 0.17821237477494634, -0.031914855823978905, 0.2901957389388936, -0.29439767231500735, -0.39428682614985444, 0.28469675398714955, -0.15391309414849141, 0.18410969908054953, 0.10996576619091203, 0.4366100026219155, 0.12668848258922027, -0.19376596222612996, 0.39947992422693535, -0.29291782437448555, 0.0258765406000393, -0.26110728751899714, -0.49303367510298224, -0.4484985564436411, 0.18580992405552932, -0.4685566065487725, 0.2636862043549746, -0.3541814667461207, 0.46525899668409465, 0.4703884817275342, 0.14350852326153452, -0.39614791770310454, -0.2798992807671117, -0.500126160487519, 0.13375959280637362, 0.37193108311289624, 0.25355217944131936, -0.2843864496790248, 0.012224016156722084, 0.13205819628704363, 0.2699631458717966, -0.09738083482648485, -0.004972894181016874, 0.035403776875871484, 0.4079923428745703, 0.3660979765026182, 0.12021281887755142, -0.15283894541499154, -0.2895101552372552, 0.21476847214638028, 0.05122452817691503, -0.08537778414937602, 0.48651378148007285, -0.13069691443287368, 0.2562240006516775, 0.04951558356723684, -0.45305538422747205, 0.3266682401280274, -0.16925127951957897, 0.1277561052153401, 0.0003124257316092782, 0.33699344650708285, 0.07053918286406191, 0.07005147339474094, -0.19242032455240377, -0.408799191343729, 0.33362472021211986, 0.42516025645145405, 0.1058953093117524, 0.0921584045170003, -0.21084946998478382, -0.17239838713324784, -0.07921068501683826, -0.18197644244128452, 0.05467204146300786, -0.21856433761397553, 0.12040924913802176, 0.38452814432605564, 0.3820728348817467, 0.24267733389221313, 0.13186654104854212, 0.11500785962705462, 0.4880041729043859, 0.3655924140736726, 0.18652740684109426, -0.4897461305379437, -0.33374979156751405, -0.4823085665341874, -0.2906461113335006, 4.9214506931183344e-5, -0.22401758208433176, 0.3783837634565499, 0.11381484861777025, -0.13765966072329183, 0.1111742892999994, -0.2431573000644226, -0.27403626850499574, -0.48905144253693733, 0.2650370002282074, 0.2025669334376008, -0.4054940587692938, -0.47634180551783334, -0.10826640761461437, 0.1966958302824514, 0.09716304115875296, -0.011148298948556956, -0.12395369966932623, 0.37546693427170397, 0.013969656126352592, 0.10034496273563676, -0.038130877335492364, -0.23814727226472954, 0.016681928814319846, 0.016791543774459794, -0.16159621915005296, 0.3867948947432899, 0.40165821510688415, -0.1989880370060504, -0.02765811424133413, 0.40265859923879355, 0.4312634133241757, 0.48618402221893275, -0.4427340713955007, -0.17758720126384664, 0.04295454622599826, 0.3037163983730492, -0.11051035241520135, 0.4294146279046267, 0.027056289745115203, 0.3277889276711953, -0.10966176941609784, 0.026564607970747667, 0.32241760672529296, -0.4485970345823841, -0.0025052998619673472, -0.1343068226534282, 0.35336190995654837, 0.34893697014898184, -0.004960471142808287, 0.23417983036396028, -0.3320799284664223, 0.45381399920698495, 0.15419905264480216, -0.22915384568748498, -0.2695306082511213, -0.26069977009484446, -0.11591073408387287, 0.04252048050371937, 0.25627298215981686, 0.13829985509394982, 0.151015477720973, -0.12425982457046247, -0.10736220130499863, -0.2177085450500551, -0.49224039090992844, -0.27267104788808505, 0.3889796313690156, -0.480527400206163, 0.10646494282934693, -0.1397261421539029, -0.10978190529752485, -0.25268092596053593, -0.3341549936636947, 0.29457245449202174, -0.37608349122816453, 0.054970254661387385, 0.3133867473210482, -0.21029831686972222, 0.026134631310289036, 0.2190096599004211, -0.14500043060497203, 0.311674587902948, 0.09150500835543951, -0.2209712629084878, -0.07078681859118041, 0.03733670365456343, -0.1284467818203845, -0.004342509760997482, 0.243358458827574, 0.4392589743781198, -0.42959049672747995, -0.17703241531660807, -0.4333957192958756, 0.3818577301327156, -0.3288516083433839, -0.11818655389345956, -0.28182587977318463, -0.2040942546900858, -0.13420030618419387, 0.3158173392949407, -0.10705205987877231, -0.05914463530799574, 0.2970736232951434, 0.1035899365766163, 0.16637858516060045, 0.08620088489919264, -0.2378296761048866, 0.058488177015326936, 0.299700872267159, -0.027523776230255703, -0.3829873751927352, -0.17388851281463247, -0.29973206587123424, -0.18218339077485624, -0.41562698464988557, -0.15413486536322807, -0.46794226582961296, 0.0073894873975163256, -0.49768177520853063, 0.35631509897369473, -0.49906811275182417, 0.43289391365107566, -0.3057305490390755, 0.24393474711929009, 0.4908356100021327, 0.48039908326671477, -0.16553185393556247, -0.203950597468342, -0.09928543310476046, 0.48292128323900896, 0.44925507106964735, 0.488853725875662, 0.0937823783164482, -0.1168579139162309, -0.13417038251855107, 0.009278885987414998, -0.0152854115941502, 0.13680062257447345, 0.11339036794833401, -0.10805672631550223, 0.10590113153523961, -0.489379938686351, 0.4545473620493651, -0.15992977814363263, -0.08426639200449748, -0.47994182525871887, 0.4688361216106677, 0.40051451256015547, -0.24722750624867806, 0.08646570465198367, -0.1345942785409493, -0.3259826244057906, -0.30137051540436943, 0.09896384324784635, -0.29776258365848585, -0.36504605221238795, 0.4303053567631693, 0.22929053702950852, -0.15954068772388974, 0.31506862276951575, -0.4340440154564136, 0.0532280634272041, -0.17664057901806607, 0.08261606538220923, -0.4619873734597173, 0.42911882077545205, -0.27156928198628183, -0.43638440672405465, 0.23520258590345955, -0.18080899805013206, 0.4896322631944141, -0.08788387336913872, 0.3305240239232071, 0.25654535431069003, -0.3669243460275252, 0.42551498153275646, -0.021656039486038003, 0.23343033481876507, -0.03488939099316746, -0.19827829069615566, 0.48090337293172314, -0.2546291161494506, 0.42925105243692574, -0.10008213176185687, -0.12477176552542713, 0.274633653841846, 0.16987969193067054, -0.3902097079466119, -0.35732614673032914, 0.422502217299055, 0.42608018690414984, -0.03412653744933647, 0.07029582858202976, -0.1487015254131968, -0.2356876283357232, -0.4658689755519547, -0.49085963677814637, 0.4092859508009855, 0.14047301353607766, 0.0016935111972344874, -0.43667492577631184, -0.1423326648344998, 0.24426558337402304, -0.4235372145536691, -0.48597760899112086, 0.20308282532751143, -0.018312760397877326, 0.16892582693058178, -0.35777654495911626, 0.035123043857314684, -0.13781924111593047, 0.2837671415242775, 0.10349289934512651, 0.4428265259808368, -0.43093700766173615, -0.05107530163635998, -0.22323987097753017, -0.3174030933872447, -0.31103920708334665, 0.3754905143358944, 0.22871296272973174, 0.42409111020069534, 0.4182371733454233, 0.22036213822383777, 0.2963592619360286, -0.3831702967872067, -0.4461530945416051, 0.013583403018980866, -0.1029954430450104, -0.43445914069472624, 0.12328232070181788, 0.0002699743363143714, -0.03517217938774575, 0.45352475431079076, -0.1243612034852345, -0.07724801316202201, 0.291895181103919, -0.20565626668108994, 0.0266208694827319, 0.0035863049050934848, 0.007905187525922064, 0.4484717657906725, 0.37229688930488625, 0.2484786828785347, 0.0967456464081684, -0.07916273937720042, 0.40084759250198765, -0.23696001367553732, 0.010763983427625523, -0.4345224276613425, 0.40197472235040255, -0.1882939896151672, 0.12816328225613127, -0.23960924434033792, 0.4553330525671522, 0.20056203318785604, 0.03416410526938296, 0.02513822217290007, 0.2800409277907696, -0.017335510761202988, 0.21947493557511144, -0.2853925261030378, 0.15642220162380593, -0.12374270621114836, 0.03131536569802451, 0.34521901349916195, -0.34700602013593174, 0.021064311381848055, -0.41841234419561757, 0.29300100425384823, -0.4410972857595644, 0.01278273859462653, 0.32547971247721785, -0.4508376360060483, -0.03872917854658864, -0.03604980029001459, 0.23881200602823338, -0.4300454623516089, -0.42695237980864253, -0.3538610160123361, -0.11591263278112074, 0.31026527272911497, 0.30902332223255047, 0.4950592024658612, 0.11976404894526593, -0.2753224747752321, -0.34712680344541935, -0.47647468713674, -0.14554512170223077, 0.13114992448247142, 0.32965893944561, 0.05530925357413641, -0.49261259866963547, -0.2120304538433616, 0.1406031916088668, 0.35760690957802765, -0.48605791988416525, 0.4869807092081564, 0.3648086995712694, -0.30395866603637733, -0.17904870833935582, -0.2421844769770808, 0.17191667373780484, -0.41717586572841237, -0.11103963092146463, 0.1456663732658432, 0.3617399853883456, 0.13505597284445703, 0.19785467393997624, 0.2964658497226418, 0.08731291820505105, 0.36347170054792866, 0.11175729224805486, -0.2887723943143107, -0.21652846147587368, -0.2826948739171149, 0.329935402402158, 0.4818471115938292, 0.372169213154539, 0.12879686959491565, 0.47211213323736156, 0.13775703766871106, 0.21322229885993793, 0.3905879164979339, -0.12387879785526867, -0.15674414377948154, 0.38041778859141906, 0.1398665666862252, -0.04738859791933914, -0.05471712151112129, -0.2817832250300766, -0.4391399834711991, 0.48076182168621884, 0.1977155072876008, 0.3721733877446871, 0.33226362975750867, -0.46937912050613606, -0.48262421277795475, 0.3143689309082074, -0.09415468169158414, -0.017858613527582556, 0.3336926349454147, -0.11609145674518118, 0.4972862648145163, 0.21351056961991188, -0.21492263789904398, -0.3302622179483292, -0.08086530565888839, 0.33735375181958027, 0.3119893618950629, 0.25864012717334395, 0.17007333754826104, -0.358083290117283, 0.21770316971794645, 0.24043999838814134, -0.27160454721728156, -0.28171544618841204, -0.34791946693740516, -0.3573793819646891, 0.45244900661750753, -0.261209251231061, -0.2908503507372129, 0.059480922040567985, -0.3711203745915165, -0.09403277484876349, 0.2491968920295835, 0.3296810559138833, -0.3953493304188298, -0.08920930088056989, -0.4162600289653846, 0.09171472378060885, 0.26516591706161974, 0.40313612245085306, -0.2227120239642445, -0.4855073265259233, -0.27745149335473784, -0.25512500287216244, 0.2645270115598012, 0.33363715673989647, -0.44707379189110474, 0.45226074152654927, -0.45973912530446226, -0.07859465190001835, 0.0072557282295524494, -0.49496169042510285, 0.0021786698035929364, -0.4463328434276893, -0.1467447588392865, -0.006753599854432579, 0.3865619187253134, -0.1728303766289937, 0.2723718930561203, -0.4007636044751658, -0.10391787713783118, 0.06635240257554964, 0.1079874844713119, 0.02947062435626535, 0.033288875601989454, -0.013139196349626125, 0.22069960963655244, -0.4743441512961879, 0.18580671854071928, 0.34960123830239276, -0.160416591552357, -0.2542588803175171, 0.17623669461616676, 0.01563059998621863, 0.38916471350839954, 0.03102768206637241, 0.06710178339400397, 0.40713654317942227, -0.5005592233592293, -0.2417692769524329, 0.16213246427647865, 0.4521454186396159, 0.38265042493093304, 0.20301608039054098, 0.4292077619153286, -0.15705904060795628, 0.18610940477550053, -0.3400129005607989, -0.39658683483411084, 0.26264479230937254, 0.1669857885437458, 0.1965792506027091, -0.11961905463091815, 0.2519608240917268, 0.1903942387886257, 0.4839802553994006, 0.14378377341929705, 0.35584589581172754, -0.4689792228576719, 0.05624375993010444, 0.3927541722217367, -0.1157620477661575, 0.3765550192957423, -0.4131505744262679, -0.1909854409085795, -0.49723778138860786, -0.1643090257108002, -0.1926100743251864, 0.4013001040192883, 0.4134588502380778, 0.3118218835073647, -0.3277649455555972, 0.4833292770742933, -0.014523354421298373, 0.04217360829025463, 0.07040164959046091, 0.4282317516558928, -0.41654200335339686, -0.2752417558466471, 0.4251432175861727, 0.21631884886905228, -0.38079810605639974, 0.23438058871876832, 0.44701197591784847, 0.19401535357350463, 0.3021269284295923, 0.43168114228752175, 0.02460117094837322, -0.121735848689152, -0.25639972278057166, 0.38247898266694547, 0.3472360488897771, -0.12920962773029288, 0.00667384022312334, -0.31606741240751635, 0.3466041982193777, 0.49622074982556497, -0.21709783482876, -0.09336170268735211, -0.331761624477627, 0.16399880814069367, 0.3220153390576559, -0.4523911689156934, -0.2507065166469845, -0.3840932963473478, -0.3751266220432805, -0.06277970313517567, -0.051873924143748806, -0.08359550138629546, 0.217678569152443, -0.4273468049118948, -0.28598751160289004, 0.3896916255999622, 0.033860862691701854, -0.34608905148575886, 0.17441926360674742, 0.3251571411167288, -0.09493717702277671, 0.1867974202388264, -0.2958766685585389, 0.4539912109398194, 0.44288799208773877, -0.25770971609290516, 0.19631191369730783, -0.37275739876546066, 0.03925339375468728, -0.2958059114936137, 0.26636084060239784, -0.3128317342542887, 0.3529836987536723, 0.038777470288319904, -0.13246488260188083, 0.1527252591101662, -0.12439483757153602, 0.07132583511440482, 0.34245578843840097, 0.2437208406054575, 0.07777811301947013, -0.45476797416270853, 0.07581870623878773, 0.1218442915352348, -0.42722918440629143, -0.01013544071192185, -0.44974437079560614, -0.21415252879870184, -0.14913749613320926, -0.06933722470913295, 0.33061287317809096, -0.09980828885153659, 0.36665841213094763, -0.000882079660815549, -0.3238132951238093, -0.16745224397133796, 0.14725053182166958, -0.08830201693365702, -0.02767213357473941, 0.4962595846391845, 0.2590926905070071, -0.1518230990186158, 0.09927801828799698, 0.4386709944912113, -0.22443141401438582, 0.2755467850716764, -0.134209987286537, 0.29269469388324265, -0.5015192124125094, -0.46905934526733606, 0.07229422045618272, -0.19979123711101376, -0.3617022056035655, 0.2527675603995484, -0.038539968130723246, 0.06923945175177992, -0.4968944109587472, 0.03429326673745603, -0.2301931789818612, -0.31577823146833517, 0.21423502418139462, 0.3129047705070176, -0.010939642441887276, -0.2917300337697206, -0.45190216937776506, -0.1022401397671101, -0.2858290677617369, -0.35305335221336154, 0.28305022317968065, -0.3897846926523091, 0.22677058927516625, -0.2845369414504918, -0.028802281393663698, -0.006025959369671874, 0.3057461924424144, 0.18201784693410894, 0.08136527821275796, -0.4465786388720252, 0.4830273596969804, -0.3040481088750939, 0.4930249559870632, -0.30183102466803235, -0.12966900168064466, -0.396553317646241, 0.3431852816732601, 0.4589486562447477, -0.16112700587244, -0.059388715988773044, 0.00809311903088028, -0.07349346469215434, -0.5015097449445728, -0.29394391211697013, -0.3241308007650364, -0.052321122263411124, 0.008006423059844003, 0.17249500267817985, -0.25063072613752646, -0.0028505731699466352, 0.26142854481604594, 0.07457987315411008, -0.4866777154424685, 0.3977745699944417, -0.23465829881018452, -0.4711222595297072, 0.43449321434496824, 0.2060857546982673, -0.3223657223844335, 0.11640450970499228, -0.407855988716899, -0.3441689483413478, -0.4526597383107682, 0.13401032713797323, 0.48554079945025175, 0.38973714567744644, 0.3216099688361096, 0.012466802471704508, 0.0462026513490007, -0.1333422644175275, -0.1342622887216789, -0.10250075882518717, 0.10427731489667802, -0.0959644892993976, 0.3121144127006754, -0.03860948894392224, 0.14049138961431085, -0.0491593027051167, 0.14308198206414247, 0.32727344931087965, 0.060121226309041376, -0.026464824199685988, 0.199029885217461, -0.4923849539245313, -0.3204233055502894, -0.38366992439967473, -0.03592339974636782, 0.1593173834092887, 0.3835721405916881, -0.06004664223885192, 0.3501210700229388, 0.4012617191527883, -0.07291056981699018, -0.17683308729977898, -0.08714701533338065, 0.34037176731002305, 0.4802339965697324, -0.3477620174736339, -0.3754309225912722, -0.09865359812433783, -0.4800375433873121, 0.3069786414435064, 0.03121833638556004, 0.19949585615109278, -0.318740996495511, 0.21601692147571794, 0.2985091667583738, -0.2643028377193659, -0.3620137880570653, -0.10491866519733173, -0.1271805136503551, -0.2706098079764183, -0.0903739277930482, -0.19133173476447796, -0.28068684710851755, -0.046487191876134704, 0.14210448153844157, -0.03488008719386526, 0.47727740372631, 0.3846682425973239, -0.3945670696656596, 0.2970133462897905, 0.21967569726492875, -0.22272473874346677, -0.1703469800650086, -0.36140241177682975, -0.06849787996904466, -0.18673815684919104, 0.3411917560048151, 0.05784550043195247, 0.35307988802474577, 0.274872507335842, -0.12244954886613069, 0.23470252757546484, 0.33530032026173917, -0.4718670259358023, -0.3069867100239121, -0.17762576990155554, 0.21662547200669013, 0.1097988703196191, -0.41795675322711756, 0.06796184637610525, -0.4602701652522223, -0.4083306608272943, -0.32322840848505685, -0.37996890988938725, -0.07570720625056171, -0.2557049473061723, -0.09922505471938359, -0.4314666604986396, -0.4887566033547588, 0.33047317022084355, 0.3236875783160451, 0.438598161204802, 0.04967757254690586, -0.10345178511007125, 0.13091625275682295, 0.35839019933864424, 0.38781434498627565, -0.07415949535975175, 0.41558761555791945, 0.3230074502700002, -0.4964716381285469, 0.25043575122889394, -0.02673124469116772, 0.19406342473564464, -0.04848177103144313, -0.1914003560662717, -0.14768894062573346, -0.4908144416099327, -0.38809551988858604, -0.4048934008658277, -0.3896204021110822, -0.48878012005420135, -0.3434579254125729, 0.11611778893003444, -0.44305287830982887, 0.4262777349608802, -0.23346613372763403, 0.2688489681829437, 0.05638947028768482, 0.410387112806306, 0.12581928704635426, 0.3816650746272041, 0.4144606318023363, 0.2682822233463513, -0.038220778254461796, -0.1435081817703081, 0.4029980620291157, 0.31875630119448795, -0.21252709516685886, -0.3887390918149408, 0.12573352793645154, -0.4104397690774102, 0.23672405783817985, 0.33588613672016354, -0.4084097587375499, 0.15519636772114975, -0.13822031363489629, -0.4056794861513786, -0.2481164884378454, -0.25618569793269763, -0.1814709475437417, -0.21090266548215086, 0.4224797716419523, 0.04265940526565892, 0.08827360295923758, 0.1884530654760257, 0.15206849602758243, 0.20139546855721646, -0.09345704024279, 0.2898295210796542, 0.11267355159568859, 0.23300642553582174, 0.2045108398537906, 0.05823093144871405, 0.025446547638349193, 0.09461761308039396, -0.43312004072213284, -0.4990059964499185, -0.42639130382032897, 0.16601850176020438, -0.42263095560566755, -0.4682888544611469, 0.39525784068594394, -0.09420806687006233, -0.1166359589064293, -0.3126088454328514, -0.04105926871889076, -0.21687139606480066, -0.41965055210148894, 0.05414282840632756, -0.18509084458925296, 0.42603419700414036, 0.20098566338152046, 0.11579721761498063, 0.4216782439567647, -0.09030102597337186, 0.38073018989679897, 0.49740877120327454, 0.17820506198500696, -0.35643308339405233, 0.48434075879126026, 0.4510686690234106, 0.4718720519820597, 0.2251379964740875, 0.025534469492412892, 0.2844902587504127, -0.44174810765411476, -0.3900853256929143, 0.47995957423414815, 0.05080920576901371, -0.44720490845516303, 0.42581912795079524, -0.2249298010638261, 0.26597435910285805, 0.26672534510710266, 0.26453939069837884, 0.39449339310638865, -0.21047191478248162, 0.49320965624420054, 0.45772003869889355, 0.06709499768947058, -0.377548708919743, -0.3799594201579728, 0.3094321270468441, -0.3994717158595732, 0.4761684263961874, -0.25368393872889283, 0.378594942473206, -0.1626207690790813, 0.4015185891168742, -0.44759909639764794, -0.1532175272279639, 0.3256407537888387, -0.44553793366193606, -0.2728402505881389, -0.02147083842492792, -0.09231507065201705, 0.363434219330535, 0.4342987284240917, -0.003062870132749529, -0.37570003252769946, 0.04876187948668753, -0.06011290857580753, 0.44358577463641824, -0.2585891545315703, 0.23278307232239093, 0.19301492341632376, -0.06366833597640587, -0.452260531710747, 0.09274809852924393, -0.43723363191513553, 0.016711556309350284, -0.4111350572565273, -0.03522320452838312, 0.38411122077819737, 0.33527596273484117, -0.47056876781950974, -0.3202707698363316, 0.16744076640123906, -0.18192775713071874, -0.3973786449169012, -0.1804558718444339, -0.13828028565571027, -0.011875374589155219, -0.2779056444556519, 0.37517399533308793, -0.18496246843227115, 0.2632576129487034, -0.4227134413888012, -0.41591582144414674, -0.39790458563716513, 0.26303611674729854, 0.12989468122139547, -0.2683737151567803, 0.47781163779728486, 0.02015764613206361, -0.4612125078591438, -0.10257894658759703, -0.26829971209512626, -0.37644467499546697, -0.42272569534591176, -0.11922927561486751, -0.2522907739564697, -0.20634652628983596, 0.49383187069229806, 0.32996534062961136, -0.34098091501693406, 0.19401538112252692, -0.14678127915089623, -0.438125226125599, -0.11392706187725199, -0.0534372004228022, 0.4239370628007477, 0.4132643310643812, -0.3856515337997065, 0.3044822723566345, -0.142949319863231, -0.2062293385856444, 0.17138448642004822, -0.013184908733764433, -0.03724002625250378, -0.2549055837100418, -0.10471039942935445, -0.19910068621391508, -0.3121426209602799, 0.4473359875931745, -0.01943034500018226, -0.24908756122615783, 0.3886912737342615, 0.22307703705080284, 0.06373142667752552, 0.39001887395471035, 0.13622793036599978, 0.028731647583637332, -0.12715199682996414, 0.3553833867930265, 0.2658817795877152, 0.3071997855512709, 0.35638130936574974, 0.15303872493357518, -0.10958007508724765, -0.11638803141199039, 0.4389100002859272]
1000-element Vector{Float64}:
-0.402858193077954
0.17163216613218168
-0.18169592947668356
0.060514380686886415
-0.2591266252711959
-0.01737820236308285
0.37319528099391985
0.23362003830781186
-0.3090151132440434
-0.06824664473090836
⋮
-0.12715199682996414
0.3553833867930265
0.2658817795877152
0.3071997855512709
0.35638130936574974
0.15303872493357518
-0.10958007508724765
-0.11638803141199039
0.4389100002859272
Problem 2 (18 points)
Cheap Plastic Products, Inc. is operating a plant that produces \(100 \text{m}^3\text{/day}\) of wastewater that is discharged into Pristine Brook. The wastewater contains \(1 \text{kg/m}^3\) of YUK, a toxic substance. The US Environmental Protection Agency has imposed an effluent standard on the plant prohibiting discharge of more than \(20 \text{kg/day}\) of YUK into Pristine Brook.
Cheap Plastic Products has analyzed two methods for reducing its discharges of YUK. Method 1 is land disposal, which costs \(X_1^2/20\) dollars per day, where \(X_1\) is the amount of wastewater disposed of on the land (\(\text{m}^3\text{/day}\)). With this method, 20% of the YUK applied to the land will eventually drain into the stream (i.e., 80% of the YUK is removed by the soil).
Method 2 is a chemical treatment procedure which costs $1.50 per \(\text{m}^3\) of wastewater treated. The chemical treatment has an efficiency of \(e= 1 - 0.005X_2\), where \(X_2\) is the quantity of wastewater (\(\text{m}^3\text{/day}\)) treated. For example, if \(X_2 = 50 \text{m}^3\text{/day}\), then \(e = 1 - 0.005(50) = 0.75\), so that 75% of the YUK is removed.
Cheap Plastic Products is wondering how to allocate their wastewater between these three disposal and treatment methods (land disposal, chemical treatment, and direct disposal) to meet the effluent standard while keeping costs manageable.
The flow of wastewater through this treatment system is shown in Figure 1. Modify the edge labels (by editing the edge_labels dictionary in the code producing Figure 1) to show how the wastewater allocations result in the final YUK discharge into Pristine Brook. For the edge_label dictionary, the tuple \((i, j)\) corresponds to the arrow going from node \(i\) to node \(j\). The syntax for any entry is (i, j) => "label text", and the label text can include mathematical notation if the string is prefaced with an L, as in L"x_1" will produce \(x_1\).
A = [0 1 1 1;
0 0 0 1;
0 0 0 1;
0 0 0 0]
names = ["Plant", "Land Treatment", "Chem Treatment", "Pristine Brook"]
# modify this dictionary to add labels
edge_labels = Dict((1, 2) => "", (1,3) => "", (1, 4) => "",(2, 4) => "",(3, 4) => "")
shapes=[:hexagon, :rect, :rect, :hexagon]
xpos = [0, -1.5, -0.25, 1]
ypos = [1, 0, 0, -1]
p = graphplot(A, names=names,edgelabel=edge_labels, markersize=0.15, markershapes=shapes, markercolor=:white, x=xpos, y=ypos)
display(p)Problem 2.1
Formulate a mathematical model for the treatment cost and the amount of YUK that will be discharged into Pristine Brook based on the wastewater allocations. This is best done with some equations and supporting text explaining the derivation. Make sure you include, as additional equations in the model, any needed constraints on relevant values. You can find some basics on writing mathematical equations using the LaTeX typesetting syntax here, and a cheatsheet with LaTeX commands can be found on the course website’s Resources page.
Solution:
These equations will be derived in terms of \(X_1\) (the land disposal amount, in kg/day) and \(X_2\) (the chemically treated amount, in kg/day), where \(X_1 + X_2 \leq 100\ \mathrm{kg/day}\). Note that we don’t need to explicitly represent the amount of directly disposed YUK, as this is \(100 - X_1 - X_2\) and so is not a free variable.
The amount of YUK which will be discharged is
\[\begin{align*} D(X_1, X_2) &= 100 - X_1 - X_2 + 0.2 X_1 + 0.005X_2^2 \\ &= 100 - 0.8 X_1 + (0.005X_2 - 1)X_2 \\ &= 100 - 0.8 X_1 + 0.005 X_2^2 - X_2 \end{align*}\]
The cost is \[ C(X_1, X_2) = X_1^2/20 + 1.5 X_2. \]
Problem 2.2
Implement your systems model as a Julia function which computes the resulting YUK discharge and cost for a particular treatment plan.
Solution:
A Julia function for this model could look like:
# we will assume that X₁, X₂ are vectors so we can vectorize
# the function; hence the use of broadcasting. This makes unpacking
# the different outputs easier as each will be returned as a vector.
# Note that even though this is vectorized, passing scalar inputs
# will still work fine.
function yuk_discharge(X₁, X₂)
# Make sure X₁ + X₂ <= 100! Throw an error if not.
if any(X₁ .+ X₂ .> 100)
error("X₁ + X₂ must be less than 200")
end
yuk = 100 .- 0.8X₁ .+ (0.005X₂ .- 1) .* X₂
cost = X₁.^2/20 .+ 1.5X₂
return (yuk, cost)
end- 1
- Checking for these kinds of errors is useful when there are hard limits on what arguments can be passed in. This syntax lets you throw an error which says something is going wrong in the code. In general, Julia style is to try to do a computation and throw an error if something goes wrong.
- 2
- We use broadcasting here to work on vectors of arguments for efficiency. This is in no way required.
yuk_discharge (generic function with 1 method)
Problem 2.3
Use your function to experiment with 1,000 different combinations of wastewater discharge and treatment. You can do this with either a grid search or by sampling from a Dirichlet distribution (a \(\text{Dirichlet}(1, n)\) distribution will generate uniformly-weighted \(n\)-dimensional vectors whose components add up to 1; see the Distributions.jl documentation for how to sample from probability distributions in Julia). Plot the results of these experiments. Do any satisfy the YUK effluent standard (plot this as well as a dashed red line). What was the cost of solutions satisfying the standard? What can you say about the tradeoff between treatment cost and YUK discharge? You don’t have to find an “optimal” solution to this problem, but what do you think would be needed to find a better solution?
Solution:
In this case, we will sample from a Dirichlet distribution, but a more systematic search would also be acceptable.
yuk_distribution = Dirichlet(3, 1)
# Need to scale samples from 0 to 200, not 0 to 1
yuk_samples = 100 * rand(yuk_distribution, 1000)
D, C = yuk_discharge(yuk_samples[1,:], yuk_samples[2, :])
# Plot the discharge vs. cost and add a line for the regulatory limit
p = scatter(D, C, markersize=2, label="Treatment Samples")
vline!(p, [20], color=:red, label="Regulatory Limit")
# Label axes
xaxis!(p, "YUK Discharge (kg/day)")
# For the y-axis label, we need to "escape" the $ by adding a slash
# otherwise it interprets that as starting math mode
yaxis!(p, "Treatment Cost (\$/day)")- 1
- The Dirichlet distribution is over combinations of values which add up to 1, which is what we want for shares of the total YUK discharge. The 3D Dirichlet distribution with parameters equal to 1 is basically uniform over these combinations. See: https://juliastats.org/Distributions.jl/stable/multivariate/#Distributions.Dirichlet.
- 2
-
This is a basic scatter plot with a label for the plot elements. If we wanted to turn the legend off in any plot, use
legend=falseas an argument. - 3
-
This is how to add a vertical line to a plot with a label. The syntax for a horizontal line is
hline(...). The!at the end ofvline!()is important: this is standard Julia syntax to distinguish commands which mutate (or change) their input (in this case, the first argumentp, the plot object), as this is not always desirable behavior. - 4
- This is how to change axis labels. Notice that this also mutates the input plot.
We can see that there are a few treatment strategies which comply with the limit, but they are fairly expensive. This is an example of a tradeoff between two objectives1, where one has to make a choice between what objectives to prioritize. But one thing to note is that just choosing an expensive strategy does not guarantee compliance.
1 More on this later in the semester!
Problem 2.4
Find the strategies which minimize cost and YUK discharge (these will be different strategies) analytically and find the values of the objective metrics. Plot these values in the plot that you created for Problem 2.3. How do their values compare to the spread of values that you found in that problem? Would you select either of them (explain why or why not)?
Solution:
To minimize these functions, we want to look at either the critical points (where the partial derivatives are zero) or at the constraints. For cost, \(dC/dX_2 = 1.5\) is never zero, so there are no critical points. However, the lowest-cost point occurs at the boundary \(X_2 = 0\) and \(X_1 = 0\), where there is no cost (unsurprisingly, doing nothing often costs the least). But this does not comply with the constraint, as the YUK discharge is 100 kg/day.
The YUK discharge function \(D\) has the following partial derivatives:
\[\begin{align*} \frac{\partial D}{\partial X_1} &= -0.8 \\ \frac{\partial D}{\partial X_2} &= 0.01 X_2 - 1, \end{align*}\]
so we need to check the values along the boundaries \(X_1 = 100 - X_2\), \(X_1 = 0\), and \(X_2 = 0\). \[D(100 - X_2, X_2) = 20X_2 - 0.005 X_2^2,\] which has a partial derivative \[\frac{\partial D}{\partial X_2}(100 - X_2, X_2) = -0.2 + 0.01 X_2.\] This has a minimum at \(X_2 = 20\) and therefore \(X_1 = 80\), with a corresponding YUK discharge of 18 kg/day. If \(X_2 = 0\), the minimum value (at \(X_1 = 100\)) is a discharge of 20 kg/day, while the minimum value along \(X_1 = 0\) is 50 kg/day (\(X_2 = 100\)). Thus, the minimum occurs at \(X_1 = 80\) and \(X_2 = 20\), with a corresponding cost of $350/day.
Adding these points to the plot:
# least cost solution
least_cost = yuk_discharge(0, 0)
scatter!(p, least_cost, color=:orange, label="Least Cost Solution")
# least discharge solution
least_discharge = yuk_discharge(80, 20)
scatter!(p, least_discharge, color=:purple, label="Least Discharge Solution")The least-cost solution, unsurprisingly, would be pretty bad given how much the YUK discharge violates the regulatory constraint. However, the least-discharge solution looks pretty good: there are more expensive treatment plans which comply with the regulatory constraint, but not many that are less expensive.
References
List any external references consulted, including classmates.