text:
### Irving Gomez Mendez
### September 3rd 2019

#Considérense una muestra de n = 47 observaciones independientes e idénticamente distribuidas que corresponden a
#los tiempos entre llegadas de grupos de visitantes al Museo de las Momias de Guanajuato el Sábado Santo 15 de abril
#de 2017 de las 12:00 a las 12:30pm. Los 47 tiempos x1, ..., x47 fueron medidos como parte de su tesis por Ana Paulina
#Pérez Romero, exalumna graduada de la Maestría de Probabilidad y Estadística del CIMAT. Los datos registrados
#en segundos fueron:

mm = [54, 24, 54, 6, 14, 87, 42, 30, 21, 4, 43, 34, 25, 26, 79, 35, 15, 25, 33, 20, 12, 34, 24, 29, 28, 32, 83, 40, 32,
        94, 27, 47, 52, 68, 14, 44, 34, 32, 32, 6, 18, 80, 90, 40, 100, 25, 30]
nn = length(mm)

using Plots; pyplot()
using Distributions
# StatsBase has the ecdf
using StatsBase
# StatsPlots allows to plot a desnity directly from the distribution name
using StatsPlots
using LaTeXStrings

?Distributions

?Exponential

θ_hat = mean(mm)
Dist = Exponential(θ_hat)
Empirical = ecdf(mm)

plot(sort(mm), map(Empirical, sort(mm)),
    label = "Empirical CDF",
    ylabel = L"F_n(x,θ)",
    background_color = :Lavender,
    linetype = :steppost,
    lw = 2,
    color = :RebeccaPurple)

plot!(x -> cdf(Dist, x),
    label = "Exponential(38.68)",
    lw = 2)

histogram(mm,
    normalized = true,
    background_color = :Lavender,
    fillalpha = 0.33,
    color = :RebeccaPurple)

plot!(Exponential(38.68),
    label = "Exponential(38.68)",
    xlims = [0,110],
    lw = 2)

α = range(1/(nn+1), step = 1/(nn+1), length = nn)

scatter(quantile.(Dist, α), sort(mm),
    label = "QQ_Exponential(38.68)",
    ylabel = L"x_{(i)}",
    xlabel = L"Q_{i/(n+1)}",
    background_color = :Lavender,
    lw = 2,
    color = :RebeccaPurple)

plot!(x -> x,
    label = "Identity",
    lw = 2)

# Bootstrap
M = 1000

# Parametrico
θ2 = []
for i in 1:M
    muest = rand(Dist, nn)
    θ2 = push!(θ2, mean(muest))
end
var(θ2)

# No Parametrico
mm2 = rand(Dist, nn)
θ3 = []
for i in 1:M
    muest = rand(mm2, nn)
    θ3 = push!(θ3, mean(muest))
end
var(θ3)

Fisher_Info = θ_hat^(-2)
real_var = 1/(Fisher_Info*nn)

max_stars_repo_name: IrvingGomez/academic-hugo

max_issues_repo_name: IrvingGomez/academic-hugo

max_forks_repo_name: IrvingGomez/academic-hugo


text:
#=
rdb-26_002.jl
based on
https://gitlab.com/fMRI/toppe/blob/master/+toppe/+utils/read_rdb_hdr.m

Matlab version is:
Copyright (c) 2012 by General Electric Company. All rights reserved.

2019-05-22 Julia version by Jeff Fessler
=#

export rdb_hdr_26_002_def

"""
    hd = rdb_hdr_26_002_def()

Return array [Symbol Int DataType] `? × 3` used in `header_read()`
"""
function rdb_hdr_26_002_def()
    vcat( # todo: broken into chunks to avoid StackOverflowError()
        [
            :rdbm_rev 1 Float32
            :off_data 1 Int32
            :off_per_pass 1 Int32
            :off_unlock_raw 1 Int32
            :off_data_acq_tab 1 Int32
            :off_nex_tab 1 Int32
            :off_nex_abort_tab 1 Int32
            :off_tool 1 Int32
            :off_exam 1 Int32
            :off_series 1 Int32
            :off_image 1 Int32
            :off_ps 1 Int32
            :off_grad_data 1 Int32
            :off_CTT_data 1 Int32
            :off_spare1 1 Int32
            :off_spare2 1 Int32
            :off_spare3 1 Int32
            :off_spare4 1 Int32
            :off_spare5 1 Int32
            :off_spare6 1 Int32
            :run_int 1 Int32
            :scan_seq 1 Int16
            :run_char 6 Cuchar
            :scan_date 10 Cuchar
            :scan_time 8 Cuchar
            :logo 10 Cuchar
            :file_contents 1 Int16
            :lock_mode 1 Int16
            :dacq_ctrl 1 Int16
            :recon_ctrl 1 UInt16
            :exec_ctrl 1 UInt16
            :scan_type 1 Int16
            :data_collect_type 1 Int16
            :data_format 1 Int16
            :recon 1 Int16
            :datacq 1 Int16
            :npasses 1 Int16
            :npomp 1 Int16
            :nslices 1 UInt16
            :nechoes 1 Int16
            :navs 1 Int16
            :nframes 1 Int16
            :baseline_views 1 Int16
            :hnover 1 Int16
            :frame_size 1 UInt16
            :point_size 1 Int16
            :vquant 1 Int16
            :cheart 1 Int16
            :ctr 1 Float32
            :ctrr 1 Float32
            :initpass 1 Int16
            :incrpass 1 Int16
            :method_ctrl 1 Int16
            :da_xres 1 UInt16
            :da_yres 1 Int16
            :rc_xres 1 Int16
            :rc_yres 1 Int16
            :im_size 1 Int16
            :rc_zres 1 Int32
            :fermi_radius 1 Float32
            :fermi_width 1 Float32
            :fermi_ecc 1 Float32
            :clip_min 1 Float32
            :clip_max 1 Float32
            :default_offset 1 Float32
            :xoff 1 Float32
            :yoff 1 Float32
            :nwin 1 Float32
            :ntran 1 Float32
            :scalei 1 Float32
            :scaleq 1 Float32
            :rotation 1 Int16
            :transpose 1 Int16
            :kissoff_views 1 Int16
            :slblank 1 Int16
            :gradcoil 1 Int16
            :ddaover 1 Int16
            :sarr 1 Int16
            :fd_tr 1 Int16
            :fd_te 1 Int16
            :fd_ctrl 1 Int16
            :algor_num 1 Int16
            :fd_df_dec 1 Int16
            :dab 8 Int16
            :user0 1 Float32
            :user1 1 Float32
            :user2 1 Float32
            :user3 1 Float32
            :user4 1 Float32
            :user5 1 Float32
            :user6 1 Float32
            :user7 1 Float32
            :user8 1 Float32
            :user9 1 Float32
            :user10 1 Float32
            :user11 1 Float32
            :user12 1 Float32
            :user13 1 Float32
            :user14 1 Float32
            :user15 1 Float32
            :user16 1 Float32
            :user17 1 Float32
            :user18 1 Float32
            :user19 1 Float32
            :v_type 1 Int32
            :v_coefxa 1 Float32
            :v_coefxb 1 Float32
            :v_coefxc 1 Float32
            :v_coefxd 1 Float32
            :v_coefya 1 Float32
            :v_coefyb 1 Float32
            :v_coefyc 1 Float32
            :v_coefyd 1 Float32
            :v_coefza 1 Float32
            :v_coefzb 1 Float32
            :v_coefzc 1 Float32
            :v_coefzd 1 Float32
            :vm_coef1 1 Float32
            :vm_coef2 1 Float32
            :vm_coef3 1 Float32
            :vm_coef4 1 Float32
            :v_venc 1 Float32
            :spectral_width 1 Float32
            :csi_dims 1 Int16
            :xcsi 1 Int16
            :ycsi 1 Int16
            :zcsi 1 Int16
            :roilenx 1 Float32
            :roileny 1 Float32
            :roilenz 1 Float32
            :roilocx 1 Float32
            :roilocy 1 Float32
            :roilocz 1 Float32
            :numdwell 1 Float32
            :ps_command 1 Int32
            :ps_mps_r1 1 Int32
            :ps_mps_r2 1 Int32
            :ps_mps_tg 1 Int32
            :ps_mps_freq 1 UInt32
            :ps_aps_r1 1 Int32
            :ps_aps_r2 1 Int32
            :ps_aps_tg 1 Int32
            :ps_aps_freq 1 UInt32
            :ps_scalei 1 Float32
            :ps_scaleq 1 Float32
            :ps_snr_warning 1 Int32
            :ps_aps_or_mps 1 Int32
            :ps_mps_bitmap 1 Int32
            :ps_powerspec 256 Cuchar
            :ps_filler1 1 Int32
            :ps_filler2 1 Int32
            :halfecho 1 Int16
            :im_size_y 1 Int16
            :data_collect_type1 1 Int32
            :freq_scale 1 Float32
            :phase_scale 1 Float32
            :ovl 1 Int16
            :pclin 1 Int16
            :pclinnpts 1 Int16
            :pclinorder 1 Int16
            :pclinavg 1 Int16
            :pccon 1 Int16
            :pcconnpts 1 Int16
            :pcconorder 1 Int16
            :pcextcorr 1 Int16
            :pcgraph 1 Int16
            :pcileave 1 Int16
            :hdbestky 1 Int16
            :pcctrl 1 Int16
            :pcthrespts 1 Int16
            :pcdiscbeg 1 Int16
            :pcdiscmid 1 Int16
            :pcdiscend 1 Int16
            :pcthrespct 1 Int16
            :pcspacial 1 Int16
            :pctemporal 1 Int16
            :pcspare 1 Int16
            :ileaves 1 Int16
            :kydir 1 Int16
            :alt 1 Int16
            :reps 1 Int16
            :ref 1 Int16
            :pcconnorm 1 Float32
            :pcconfitwt 1 Float32
            :pclinnorm 1 Float32
            :pclinfitwt 1 Float32
            :pcbestky 1 Float32
            :vrgf 1 Int32
            :vrgfxres 1 Int32
            :bp_corr 1 Int32
            :recv_freq_s 1 Float32
            :recv_freq_e 1 Float32
            :hniter 1 Int32
            :fast_rec 1 Int32
            :refframes 1 Int32
            :refframep 1 Int32
            :scnframe 1 Int32
            :pasframe 1 Int32
            :user_usage_tag 1 UInt32
            :user_fill_mapMSW 1 UInt32
            :user_fill_mapLSW 1 UInt32
        ],
        [
            :user20 1 Float32
            :user21 1 Float32
            :user22 1 Float32
            :user23 1 Float32
            :user24 1 Float32
            :user25 1 Float32
            :user26 1 Float32
            :user27 1 Float32
            :user28 1 Float32
            :user29 1 Float32
            :user30 1 Float32
            :user31 1 Float32
            :user32 1 Float32
            :user33 1 Float32
            :user34 1 Float32
            :user35 1 Float32
            :user36 1 Float32
            :user37 1 Float32
            :user38 1 Float32
            :user39 1 Float32
            :user40 1 Float32
            :user41 1 Float32
            :user42 1 Float32
            :user43 1 Float32
            :user44 1 Float32
            :user45 1 Float32
            :user46 1 Float32
            :user47 1 Float32
            :user48 1 Float32
            :pcfitorig 1 Int16
            :pcshotfirst 1 Int16
            :pcshotlast 1 Int16
            :pcmultegrp 1 Int16
            :pclinfix 1 Int16
            :pcconfix 1 Int16
            :pclinslope 1 Float32
            :pcconslope 1 Float32
            :pccoil 1 Int16
            :vvsmode 1 Int16
            :vvsaimgs 1 Int16
            :vvstr 1 Int16
            :vvsgender 1 Int16
            :zip_factor 1 Int16
            :maxcoef1a 1 Float32
            :maxcoef1b 1 Float32
            :maxcoef1c 1 Float32
            :maxcoef1d 1 Float32
            :maxcoef2a 1 Float32
            :maxcoef2b 1 Float32
            :maxcoef2c 1 Float32
            :maxcoef2d 1 Float32
            :maxcoef3a 1 Float32
            :maxcoef3b 1 Float32
            :maxcoef3c 1 Float32
            :maxcoef3d 1 Float32
            :ut_ctrl 1 Int32
            :dp_type 1 Int16
            :arw 1 Int16
            :vps 1 Int16
            :mcReconEnable 1 Int16
            :fov 1 Float32
            :te 1 Int32
            :te2 1 Int32
            :dfmrbw 1 Float32
            :dfmctrl 1 Int32
            :raw_nex 1 Int32
            :navs_per_pass 1 Int32
            :dfmxres 1 Int32
            :dfmptsize 1 Int32
            :navs_per_view 1 Int32
            :dfmdebug 1 Int32
            :dfmthreshold 1 Float32
            :grid_control 1 Int16
            :b0map 1 Int16
            :grid_tediff 1 Int16
            :grid_motion_comp 1 Int16
            :grid_radius_a 1 Float32
            :grid_radius_b 1 Float32
            :grid_max_gradient 1 Float32
            :grid_max_slew 1 Float32
            :grid_scan_fov 1 Float32
            :grid_a2d_time 1 Float32
            :grid_density_factor 1 Float32
            :grid_display_fov 1 Float32
            :fatwater 1 Int16
            :fiestamlf 1 Int16
            :app 1 Int16
            :rhncoilsel 1 Int16
            :rhncoillimit 1 Int16
            :app_option 1 Int16
            :grad_mode 1 Int16
            :pfile_passes 1 Int16
            :asset 1 Int32
            :asset_calthresh 1 Int32
            :asset_R 1 Float32
            :coilConfigUID 1 UInt32
            :asset_phases 1 Int32
            :scancent 1 Float32
            :position 1 Int32
            :entry 1 Int32
            :lmhor 1 Float32
            :last_slice_num 1 Int32
            :asset_slice_R 1 Float32
            :asset_slabwrap 1 Float32
            :dwnav_coeff 1 Float32
            :dwnav_cor 1 Int16
            :dwnav_view 1 Int16
            :dwnav_corecho 1 Int16
            :dwnav_sview 1 Int16
            :dwnav_eview 1 Int16
            :dwnav_sshot 1 Int16
            :dwnav_eshot 1 Int16
            :a3dwin_type 1 Int16
            :a3dwin_apod 1 Float32
            :a3dwin_q 1 Float32
            :ime_scic_enable 1 Int16
            :clariview_type 1 Int16
            :ime_scic_edge 1 Float32
            :ime_scic_smooth 1 Float32
            :ime_scic_focus 1 Float32
            :clariview_edge 1 Float32
            :clariview_smooth 1 Float32
            :clariview_focus 1 Float32
            :scic_reduction 1 Float32
            :scic_gauss 1 Float32
            :scic_threshold 1 Float32
            :ectricks_no_regions 1 Int32
            :ectricks_input_regions 1 Int32
            :psc_reuse 1 Int16
            :left_blank 1 Int16
            :right_blank 1 Int16
            :acquire_type 1 Int16
            :retro_control 1 Int16
            :etl 1 Int16
            :pcref_start 1 Int16
            :pcref_stop 1 Int16
            :ref_skip 1 Int16
            :extra_frames_top 1 Int16
            :extra_frames_bot 1 Int16
            :multiphase_type 1 Int16
            :nphases 1 Int16
            :pure 1 Int16
            :pure_scale 1 Float32
            :new_wnd_level_flag 1 Int32
            :wnd_image_hist_area 1 Int32
            :wnd_high_hist 1 Float32
            :wnd_lower_hist 1 Float32
            :pure_filter 1 Int16
            :cfg_pure_filter 1 Int16
            :cfg_pure_fit_order 1 Int16
            :cfg_pure_kernelsize_z 1 Int16
            :cfg_pure_kernelsize_xy 1 Int16
            :cfg_pure_weight_radius 1 Int16
            :cfg_pure_intensity_scale 1 Int16
            :cfg_pure_noise_threshold 1 Int16
            :wienera 1 Float32
            :wienerb 1 Float32
            :wienert2 1 Float32
            :wieneresp 1 Float32
            :wiener 1 Int16
            :flipfilter 1 Int16
            :dbgrecon 1 Int16
            :ech2skip 1 Int16
            :tricks_type 1 Int32
            :lcfiesta_phase 1 Float32
            :lcfiesta 1 Int16
            :herawflt 1 Int16
            :herawflt_befnwin 1 Int16
            :herawflt_befntran 1 Int16
            :herawflt_befamp 1 Float32
            :herawflt_hpfamp 1 Float32
            :heover 1 Int16
            :pure_correction_threshold 1 Int16
            :swiftenable 1 Int32
            :numslabs 1 Int16
            :numCoilConfigs 1 UInt16
            :ps_autoshim_status 1 Int32
            :dynaplan_numphases 1 Int32
            :medal_cfg 1 Int16
            :medal_nstack 1 Int16
            :medal_echo_order 1 Int16
            :medal_kernel_up 1 Int16
            :medal_kernel_down 1 Int16
            :medal_kernel_smooth 1 Int16
            :medal_start 1 Int16
            :medal_end 1 Int16
            :rcideal 1 UInt32
            :rcdixproc 1 UInt32
            :df 1 Float32
            :bw 1 Float32
            :feextra 1 Int32
            :raw_pass_size 1 UInt64
            :sspsave 1 UInt64
            :udasave 1 UInt64
            :vibrant 1 Int16
            :asset_torso 1 Int16
            :asset_alt_cal 1 Int32
            :kacq_uid 1 Int32
            :psc_ta 1 Int32
            :disk_acq_ctrl 1 Int32
        ],
        [
            :asset_localTx 1 Int32
            :a3dscale 1 Float32
            :broad_band_select 1 Int32
            :scanner_mode 1 Int16
            :numbvals 1 Int16
            :numdifdirs 1 Int16
            :difnext2 1 Int16
            :difnextab 100 Int16
            :channel_combine_method 1 Int16
            :nexForUnacquiredEncodes 1 Int16
            :a2dscale 1 Float32
            :dd_mode 1 Int16
            :dd_q_ta_offset 1 Int16
            :dd_q_phase_delay 1 Float32
            :dacq_ctrl_chksum 1 UInt32
            :patient_checksum 1 UInt32
            :rcidealctrl 1 UInt32
            :echotimes 64 Float32
            :asl_perf_weighted_scale 1 Int16
            :echo_pc_extra_frames_bot 1 UInt16
            :echo_pc_ctrl 1 UInt32
            :echo_pc_ylines 1 UInt16
            :echo_pc_primary_yfirst 1 UInt16
            :echo_pc_reverse_yfirst 1 UInt16
            :echo_pc_zlines 1 UInt16
            :echo_pc_yxfitorder 1 UInt16
            :channel_combine_filter_type 1 Int16
            :mavric_control 1 UInt32
            :mavric_ImageType 1 UInt32
            :mavric_bin_separation 1 Int32
            :mavric_b0_offset 40 Float32
            :channel_combine_filter_width 1 Float32
            :channel_combine_filter_beta 1 Float32
            :low_pass_nex_filter_width 1 Float32
            :aps_tg_status 1 UInt32
            :cal_pass_set_vector 1 Int32
            :cal_nex_vector 1 Int32
            :cal_weight_vector 1 Int32
            :pure_filtering_mode 1 Int32
            :pure_lambda 1 Float32
            :pure_tuning_factor_surface 1 Float32
            :pure_tuning_factor_body 1 Float32
            :pure_derived_cal_fraction 1 Float32
            :pure_derived_cal_reapodization 1 Float32
            :noncart_grid_factor 1 Float32
            :noncart_dual_traj 1 Int32
            :noncart_traj_kmax_ratio 1 Int32
            :noncart_traj_merge_start 1 Int32
            :noncart_traj_merge_end 1 Int32
            :oversamplingfactor 1 Float32
            :nspokes_highres 1 Int32
            :nspokes_lowres 1 Int32
            :nrefslices 1 Int32
            :psmde_pixel_offset 1 Int32
            :hoecc 1 Int32
            :hoec_fit_order 1 Int32
            :esp 1 Int32
            :viewSharing3D 1 UInt32
            :gradwarp_interp_type 1 Int32
            :pure_blur_enable 1 Int32
            :pure_blur 1 Float32
            :slice_info_tab_checksum 1 UInt32
            :mb_factor 1 UInt32
            :mb_slice_fov_shift_factor 1 UInt32
            :mb_slice_order_sign 1 Int32
            :cal_options 1 UInt32
            :cs_factor 1 Float32
            :cs_flag 1 Int32
            :cs_maxiter 1 Int32
            :cs_consistency 1 Float32
            :cs_ph_stride 1 Int32
            :cs_sl_stride 1 Int32
            :noncart_traj_mode 1 Int32
            :pure_alpha 1 Float32
            :pure_otsu_class 1 Int32
            :pure_exp_wt 1 Float32
            :pure_erode_dist 1 Int32
            :pure_dilate_dist 1 Int32
            :pure_aniso_blur 1 Int32
            :pure_aniso_erode_dist 1 Int32
            :pure_aniso_dilate_dist 1 Int32
            :calmode 1 Int32
            :cine_ctrl 1 UInt16
            :cine_ncycles 1 UInt16
            :cine_recon_phases 1 UInt16
            :cine_acq_phases 1 UInt16
            :cine_virtual_coils 1 UInt16
            :cine_nrejects 1 UInt16
            :cine_low_rr 1 UInt16
            :cine_high_rr 1 UInt16
            :cine_avg_rr 1 UInt16
            :cine_bpm 1 UInt16
            :kat_arc_cal_size_ky 1 UInt16
            :kat_arc_cal_size_kz 1 UInt16
            :kat_arc_accel_outer 1 UInt16
            :kat_arc_accel_center 1 UInt16
            :cardt1map_ctrl 1 UInt16
            :cardt1map_ti 1 UInt16
            :propellerCtrl 1 Int32
            :daviewsPerBlade 1 Int32
            :rotationThreshold 1 Float32
            :shiftThreshold 1 Float32
            :correlationThreshold 1 Float32
            :npwfactor 1 Float32
            :viewshd_num 1 Int32
            :bvalstab 100 Float32
            :synbvalstab 10 Float32
            :numsynbvals 1 UInt16
            :position_detection 1 UInt16
            :medal_param 1 Float32
            :clariview_filter_x_nr_val 1 UInt16
            :clariview_filter_x_sh_val 1 UInt16
            :clariview_filter_x_is_factory 1 UInt16
            :clariview_filter_x_option 1 UInt16
            :grad_intensity_thres 1 Float32
            :anne_mask_dilation_length 1 Int32
            :anne_intensity_thres 1 Float32
            :anne_channel_percentage 1 Float32
            :ec3_iterations 1 Int32
            :combined_coil_map_thres 1 Float32
            :coil_map_smooth_factor 1 Float32
            :coil_map_2_filter_width 1 Float32
            :ec3_iteration_method 1 Int32
            :img_ctrl 1 Int32
            :moco_ctrl 1 Int32
            :distcorr_ctrl 1 Int32
            :distcorr_B0_filter_size 1 Int32
            :distcorr_B0_filter_std_dev 1 Float32
            :tensor_file_number 1 Int32
            :muse 1 UInt16
            :muse_nav 1 UInt16
            :muse_nav_accel 1 UInt16
            :muse_nav_hnover 1 UInt16
            :muse_nav_yres 1 UInt16
            :optimal_snr_noise_type 1 UInt16
            :virtual_chan_policy 1 UInt16
            :virtual_chan_sens_type 1 UInt16
            :virtual_chan_apply_type 1 UInt16
            :virtual_chan_uniformity_type 1 UInt16
            :num_virtual_channels 1 UInt16
            :excess 507 Int16
        ],
    )
end

ext: jl


text:
"""
    NamedDimsArray{L, T, N, A}(data)

A `NamedDimsArray` is a wrapper array type, that provides a view onto  the
orignal array, which can have its dimensions refer to name rather than by
position.

For example:
```
xs = NamedDimsArray{(:features, :observations)}(data);

n_obs = size(xs, :observations)
feature_totals = sum(xs; dims=:observations)

first_obs_vector = xs[observations=1]
x = x[observations=15, features=2]  # 2nd feature in 15th observation.
```

`NamedDimsArray`s are normally a (near) zero-cost abstraction.
They generally resolve most dimension name related operations at compile
time.

The `NamedDimsArray` constructor takes a list of names as `Symbol`s,
one per dimension, and an array to wrap.
If the array being wrapped is a `NamedDimsArray` already then the new names
are combined with the existing names -- to replace wildcards (`:_`).
This will throw an error if the new names are not compatible with the old names
(i.e. if the nonwildcards do not match).
To assign new names to a `NamedDimsArray` without regard to compatibility with the old names
see `rename`(@ref).
"""
struct NamedDimsArray{L, T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
    # `L` is for labels, it should be an `NTuple{N, Symbol}`
    data::A
end

function NamedDimsArray{L}(orig::AbstractArray{T, N}) where {L, T, N}
    if !(L isa NTuple{N, Symbol})
        throw(ArgumentError(
            "A $N dimentional array, needs a $N-tuple of dimension names. Got: $L"
        ))
    end
    return NamedDimsArray{L, T, N, typeof(orig)}(orig)
end

function NamedDimsArray(orig::AbstractArray{T, N}, names::NTuple{N, Symbol}) where {T, N}
    return NamedDimsArray{names}(orig)
end
NamedDimsArray(orig::AbstractVector, name::Symbol) = NamedDimsArray(orig, (name,))

# Name-asserting constructor (like renaming, but only for wildcards (`:_`).)
NamedDimsArray{L}(orig::NamedDimsArray{L}) where L = orig
function NamedDimsArray{L}(orig::NamedDimsArray{old_names, T, N, A}) where {L, old_names, T, N, A}
    new_names = unify_names(L, old_names)
    return NamedDimsArray{new_names, T, N, A}(parent(orig))
end

parent_type(::Type{<:NamedDimsArray{L, T, N, A}}) where {L, T, N, A} = A
Base.parent(x::NamedDimsArray) = x.data

"""
    names(A)

Returns a tuple of containing the names of all the dimensions of the array `A`.
"""
names(::Type{<:NamedDimsArray{L}}) where L = L
names(::Type{<:AbstractArray{T, N}}) where {T, N} = ntuple(_->:_, N)
names(x::T) where T<:AbstractArray = names(T)

dim(a::NamedDimsArray{L}, name) where L = dim(L, name)

#############################
# AbstractArray Interface
# https://docs.julialang.org/en/v1/manual/interfaces/index.html#man-interface-array-1

## Minimal
Base.size(a::NamedDimsArray) = size(parent(a))
Base.size(a::NamedDimsArray, d) = size(parent(a), dim(a, d))

## optional
Base.IndexStyle(::Type{A}) where A<:NamedDimsArray = Base.IndexStyle(parent_type(A))

Base.length(a::NamedDimsArray) = length(parent(a))

Base.axes(a::NamedDimsArray) = axes(parent(a))
Base.axes(a::NamedDimsArray, d) = axes(parent(a), dim(a, d))

function named_size(a::AbstractArray{T,N}) where {T,N}
    L = names(a)
    NamedTuple{L, NTuple{N,Int}}(size(a))
end
function Base.similar(
    a::NamedDimsArray{L,T},
    eltype::Type=T,
    dims::NamedTuple{new_names}=named_size(a)
) where {L,T,new_names}

    new_sizes = values(dims)
    return NamedDimsArray{new_names}(similar(parent(a), eltype, new_sizes))
end

function Base.similar(
    a::NamedDimsArray{L, T, N},
    eltype::Type,
    new_names::NTuple{N, Symbol}
) where {T,N,L}
    dims = NamedTuple{new_names, NTuple{N, Int}}(size(a))
    return similar(a, eltype, dims)
end

function Base.similar(
    a::NamedDimsArray{L, T, N},
    eltype::Type,
    new_sizes::NTuple{N, Int}
) where {L,T,N}

    dims = NamedTuple{L, NTuple{N, Int}}(new_sizes)
    return similar(a, eltype, dims)
end

###############################
# kwargs indexing

"""
    order_named_inds(A, named_inds...)

Returns the indices that have the names and values given by `named_inds`
sorted into the order expected for the dimension of the array `A`.
If any dimensions of `A` are not present in the named_inds,
then they are given the value `:`, for slicing

For example:
```
A = NamedDimArray(rand(4,4), (:x,, :y))
order_named_inds(A; y=10, x=13) == (13,10)
order_named_inds(A; x=2, y=1:3) == (2, 1:3)
order_named_inds(A; y=5) == (:, 5)
```

This provides the core indexed lookup for `getindex` and `setindex` on the Array `A`
"""
order_named_inds(A::AbstractArray; named_inds...) = order_named_inds(names(A); named_inds...)

###################
# getindex / view / dotview
# Note that `dotview` is undocumented but needed for making `a[x=2] .= 3` work

for f in (:getindex, :view, :dotview)
    @eval begin
        @propagate_inbounds function Base.$f(A::NamedDimsArray; named_inds...)
            inds = order_named_inds(A; named_inds...)
            return Base.$f(A, inds...)
        end

        @propagate_inbounds function Base.$f(a::NamedDimsArray, inds::Vararg{<:Integer})
            # Easy scalar case, will just return the element
            return Base.$f(parent(a), inds...)
        end

        @propagate_inbounds function Base.$f(a::NamedDimsArray, ci::CartesianIndex)
            # Easy scalar case, will just return the element
            return Base.$f(parent(a), ci)
        end

