Created
February 13, 2023 06:25
-
-
Save Omar-Elrefaei/43e5b1e96c44b6e001197fb556c858ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using AxisArrayTables | |
| using AxisArrayTables: AxisArrays, data, named_axes | |
| using Distributions | |
| using Plots | |
| ## Option 1 | |
| function conf_limits(data, interval::Float64) | |
| upper_limit = Vector{Float64}(undef, length(data)) | |
| lower_limit = Vector{Float64}(undef, length(data)) | |
| lo_per = (1 - interval) / 2 | |
| up_per = 1 - (1 - interval) / 2 | |
| dists = Normal.(0, 1 .+ 0.5 * collect(0:length(data))) | |
| randss = (rand.(dists, 70)) | |
| for idx in 1:length(data) | |
| rands = sort(randss[idx]) | |
| up_rand = quantile(rands, up_per) | |
| lo_rand = quantile(rands, lo_per) | |
| upper_limit[idx] = data[idx] + up_rand | |
| lower_limit[idx] = data[idx] + lo_rand | |
| end | |
| upper_limit, lower_limit | |
| end | |
| function option1() | |
| len = 100 | |
| original_data = collect(1+100:len+100.0) | |
| org_axar = AxisArrayTable( | |
| [original_data original_data .* 2 .- 100], | |
| 1:len, | |
| [:omar, :michel] | |
| ) | |
| intervals_omar = [0.9, 0.3] | |
| #### These are all the ^ Inputs | |
| uppers_omar::Vector{Vector{Float64}} = [] | |
| lowers_omar::Vector{Vector{Float64}} = [] | |
| for interval in intervals_omar | |
| lims = conf_limits(data(org_axar[:omar]), interval) | |
| push!(uppers_omar, lims[1]) | |
| push!(lowers_omar, lims[2]) | |
| end | |
| plt = plot(org_axar.omar, linecolor=:red, label="mean", width=2, | |
| title="omar" | |
| ) | |
| for i in 1:length(intervals_omar) | |
| plot!( | |
| uppers_omar[i], | |
| fill=(lowers_omar[i], 0.3, :red), | |
| linecolor=nothing, #erase the top line | |
| primary=false, | |
| labels="$(Int(intervals_omar[i]*100))th confidence in omar" | |
| ) | |
| end | |
| # Relavent vars at the end: org_axar, intervals_omar, uppers_omar, lowers_omar | |
| return plt | |
| end | |
| ## Option 2: A struct | |
| function option2() | |
| len = 100 | |
| original_data = collect(1+100:len+100.0) | |
| org_axar = AxisArrayTable( | |
| [original_data original_data .* 2 .- 100], | |
| 1:len, | |
| [:omar, :michel] | |
| ) | |
| varA_conf = variable_confidence(org_axar, :omar, [0.3, 0.9]) | |
| plt = plot(varA_conf.original, linecolor=:red, label="mean", width=2, | |
| # title="$(intervals_omar*100) confidences", | |
| title="omar" | |
| ) | |
| for i in 1:length(varA_conf.intervals) | |
| plot!( | |
| varA_conf.upper_limits[i], | |
| fill=(data(varA_conf.lower_limits[i]), 0.3, :red), | |
| linecolor=nothing, #erase the top line | |
| primary=false, | |
| labels="$(Int(varA_conf.intervals[i]*100))th confidence in omar" | |
| ) | |
| end | |
| # Relavent var at the end: varA_conf | |
| return plt | |
| end | |
| struct variable_confidence | |
| original::AxisArrayTable | |
| intervals::Vector{Float64} | |
| upper_limits::Vector{AxisArrayTable} # col_name=[0.9, 0.3] | |
| lower_limits::Vector{AxisArrayTable} | |
| variable_confidence(original, intervals) = | |
| variable_confidence(original, intervals, Vector{AxisArrayTable}(), Vector{AxisArrayTable}()) | |
| function variable_confidence(original, intervals, upper_limits, lower_limits) | |
| if length(named_axes(original)[2]) > 1 | |
| error("Only one column of the originaly table should be passed") | |
| else | |
| new(original, intervals, upper_limits, lower_limits) | |
| end | |
| end | |
| end | |
| # Construct a variable_confidence from a table, axis label, and wanted intervals | |
| function variable_confidence(table::AxisArrayTable, axis::Symbol, intervals::Vector{Float64}) | |
| var_conf = variable_confidence(table[axis], intervals) | |
| for interval in intervals | |
| # these two lined aren where Boss does not peek are biromg [acemrls" | |
| _upper_boundry = conf_limits(data(table[axis]), interval, :upper) | |
| _lower_boundry = conf_limits(data(table[axis]), interval, :lower) | |
| push!(var_conf.upper_limits, AxisArrayTable( | |
| reshape(_upper_boundry, :, 1), named_axes(table)[1], [Symbol(interval)]) | |
| ) | |
| push!(var_conf.lower_limits, AxisArrayTable( | |
| reshape(_lower_boundry, :, 1), named_axes(table)[1], [Symbol(interval)]) | |
| ) | |
| end | |
| var_conf | |
| end | |
| function conf_limits(data, interval, option::Symbol) | |
| @assert option in (:upper, :lower) "option has to be either :upper or :lower" | |
| vec_data = reshape(data.data, :) | |
| if option == :upper | |
| return conf_limits(vec_data, interval)[1] | |
| else | |
| return conf_limits(vec_data, interval)[2] | |
| end | |
| ends | |
| end # module ConfidenceExp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment