|
| 1 | +using PyPlot |
| 2 | +using Statistics |
| 3 | + |
| 4 | +Statistics.mean(x::Real,y::Real) = (x+y)/2 |
| 5 | + |
| 6 | +x = [0.5,1.0,2.0] |
| 7 | +xint = [mean(x[i],x[i+1]) for i in 1:length(x)-1] |
| 8 | + |
| 9 | +fig,(ax1,ax2,ax3,ax4,ax5) = subplots(5,1,figsize=(5,5),sharex=true) |
| 10 | +axs = [ax1,ax2,ax3,ax4,ax5] |
| 11 | + |
| 12 | +# BITSHAVE |
| 13 | +for i in 1:length(x) |
| 14 | + ax1.scatter(x[i],0,20,color="C$(i-1)",edgecolor="k",zorder=10) |
| 15 | +end |
| 16 | + |
| 17 | +ax1.plot([0,x[1]],[0,0],"C5") |
| 18 | +ax1.plot([x[1],x[2]],[0,0],"C0") |
| 19 | +ax1.plot([x[2],x[3]],[0,0],"C1") |
| 20 | +ax1.plot([x[3],3],[0,0],"C2") |
| 21 | +ax1.set_yticks([]) |
| 22 | +ax1.set_title("Bitshave (round-to-zero)",loc="left") |
| 23 | + |
| 24 | +ax1.plot([1.2,1.2],[-0.2,0.2],"k--",lw=0.5) |
| 25 | +ax1.text(1.22,-0.2,L"x is round to $x_2$") |
| 26 | + |
| 27 | +# BITSET |
| 28 | +for i in 1:length(x)-1 |
| 29 | + ax2.scatter(x[i+1]-0.02,0,20,color="C$(i-1)",edgecolor="k",zorder=10) |
| 30 | +end |
| 31 | + |
| 32 | +ax2.plot([0,x[1]],[0,0],"C5") |
| 33 | +ax2.plot([x[1],x[2]],[0,0],"C0") |
| 34 | +ax2.plot([x[2],x[3]],[0,0],"C1") |
| 35 | +ax2.plot([x[3],3],[0,0],"C2") |
| 36 | +ax2.set_yticks([]) |
| 37 | +ax2.set_title("Bitset (round-away-from-zero)",loc="left") |
| 38 | + |
| 39 | +ax2.plot([1.2,1.2],[-0.2,0.2],"k--",lw=0.5) |
| 40 | +ax2.text(1.22,-0.2,L"x is round to $x_3^-$") |
| 41 | + |
| 42 | +# HALFSHAVE |
| 43 | +for i in 1:length(xint) |
| 44 | + ax3.scatter(xint[i],0,20,color="C$(i-1)",edgecolor="k",zorder=10) |
| 45 | +end |
| 46 | + |
| 47 | +ax3.plot([0,x[1]],[0,0],"C5") |
| 48 | +ax3.plot([x[1],x[2]],[0,0],"C0") |
| 49 | +ax3.plot([x[2],x[3]],[0,0],"C1") |
| 50 | +ax3.plot([x[3],3],[0,0],"C2") |
| 51 | +ax3.set_yticks([]) |
| 52 | +ax3.set_title("Halfshave",loc="left") |
| 53 | + |
| 54 | +ax3.plot([1.2,1.2],[-0.2,0.2],"k--",lw=0.5) |
| 55 | +ax3.text(1.22,-0.2,L"x is round to $x_2^*$") |
| 56 | + |
| 57 | +# ROUND NEAREST |
| 58 | +for i in 1:length(x) |
| 59 | + ax4.scatter(x[i],0,20,color="C$(i-1)",edgecolor="k",zorder=10) |
| 60 | +end |
| 61 | + |
| 62 | +ax4.plot([0,xint[1]],[0,0],"C0") |
| 63 | +ax4.plot([xint[1],xint[2]],[0,0],"C1") |
| 64 | +ax4.plot([xint[2],3],[0,0],"C2") |
| 65 | +ax4.set_yticks([]) |
| 66 | +ax4.set_title("Round-to-nearest",loc="left") |
| 67 | + |
| 68 | +ax4.plot([1.2,1.2],[-0.2,0.2],"k--",lw=0.5) |
| 69 | +ax4.text(1.22,-0.2,L"x is round to $x_2$") |
| 70 | + |
| 71 | +# STOCHASTIC ROUNDING |
| 72 | +ax5.plot([-0.5,0,x[1]],[0,1,0],"C5") |
| 73 | +ax5.plot([0,x[1],x[2]],[0,1,0],"C0") |
| 74 | +ax5.plot([x[1],x[2],x[3]],[0,1,0],"C1") |
| 75 | +ax5.plot([x[2],x[3],4],[0,1,0],"C2") |
| 76 | +ax5.plot([x[3],4,5],[0,1,0],"C4") |
| 77 | + |
| 78 | +for i in 1:length(x) |
| 79 | + ax5.scatter(x[i],1,20,color="C$(i-1)",edgecolor="k",zorder=10) |
| 80 | +end |
| 81 | + |
| 82 | +ax5.plot([1.2,1.2],[0,1],"k--",lw=0.5) |
| 83 | +ax5.text(1.18,-0.3,L"x = ") |
| 84 | +ax5.text(1.31,-0.15,L"$x_2$ at 80%") |
| 85 | +ax5.text(1.31,-0.45,L"$x_3$ at 20% chance.") |
| 86 | + |
| 87 | +ax5.set_xlim(x[1]-0.2,x[3]+0.2) |
| 88 | +ax5.set_yticks([0,1]) |
| 89 | +ax5.set_ylim([-0.1,1.1]) |
| 90 | +ax5.set_yticklabels([L"0\%",L"100\%"]) |
| 91 | +ax5.set_xticks(x) |
| 92 | +ax5.set_xticklabels([L"$x_1$",L"$x_2$",L"$x_3$"]) |
| 93 | +ax5.set_title("Stochastic rounding",loc="left") |
| 94 | + |
| 95 | +for (iax,ax) in enumerate(axs) |
| 96 | + ax.set_title(string(Char(iax+96)),loc="right",fontweight="bold") |
| 97 | + ax.set_frame_on(false) |
| 98 | +end |
| 99 | + |
| 100 | +tight_layout() |
| 101 | +savefig("figs/schematic_all.pdf") |
| 102 | +savefig("figs/schematic_all.png",dpi=200) |
0 commit comments