        @propagate_inbounds function Base.$f(a::NamedDimsArray, inds...)
            # Some nonscalar case, will return an array, so need to give that names.
            data = Base.$f(parent(a), inds...)
            L = remaining_dimnames_from_indexing(names(a), inds)
            return NamedDimsArray{L}(data)
        end
    end
end

############################################
# setindex!
@propagate_inbounds function Base.setindex!(a::NamedDimsArray, value; named_inds...)
    inds = order_named_inds(a; named_inds...)
    return setindex!(a, value, inds...)
end

@propagate_inbounds function Base.setindex!(a::NamedDimsArray, value, inds...)
    return setindex!(parent(a), value, inds...)
end


text:
using MultipleViewGeometry, Test, Random
using MultipleViewGeometry.ModuleCostFunction
using MultipleViewGeometry.ModuleTypes
using MultipleViewGeometry.ModuleConstraints
using MultipleViewGeometry.ModuleConstruct
using LinearAlgebra
using StaticArrays

# Fix random seed.
Random.seed!(1234)

𝒳 = [Point3D(x,y,rand(50:100)) for x = -100:5:100 for y = -100:5:100]
𝒳 = 𝒳[1:50:end]

# Specify the coordinate systems of the world, the camera frame and the picture
# plane.
world_basis = (Vec(1.0, 0.0, 0.0), Vec(0.0, 1.0, 0.0), Vec(0.0, 0.0, 1.0))
camera_basis = (Point(0.0, 0.0, 0.0), Vec(-1.0, 0.0, 0.0), Vec(0.0, -1.0, 0.0), Vec(0.0, 0.0, 1.0))
picture_basis = (Point(0.0, 0.0), Vec(-1.0, 0.0), Vec(0.0, -1.0))

# The focal length for both cameras is one.
f = 1
image_width = 640 / 10
image_height = 480 / 10

camera₁ = Pinhole(image_width, image_height, f, camera_basis..., picture_basis...)
camera₂ = Pinhole(image_width, image_height, f, camera_basis..., picture_basis...)

# Rotate and translate camera one.
𝐑₁ = Matrix{Float64}(I,3,3)
𝐭₁ = [-50.0, -2.0, 0.0]
relocate!(camera₁, 𝐑₁, 𝐭₁)

# Rotate and translate camera two.
𝐑₂ = Matrix{Float64}(I,3,3)
𝐭₂ = [50.0, 2.0, 0.0]
relocate!(camera₂, 𝐑₂, 𝐭₂)

𝐑₁′, 𝐭₁′ = ascertain_pose(camera₁, world_basis... )
𝐊₁′ = obtain_intrinsics(camera₁, CartesianSystem())
𝐑₂′, 𝐭₂′ = ascertain_pose(camera₂, world_basis... )
𝐊₂′ = obtain_intrinsics(camera₂, CartesianSystem())

# Camera projection matrices.
𝐏₁ = construct(ProjectionMatrix(),𝐊₁′,𝐑₁′,𝐭₁′)
𝐏₂ = construct(ProjectionMatrix(),𝐊₂′,𝐑₂′,𝐭₂′)

# Set of corresponding points.
ℳ = project(camera₁,𝐏₁,𝒳)
ℳʹ = project(camera₂,𝐏₂,𝒳)

𝒴 = triangulate(DirectLinearTransform(),𝐏₁,𝐏₂,(ℳ,ℳʹ))

# Triangulating with the same projection matrices that were used to construct
# (ℳ,ℳʹ) should yield the same 3D points as the original 𝒳.
N = length(𝒴)
for n = 1:N
    @test  isapprox(sum(abs.(𝒳[n]-𝒴[n])/3), 0.0; atol = 1e-11)
end

𝐅 = construct(FundamentalMatrix(),𝐊₁′,𝐑₁′,-𝐭₁′,𝐊₂′,𝐑₂′,-𝐭₂′)

# To triangulate the corresponding points using the Fundamental matrix, we first
# have to factorise the Fundamental matrix into a pair of Camera matrices. Due
# to projective ambiguity, the camera matrices are not unique, and so the
# triangulated 3D points will most probably not match the original 3D points.
# However, when working with noiseless data, the projections of the triangulated
# points should satisfy the epipolar constraint. We can use this fact to
# validate that the triangulation is correctly implemented.
𝒴 = triangulate(DirectLinearTransform(),𝐅,(ℳ,ℳʹ))

𝐐₁, 𝐐₂ = construct(ProjectionMatrix(),𝐅)
# Because we constructed the projection matrices from the fundamental matrix we
# don't know the intrinsics or extrinsics of the camera.
# The current API requires us to construct a CameraModel type for the `project`
# function. TODO: Need to revisit this.
camera₁ = Pinhole(image_width, image_height, f, camera_basis..., picture_basis...)
camera₂ = Pinhole(image_width, image_height, f, camera_basis..., picture_basis...)
𝒪 = project(camera₁,𝐐₁,𝒴)
𝒪ʹ= project(camera₂,𝐐₂,𝒴)
N = length(𝒪)
for n = 1:N
    𝐦 = hom(𝒪[n])
    𝐦ʹ = hom(𝒪ʹ[n])
    @test  isapprox(𝐦'*𝐅*𝐦ʹ, 0.0; atol = 1e-12)
end


text:
module Service
using ..DFControl
using Distributed, Pkg, CodeTracking, LoggingExtras, Dates, JLD2, LinearAlgebra
using ..DFControl: config_path
using ..Utils
using ..Calculations
using ..Jobs
using ..FileIO
using ..Servers
# const DAEMON_CONFIG_PATH = config_path("daemon.jld2")
# delete_daemon_config!() = rm(DAEMON_CONFIG_PATH)

const RUNNING_JOBS_FILE = config_path("jobs", "running.txt")
const PENDING_JOBS_FILE = config_path("jobs", "pending.txt")
const SERVICE_LOG = config_path("daemon.log")
const SLEEP_TIME = 10.0

daemon_logger() = FileLogger(config_path("logs/daemon.log"); append = false)
job_logger(id::Int) = FileLogger(config_path("logs/jobs/$id.log"))
next_jobid() = length(readdir(config_path("logs/jobs"))) + 1

server_config() = Server("localhost")

include("running.jl")
include("calculation.jl")
include("pseudos.jl")
include("job.jl")
# include("execs.jl")
# include("fileio.jl")
include("slurm.jl")
include("bash.jl")
end


text:
# A giant whale has decided your submarine is its next meal, and it's much
# faster than you are. There's nowhere to run!
#
# Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep
# for them otherwise) zooms in to rescue you! They seem to be preparing to
# blast a hole in the ocean floor; sensors indicate a massive underground cave
# system just beyond where they're aiming!
#
# The crab submarines all need to be aligned before they'll have enough power
# to blast a large enough hole for your submarine to get through. However, it
# doesn't look like they'll be aligned before the whale catches you! Maybe you
# can help?
#
# There's one major catch - crab submarines can only move horizontally.
#
# You quickly make a list of the horizontal position of each crab (your puzzle
# input). Crab submarines have limited fuel, so you need to find a way to make
# all of their horizontal positions match while requiring them to spend as
# little fuel as possible.
#
# For example, consider the following horizontal positions:
#
# 16,1,2,0,4,2,7,1,2,14
#
# This means there's a crab with horizontal position 16, a crab with horizontal
# position 1, and so on.
#
# Each change of 1 step in horizontal position of a single crab costs 1 fuel.
# You could choose any horizontal position to align them all on, but the one
# that costs the least fuel is horizontal position 2:
#
# Move from 16 to 2: 14 fuel
# Move from 1 to 2: 1 fuel
# Move from 2 to 2: 0 fuel
# Move from 0 to 2: 2 fuel
# Move from 4 to 2: 2 fuel
# Move from 2 to 2: 0 fuel
# Move from 7 to 2: 5 fuel
# Move from 1 to 2: 1 fuel
# Move from 2 to 2: 0 fuel
# Move from 14 to 2: 12 fuel
#
# This costs a total of 37 fuel. This is the cheapest possible outcome; more
# expensive outcomes include aligning at position 1 (41 fuel), position 3 (39
# fuel), or position 10 (71 fuel).
#
# Determine the horizontal position that the crabs can align to using the least
# fuel possible. How much fuel must they spend to align to that position?

# read input and sort it
positions = sort!(parse.(UInt16, split(readline("input.txt"), ',')))

# Observation: if I pick a point and make all the crabs move there, it'll cost
# some amount of fuel. If I move that point one position to the left, all crabs
# to the left of the original point will spend 1 less fuel, and all crabs to
# the right will spend one more. Vice versa if I move one position to the
# right. So, if I keep track of how many crabs are left and right of my chosen
# point, and the total fuel cost of the crabs to the left and right, I could
# loop through all positions fairly quickly. Starting at position 0 (there are
# no negative positions in the input), there are no crabs to the left, but
# there may be some crabs already at position 0.
fuel_left = 0
fuel_right = sum(positions)
next_break = findfirst(>(0), positions)
num_left = 0
num_centered = next_break - 1
num_right = length(positions) - num_centered
cheapest = fuel_right
for position in 1:positions[end]
  # calculate new fuels
  global (fuel_left, fuel_right) = (fuel_left + num_left + num_centered, fuel_right - num_right)
  if fuel_left + fuel_right < cheapest
    global cheapest = fuel_left + fuel_right
  end

  # calculate how many crabs are already at this position (ie, fuel 0)
  new_num_centered = 0
  if position == positions[next_break]
    new_next_break = findnext(>(position), positions, next_break)
    if isnothing(new_next_break)
      # honestly, when this happens, we can break - continuing to move to the
      # right will only increase fuel cost because all crabs are to the left
      break
    end

    new_num_centered = new_next_break - next_break
    global next_break = new_next_break
  end

  # update crab counts
  global (num_left, num_centered, num_right) = (num_left + num_centered, new_num_centered, num_right - new_num_centered)
end

println("Cheapest fuel cost: ", cheapest)


text:
module LogitNormals

using Distributions, DistributionFits
using StaticArrays
using Optim
import StatsFuns: logit, logistic, normcdf

# for extension
import Distributions: mean, var, mode
import StatsBase: fit
import DistributionFits: fit_mode_quantile, fit_mean_quantile

export fit_mode_flat

include("logitnormal.jl")
include("util.jl")
include("logitnormDistributions.jl")

end


text:
import Statistics
import Parameters.@with_kw

##### BinFixedWidth #####

"""
"""
@with_kw mutable struct BinFixedWidth
    n_bins::Integer = 10; @assert n_bins > 0
    data_min::Union{Float64,Missing} = missing
    data_span::Union{Float64,Missing} = missing
    bin_width::Union{Float64,Missing} = missing
    is_fit::Bool = false
end

"""
"""
function fit_transform!(bin::BinFixedWidth, data::AbstractArray{<:Real})
    bin.data_min = minimum(data)
    bin.data_span = maximum(data) - bin.data_min
    bin.bin_width = bin.data_span / bin.n_bins
    bin.is_fit = true
    bin
end

"""
"""
function apply_transform(bin::BinFixedWidth, data::AbstractArray{<:Real})
    if !bin.is_fit | bin.data_min === missing | \
        bin.data_span === missing | bin.bin_width === missing
        error("`bin` hasn't been fit.")
    end
    ceil.(Int,(data .- bin.data_min) ./ bin.bin_width)
end

##### BinQuantile #####

"""
"""
@with_kw mutable struct BinQuantile
    n_bins::Integer = 10; @assert n_bins > 0
    quantiles::Union{Array{Float64},Missing} = missing
    is_fit::Bool = false
end

"""
"""
function fit_transform!(bin::BinQuantile, data::AbstractArray{<:Real})
    bin.quantiles = Statistics.quantile(
        data,
        0:( 1 / (n_bins-1) ):1
    )
    bin.is_fit = true
    bin
end

"""
"""
function apply_transform(bin::BinQuantile, data::AbstractArray{<:Real})
    if !bin.is_fit | bin.quantiles === missing
        error("`bin` hasn't been fit.")
    end
    [findlast(q -> n < q, bin.quantiles) for n = data]
end


text:
### A Pluto.jl notebook ###
# v0.14.1

using Markdown
using InteractiveUtils

# ╔═╡ e4d3550a-72dd-11eb-06bc-fb27a9d9cd72
md"""##### SUBJECT:$~~$ YourId;ClassId;NotebookID;Notebook-Version mm/dd/yyyy
MyIdDavid1234;
Class-MissSmith123;
Notbook#2-MathGrade6;
02/15/2021
"""

# ╔═╡ 61919858-72df-11eb-1745-67ab5e72244b
md"""##### EMAIL:$~~$Self@email.com;Teacher@email.com;Friend@email.com
mySelf@email.com;
myTeacher@email.com;
myFriend@email.com
"""

# ╔═╡ 63a1b84a-7324-11eb-0682-1fb702507e32
md"""# $~~~~~~~~~~~\mathbb Demo~~~\mathbb Lesson~~~\mathbb Expanded$
##### Based On; Version; ServerToken; Sources(put a blank line before each)
Demo Lesson;
version 02/15/2021;
Token12345

https://github.com/paradocs/pluto-community/blob/main/Demo%20Lesson.jl

[ParaDoc.Care] (https://www.paradocs.care/computing)  Copied from this web-page.

Copied from e-mail sent to student.
"""

# ╔═╡ 7fe2d7b6-72e1-11eb-1a1a-afdd77020baa
md"""##### SAVE/SEND:
1) Your work will be lost if not sent by e-mail.
2) Place your ID Name on line 2 of cell 1$~~$ End with a semicolon.
3) Send youself copies as you work. for backups.
4) Answers must be written in md-cells for your teacher to see.
5) Click top right △Export and then △Notebook file.
5) Open the new tab, Ctrl a$~~~$ to select all, Ctrl c$~~~$ to copy.
7) Open a new e-mail and use Ctrl v$~~$ paste in to the body.
8) Select and past the subject lines ending with semicolons.
9) Select and past one or more of the e-mail lines.
10) Check your ID and that you have copied correctly and SEND.
"""

# ╔═╡ a30fd548-72f0-11eb-0180-f1902eece57a
md"""##### OPEN NEW NOTEBOOK: (-- work in progress --)
1) Start a web Pluto Notebook or use a Notebook that is already open.
2) Open a web page, e-mail, or file in an editor with the new notebook code.
3) Carefully select just the code with the mouse, holding the left button.
4) For a code only page use Crtl a$~~$.  Holding shift may help with selecting.
4) Be sure you have all of the code.  Then copy with Ctrl c.
5) Click at the start of the top address bar and delete anything there.
6) With Ctrl v paste the new code.
7) Click the button to open the new notebook.
"""

# ╔═╡ 7dd2db00-7300-11eb-20a9-ef06117f61e2
md"""##### HELP/HINT$~~$ EMAIL SYSTEM
###### If your teacher or assistents are available to scan e-mail for hint requests,

###### you may write a request in a md-cell starting with helpyyyy/mm/dd $~~~$ and

###### place an * in front of your ID.$~~$  E-mail it to your teacher or assistent.

###### Remove the * for the final notebook you send.

!!! start
"""

# ╔═╡ c3b85594-72f5-11eb-3717-a1c384543220
md"""
###### Problem(1)

Write an expresstion to add 3 plus 5 plus one-half,

Click the Answer(1) cell and type in your answer.

"""

# ╔═╡ 9b1f5900-72f5-11eb-24ac-2b31323430fd
a = 3 + 5 + .5

# ╔═╡ aae018b4-74af-11eb-2bd7-f58e6e737e0b
md"""
Answer(1)
a = 8.5

"""

# ╔═╡ ff44b926-78d5-11eb-3ea6-e16e11b73242
md"""
!!! study
##### ``~~~~~~~``Use a zoom enabled white board for Hints, Help, and Teaching.
##### ``~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``To move and zoom use:
##### ``~~~~~~~~~~~``arrow keys -or- left click and drag -or- the mousewheel
##### ``~~~~~~~``You may open to write and edit, but nothing will be saved.
##### ``~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``Run the cell to start over.
##### ``~~~~~~~~~~~~~~``Click (n)ext or the "n" key twice the first time.
##### ``~~~~~~~~~~~``The "HOME" key goes home with the stardard zoom.
''
"""

# ╔═╡ 5844acaa-78d3-11eb-1582-717d7015401c
html"""

Zoom & Move

"""

# ╔═╡ 592e7fb2-8182-11eb-2066-376bc42c6d1e

# ╔═╡ 98f1eb34-8187-11eb-05b7-c79cd8da3a7f

# ╔═╡ a81ca4de-8182-11eb-2a7a-03d27b8d1e46

# ╔═╡ c6021c2c-818a-11eb-0a03-e529ec84eeba

# ╔═╡ Cell order:
# ╠═e4d3550a-72dd-11eb-06bc-fb27a9d9cd72
# ╠═61919858-72df-11eb-1745-67ab5e72244b
# ╟─63a1b84a-7324-11eb-0682-1fb702507e32
# ╟─7fe2d7b6-72e1-11eb-1a1a-afdd77020baa
# ╟─a30fd548-72f0-11eb-0180-f1902eece57a
# ╠═7dd2db00-7300-11eb-20a9-ef06117f61e2
# ╟─c3b85594-72f5-11eb-3717-a1c384543220
# ╠═9b1f5900-72f5-11eb-24ac-2b31323430fd
# ╠═aae018b4-74af-11eb-2bd7-f58e6e737e0b
# ╠═ff44b926-78d5-11eb-3ea6-e16e11b73242
# ╟─5844acaa-78d3-11eb-1582-717d7015401c
# ╟─592e7fb2-8182-11eb-2066-376bc42c6d1e
# ╟─98f1eb34-8187-11eb-05b7-c79cd8da3a7f
# ╟─a81ca4de-8182-11eb-2a7a-03d27b8d1e46
# ╟─c6021c2c-818a-11eb-0a03-e529ec84eeba

max_stars_repo_path: Demo Lesson.jl

max_stars_repo_name: paradocs/pluto-community

max_issues_repo_path: Demo Lesson.jl

max_issues_repo_name: paradocs/pluto-e-mail

max_forks_repo_path: Demo Lesson.jl

max_forks_repo_name: paradocs/pluto-e-mail


text:
using Distances

function (d::Distances.Hamming)(X::Set{T}, Y::Set{T}) where {T}
    if length(X) > length(Y)
        return d(Y,X)
    else
        n_intersect = 0
        for v in X
            n_intersect += (v ∈ Y)
        end
        return length(X) + length(Y) - 2n_intersect
    end
end

(d::Distances.Hamming)(X::Union{Set{T},Vector{T}}, Y::Nothing) where {T} = length(X)
(d::Distances.Hamming)(X::Nothing, Y::Union{Set{T},Vector{T}}) where {T} = d(Y,X)
(d::Distances.Hamming)(X::Nothing,Y::Nothing) = 0.0

# function (d::Distances.Jaccard)(X::Vector{T}, Y::Vector{T}) where {T}
#     if length(X) > length(Y)
#         return d(Y,X)
#     else
#         n_intersect = 0
#         for v in X
#             n_intersect += (v ∈ Y)
#         end

#         return 1 - n_intersect/(length(X) + length(Y) - n_intersect)
#     end
# end

function (d::Distances.Jaccard)(X::Set{T}, Y::Set{T}) where {T}
    if length(X) > length(Y)
        return d(Y,X)
    else
        n_intersect = 0
        for v in X
            n_intersect += (v ∈ Y)
        end

        return 1 - n_intersect/(length(X) + length(Y) - n_intersect)
    end
end


text:
using SymPyCall
import SymPyCall: Latexify
using Test

@testset "Latexify" begin
  @syms α
  @test_nowarn Latexify.latexify(- 3/(8*α) + 1/(8*α^2))
  @test  occursin("\\cdot", Latexify.latexify(- 3/(8*α) + 1/(8*α^2), cdot=true))
  @test !occursin("\\cdot", Latexify.latexify(- 3/(8*α) + 1/(8*α^2), cdot=false))
end

max_stars_repo_name: jverzani/SymPyCall

max_issues_repo_name: jverzani/SymPyCall

max_forks_repo_name: jverzani/SymPyCall


text:
########################################################
####             equation prediction                ####
########################################################

"""
    sfmodel_predict(@eq(eq_name), data::DataFrame)

Return predicted values of an equation (`eq_name`) of a stochastic frontier
model specified and estimated by `sfmodel_spec()` and `sfmodel_fit()` .

# Arguments:
- `@eq(eq_name)`: where `eq_name` is the name of the equation to which the
  value is to be predicted. A SF model has various equations, and different
  SF models have different sets of equations. Eligible `eq_name`s follow
  those specified in `sfmodel_spec()`. The `eq_name` may be specified using
  unicodes or their alias names; viz., `@eq(σᵤ²)` is the same as
  `@eq(sigma_u_2)`. Also, `log_σᵤ²` = `log_sigma_u_2`, `σᵥ²`= `sigma_v_2`,
  `log_σᵥ²` = `log_sigma_v_2`, `σₐ²`= `sigma_a_2`, `log_σₐ²` = `log_sigma_a_2`,
  and `μ` = `mu`.
- `data::DataFrame`: the DataFrame used in `sfmodel_fit()`. The DataFrame is
  required only when Method 1 is used in specifying `sfmodel_spec()`. If
  Method 2 is used instead, do not need this argument. See the help of
  `sfmodel_spec()` for the information on Method 1 and Method 2.

# Remarks:
The predicted value is computed based on the equation's variable list and the
estimated coefficient vector. For instance, if the `frontier` function is a
linear function of variables `X` and coefficient vector `β`,
`sfmodel_predict(@eq(frontier), df)` returns `X*β̂ `. If the variance
function `σᵤ²` is parameterized by an exponential function of `Z` and `δ`
(i.e., `σᵤ² = exp(Zδ)`), `sfmodel_predict(@eq(log_σᵤ²))` returns `Z*δ̂`
and `sfmodel_predict(@eq(σᵤ²))` returns `exp(Z*δ̂)`.

# Examples
```julia
frontier_hat = sfmodel_predict(@eq(frontier), df);
sigma_u_2_hat = sfmodel_predict(@eq(σᵤ²), df);
```
"""
function sfmodel_predict(eq::Vector{Symbol})

    # For Method 2 of `sfmodel_spec()`

    !(_dicM[:hasDF]) || throw("Need to specify DataFrame in `sfmodel_predict()`.")

    _dicM[:transfer] = true
    sfmodel_predict(eq, _dicM[:sdf])
end

function sfmodel_predict(eq::Vector{Symbol},  sfdata::DataFrame)

  # usage：
  # frontier_hat = sfmodel_predict(@eq(frontier), mydata)

  # σᵤ² = sigma_u_2 = exp(log_σᵤ²) = exp(log_sigma_u_2)

  (_dicM[:hasDF] || _dicM[:transfer]) || throw("You provided matrix in `sfmodel_spec()` so you cannot specify a DataFrame in `sfmodel_predict()`. Leave it out.")

  eqname = eq[1]

  takeexp = false

  if eqname == :frontier
      eq_var = :frontier
      eq_coe = :frontier
  elseif eqname == :mu || eqname == :μ
      eq_var = :μ
      eq_coe = :μ
 elseif eqname == :sigma_a_2 || eqname == :σₐ²
      eq_var  = :σₐ²
      eq_coe  = :log_σₐ²
      takeexp = true
  elseif eqname == :sigma_u_2 || eqname == :σᵤ²
      eq_var  = :σᵤ²
      eq_coe  = :log_σᵤ²
      takeexp = true
  elseif eqname == :sigma_v_2 || eqname == :σᵥ²
      eq_var  = :σᵥ²
      eq_coe  = :log_σᵥ²
      takeexp = true
  elseif eqname == :log_sigma_u_2 || eqname == :log_σᵤ²
      eq_var  = :σᵤ²
      eq_coe  = :log_σᵤ²
  elseif eqname == :log_sigma_v_2 || eqname == :log_σᵥ²
      eq_var  = :σᵥ²
      eq_coe  = :log_σᵥ²
  elseif eqname == :gamma
      eq_var  = :gamma
      eq_coe  = :log_gamma
      takeexp = true
  elseif eqname == :log_gamma
      eq_var  = :gamma
      eq_coe  = :log_gamma
  elseif eqname == :hscale
      eq_var  = :hscale
      eq_coe  = :log_hscale
      takeexp = true
  elseif eqname == :log_hscale
      eq_var  = :hscale
      eq_coe  = :log_hscale
  else
      throw("@eq() is not specified correctly")
  end

  #* -- begin processing the data --------

  pvar = Matrix(sfdata[:, _dicM[eq_var]])  # get the variable vector/Matrix

  value = pvar*_eqncoe[eq_coe]

  if takeexp
     value = exp.(value)
  end

  return value
end


text:
using ITensorTutorials
using Suppressor
using Test

@testset "ITensorTutorials.jl" begin

  @testset "Run tutorials" begin
    tutorials_dir = joinpath(pkgdir(ITensorTutorials), "tutorials")

    is_tutorial(file) = endswith(file, ".jl")

    for file in readdir(tutorials_dir; join=true)
      if is_tutorial(file)
        @suppress include(file)
      end
    end

  end

end


text:
include("sensmat/telegraph.jl")
include("sensmat/poisson.jl")


text:
# Destabilizing polarization as a function of number of attributes.
# Figure 4
using DrWatson
quickactivate(@__DIR__)

using PolarizationFramework

ns = [3, 5, 7, 9, 11, 13, 15]#[9,15,25]
# ns = [3, 7]
reps = Int.([10000, 1000, 100, 100, 100] / 10)
# reps = [10000, 1000]
# reps = Int.(reps/10*9)
reps_dict = Dict(zip(ns, reps))
gs = [5, 11]
threshold = 0.5;
vs = [4, @onlyif("attr_types" == "OA", 1000)] #includes CA

attr_types = ["BA", "UA", "OA", "UPA"]

gammas = [0.5, 1.5, 4, 6]

larger_sizes = [@onlyif(i <= "ns" < 2 * i, i) for i = 1:maximum(ns)]

all_params = @strdict(ns, gs, threshold, vs, attr_types, larger_sizes)
dicts = dict_list(all_params)
[d["reps"] = reps_dict[d["ns"]] for d in dicts] #adding reps
# [d["gammas"] = gammas for d in dicts] #adding gammas

for params in dicts
    n, g, attr_type, v, rep, larger_size = let
        @unpack ns, threshold, reps, gs, attr_types, vs, larger_sizes = params
        ns, gs, attr_types, vs, reps, larger_sizes
    end
    println("Started ", @ntuple(n, g, attr_type, v, larger_size))

    if attr_type == "UA"
        attr = UnorderedAttributes(g, threshold, v)
    elseif attr_type == "BA"
        attr = BinaryAttributes(g)
    elseif attr_type == "OA"
        attr = OrderedAttributes(g, threshold, v)
    elseif attr_type == "UPA"
        attr = UnorderedPositiveAttributes(g, threshold, v)
    else
        throw(attr_type)
    end
    r = using_heider_attr_destab(
        n,
        attr,
        gammas,
        larger_size,
        rep,
        3000.0,
        "Heider7!",
        disp_each = 0,
        disp_more_every = 600,
        save_each = 600,
        files_folder = ["data", "sims"],
        filename_prefix = "DestabFig4",
    )

end

max_stars_repo_name: pjgorski/PolarizationFramework

max_issues_repo_name: pjgorski/PolarizationFramework

max_forks_repo_name: pjgorski/PolarizationFramework


text:
struct CanonicalDescription{T<:AbstractFloat,M<:AbstractMatrix}
  Q::M
  q₁::Vector{T}
  q₂::T
  A::M
  b::Vector{T}
  C̃::M
  c̃::Vector{T}
  lb::Vector{T}
  ub::Vector{T}
  vars::Vector{Symbol}
  name::String

  function (::Type{CanonicalDescription})(qp::MPSDescription{T,M}) where {T<:AbstractFloat,M<:AbstractMatrix}
    c1ind   = isfinite.(qp.c₁)
    c2ind   = isfinite.(qp.c₂)
    m₁, m₂  = sum(c1ind), sum(c2ind)
    m       = m₁ + m₂
    n       = length(qp.vars)
    C̃       = zeros(T, m, n)
    c̃       = zeros(T, m)
    # First, left hand side
    C̃[1:m₁,:]  = -qp.C[c1ind,:]
    c̃[1:m₁]    = -qp.c₁[c1ind]
    # Then, right hand side
    C̃[(m₁+1):end,:]  = qp.C[c2ind,:]
    c̃[(m₁+1):end]    = qp.c₂[c2ind]

    new{T,M}(qp.Q, qp.q₁, qp.q₂, qp.A, qp.b, C̃, c̃, qp.lb, qp.ub, qp.vars, qp.name)
  end

  function (::Type{CanonicalDescription})(qp::MPSDescription{T,M}) where {T<:AbstractFloat,M<:AbstractSparseMatrix}
    c1ind   = isfinite.(qp.c₁)
    c2ind   = isfinite.(qp.c₂)
    m₁, m₂  = sum(c1ind), sum(c2ind)
    m       = m₁ + m₂
    n       = length(qp.vars)
    C̃ᵢ      = Vector{Int}()
    C̃ⱼ      = Vector{Int}()
    C̃ᵥ      = Vector{T}()
    c̃       = zeros(T, m)

    rows = rowvals(qp.C)
    vals = nonzeros(qp.C)
    for col = 1:size(qp.C,2)
      for j in nzrange(qp.C, col)
        row = rows[j]
        if c1ind[row]
          idx = count(i->i,c1ind[1:row])
          push!(C̃ᵢ,idx)
          push!(C̃ⱼ,col)
          push!(C̃ᵥ,-vals[j])
          c̃[idx] = -qp.c₁[row]
        end
        if c2ind[row]
          idx = count(i->i,c2ind[1:row])
          push!(C̃ᵢ,m₁+idx)
          push!(C̃ⱼ,col)
          push!(C̃ᵥ,vals[j])
          c̃[m₁+idx] = qp.c₂[row]
        end
      end
    end
    C̃ = sparse(C̃ᵢ, C̃ⱼ, C̃ᵥ, m, n)

    new{T,M}(qp.Q, qp.q₁, qp.q₂, qp.A, qp.b, C̃, c̃, qp.lb, qp.ub, qp.vars, qp.name)
  end
end

"""
    canonical(mps::MPSDescription) -> canon

Transform the `mps` object as returned from `parseqps` into `canon`, an object
of type `CanonicalDescription`.

`canon` contains the description of the following optimization problem formulation:

```
minimize    0.5*x'*Q*x + q₁'*x + q₂
subject to       A*x = b
                 C̃*x ≤ c̃
            lb ≤  x  ≤ ub
```

where all the matrices and vectors are of type `T`, where `T` is the first
argument in `parseqps`.

See also: `name`, `variables`, `objective`, `equalities`, `inequalities`,
  `bounds` and `parseqps`.
"""
canonical(qp::MPSDescription{T}) where {T<:AbstractFloat}     =
  CanonicalDescription(qp)

name(qp::CanonicalDescription)          = qp.name
variables(qp::CanonicalDescription)     = qp.vars
objective(qp::CanonicalDescription)     = (qp.Q, qp.q₁, qp.q₂ )
equalities(qp::CanonicalDescription)    = (qp.A, qp.b         )
inequalities(qp::CanonicalDescription)  = (qp.C̃, qp.c̃         )
bounds(qp::CanonicalDescription)        = (qp.lb, qp.ub       )

function _compact(stream, ::MIME"text/plain", qp::CanonicalDescription)
  print(stream, "QP")
end

function _full(stream, ::MIME"text/plain", qp::CanonicalDescription)
  println(stream, _header(qp))
  println(stream, "---")
  println(stream, "  minimize    0.5*x'*Q*x + q₁'*x + q₂")
  println(stream, "  subject to       A*x = b")
  println(stream, "                   C̃*x ≤ c̃")
  println(stream, "              lb ≤  x  ≤ ub")
  print(stream, "---")
end

_header(qp::CanonicalDescription) = string("QP instance: ", name(qp), ".")
_header(qp::CanonicalDescription{T,M}) where {T<:AbstractFloat,M<:AbstractSparseMatrix} = string("Sparse QP instance: ", name(qp), ".")

show(stream::IO, qp::CanonicalDescription)                          =
  show(stream, MIME("text/plain"), qp)
show(stream::IO, mime::MIME"text/plain", qp::CanonicalDescription)  =
  get(stream, :compact, false) ? _compact(stream, mime, qp) : _full(stream, mime, qp)


text:
using Test, AhoCorasickAutomatons
import Random

# @testset "Brute test." begin
#     keys = Vector{String}()
#     open("../data/dict2") do io
#         for w in eachline(io)
#             push!(keys, w)
#         end
#     end
#     Ti = UInt32
#     obj = AhoCorasickAutomaton{Ti}(keys; sort = true)
# end

@testset "From and to Dict ops is correct." begin
    keys1 = Dict{String, Int}()
    for i = 1:10
        key = Random.randstring(rand(0:10))
        keys1[key] = i
    end
    obj = AhoCorasickAutomaton{UInt16}(keys1)
    @test length(keys1) == length(obj)
    keys2 = Dict(collect(obj))
    @test keys1 == keys2
end

@testset "Inserted in, not inserted not in." begin
    keys1 = Vector{String}()
    for i = 1:10
        key = Random.randstring(rand(0:10))
        push!(keys1, key)
    end
    keys2 = Vector{String}()
    for i = :10
        key = Random.randstring(rand(0:10))
        push!(keys2, key)
    end
    obj = AhoCorasickAutomaton(keys1)
    for key in keys1
        @test key in obj
    end
    for key in keys2
        @test key ∉ keys1 && key ∉ obj || key ∈ keys1 && key ∈ obj
    end
end

@testset "Result of eachmatch is correct." begin
    s = Random.randstring("AB", 10)
    keys = Vector{String}()
    for i = 1:10
        key = Random.randstring("AB", rand(1:10))
        push!(keys, key)
    end
    sort!(keys)
    unique!(keys)
    obj = AhoCorasickAutomaton(keys)
    res1 = sort!(collect(eachmatch(obj, s)))
    res2 = Vector{ACMatch}()
    res3 = sort!(map(x -> s[x], res1))
    res4 = String[]
    for i = 1:length(keys)
        key = keys[i]
        for j = 1:length(s) - length(key) + 1
            if s[j:j + length(key) - 1] == key
                push!(res2, ACMatch(j, j + length(key) - 1, i))
                push!(res4, key)
            end
        end
    end
    sort!(res2)
    sort!(res4)
    @test begin
        res = res1 == res2 && res3 == res4
        if !res
            @show s keys
            flush(stdout)
        end
        res
    end
end

@testset "Read and write is correct." begin
    keys = Vector{String}()
    for i = 1:10
        key = Random.randstring("AB", rand(0:10))
        push!(keys, key)
    end
    obj = AhoCorasickAutomaton(keys)
    io = IOBuffer()
    nbytes = write(io, obj)
    @test nbytes == sizeof(io.data)
    seek(io, 0)
    obj2 = read(io, AhoCorasickAutomaton)
    @test obj == obj2
end


text:
if haskey(ENV, "GITHUB_ACTIONS")
    # We're being run as part of a Github action. The most likely case is that
    # this is the auto-merge action as part of the General registry.
    # For now, we're going to silently skip the tests.
    @info("Detected a Github action. Skipping tests.")
    exit(0)
end

using Gurobi, Test, Random

@testset "C API" begin
    include("c_wrapper.jl")
end

@testset "MathProgBase Tests" begin
    @testset for file in ["env", "large_coefficients", "range_constraints",]
        evalfile(joinpath("MathProgBase", "$(file).jl"))
    end
    @testset "MathProgJuMP" begin
        evalfile(joinpath("MathProgBase", "mathprog.jl"))
    end
end

@testset "MathOptInterface Tests" begin
    @testset "$(file)" for file in readdir("MOI")
        include(joinpath("MOI", file))
    end
end


text:
# import Base:
#     string,
#     Sys,
#     char,
#     CPU_CORES

XSH_BIN = "xsh"
XSH_SYMBOL = string(char(0xb83))
XSH_PROGRAM = "$(XSH_BIN).$(XSH_SYMBOL)"
XSH_DESCRIPTION = "$(XSH_PROGRAM) v$(VERSION)"
XSH_DESCRIPTION_LONG = "$(XSH_PROGRAM) v$(VERSION) [$(Sys.MACHINE)-cpu$(CPU_CORES)]"
XSH_ARG_DESCRIPTION = "xsh. A modern shell environment written in Julia language."
XSH_ARG_EPILOGUE = ""
XSH_ARG_USAGE = "usage: xsh [-hvqVdHilmnEce] <cmd> [script]"

max_stars_repo_name: tmaone/xsh

max_issues_repo_name: turing-complete/xsh

max_forks_repo_name: turing-complete/xsh


text:
function overlap_cuboid(	xAB::Float64,
						yAB::Float64,
						zAB::Float64,
						a11::Float64,
						a12::Float64,
						a13::Float64,
						a21::Float64,
						a22::Float64,
						a23::Float64,
						a31::Float64,
						a32::Float64,
						a33::Float64,
						b11::Float64,
						b12::Float64,
						b13::Float64,
						b21::Float64,
						b22::Float64,
						b23::Float64,
						b31::Float64,
						b32::Float64,
						b33::Float64,
						r1a::Float64,
						r2a::Float64,
						r3a::Float64,
						r1b::Float64,
						r2b::Float64,
						r3b::Float64)

	overlapfun::Float64 = 0.0
	overlapfun_binary::Float64 = 0.0

	overlapfun_binary = overlap_cuboid_binary(xAB, yAB, zAB, a11, a12, a13, a21, a22, a23, a31, a32, a33, b11, b12, b13, b21, b22, b23, b31, b32, b33, r1a, r2a, r3a, r1b, r2b, r3b)

	if overlapfun_binary == 0.0
		return 0.0
	else
		gamma_min::Float64 = 0.0
		gamma_max::Float64 = 1.0
		gamma::Float64 = 0.5
		number_of_iterations::Int64 = 10

		for current_iteration = 1:number_of_iterations
			if overlap_cuboid_binary(xAB, yAB, zAB, a11, a12, a13, a21, a22, a23, a31, a32, a33, b11, b12, b13, b21, b22, b23, b31, b32, b33, gamma * r1a, gamma * r2a, gamma * r3a, gamma * r1b, gamma * r2b, gamma * r3b) == 1.0
				gamma_max = gamma
			else
				gamma_min = gamma
			end
			gamma = 0.5 * (gamma_min + gamma_max)
		end

		overlapfun = 1.0 - gamma
	end
end

# xAB = 0.01
# yAB = xAB
# zAB = xAB
# a11 = 1.0
# a12 = 0.0
# a13 = 0.0
# a21 = 0.0
# a22 = 1.0
# a23 = 0.0
# a31 = 0.0
# a32 = 0.0
# a33 = 1.0
# b11 = 1.0
# b12 = 0.0
# b13 = 0.0
# b21 = 0.0
# b22 = 1.0
# b23 = 0.0
# b31 = 0.0
# b32 = 0.0
# b33 = 1.0
# r1a = 1.0
# r2a = 0.75
# r3a = 1.0
# r1b = 1.0
# r2b = 1.5
# r3b = 2.0

# overlap_cuboid(	xAB,
				# yAB,
				# zAB,
				# a11,
				# a12,
				# a13,
				# a21,
				# a22,
				# a23,
				# a31,
				# a32,
				# a33,
				# b11,
				# b12,
				# b13,
				# b21,
				# b22,
				# b23,
				# b31,
				# b32,
				# b33,
				# r1a,
				# r2a,
				# r3a,
				# r1b,
				# r2b,
				# r3b)

max_stars_repo_name: roding/whitefish

max_issues_repo_name: roding/whitefish

max_forks_repo_name: roding/whitefish