Просмотр исходного кода

[research/crypsinous/linearindependence] finalize research with conclusion

mohab metwally 3 лет назад
Родитель
Сommit
557d1684b0

+ 4 - 0
script/research/crypsinous/linearindependence/README.md

@@ -10,3 +10,7 @@ excluding use of floats, and division, only +,-,* are allowed.
 # comparison of original target to approximation
 
 ![alt text](https://github.com/darkrenaissance/darkfi/blob/master/script/research/crypsinous/linearindependence/plot.png?raw=true)
+
+# conclusion
+
+as the derivative of deltas graph shows, starting for term 2, the derivatives is ~ 0, and it's the optimal number of terms in approximation accuracy that has the least number of terms.

+ 64 - 27
script/research/crypsinous/linearindependence/main.py

@@ -5,53 +5,90 @@ import random
 
 L = 28948022309329048855892746252171976963363056481941560715954676764349967630337
 
+# crypsinous original target function
 def target(f, rel_stake):
     T  = L * (1 - (1-f)**rel_stake)
     return T
 
-def approx_target_in_zk(sigma_1, sigma_2, stake):
+# naive factorial
+def fact(n):
+    assert (n>0)
+    n = int(n)
+    if n==1:
+        return 1
+    elif n==2:
+        return 2
+    else:
+        return n * fact(n-1)
+
+
+# all inputs to this function are integers
+# sigmas are public
+# stake is private
+def approx_target_in_zk(sigmas, stake):
     # both sigma_1, sigma_2 are constants, if f is a constant.
     # if f is constant then sigma_12, sigma_2
     # this dictates that tuning need to be hardcoded,
     # secondly the reward, or at least the total stake in the network,
     # can't be anonymous, should be public.
-    T = sigma_1 * stake + sigma_2*stake**2
-    return T
+    T = [sigma*stake**(i+1) for i, sigma in enumerate(sigmas)]
+    return sum(T)
 
-def approx_target(f, stake, Sigma):
-    x = (1-f)
-    c = math.log(x)
-    k = L*c
-    kp = k*c
-    kpp = kp/2
-    # approx sigma
-    sigma_1 = -1 * k/Sigma
-    sigma_2 = -1 * kpp/(Sigma**2)
-    # sigma is in Z
-    sigma_2 = int(sigma_2)
-    sigma_1 = int(sigma_1)
-    stake = int(stake)
-    return approx_target_in_zk(sigma_1, sigma_2, stake)
+# approximation of crypsinous targt
+def approx_target(c, stake, Sigma, k):
+    sigmas = [int((c/Sigma)**i * (L/fact(i))) for i in range(1, k+1)]
+    return -1*approx_target_in_zk(sigmas, stake)
 
 
 f = 0.5
+x = (1-f)
+c = math.log(x)
 # let's assume stakeholde having 1% of the stake, 1/100.
 # each iteration increases stake by value 1.
 TOTAL = 10000
 S = []
 stake = 0
+targets = []
 T = []
-T_approx = []
+T_approx_2term = []
+T_approx_3term = []
+T_approx_5term = []
+k=7
+
 for i in range(TOTAL):
-    if random.random()>=0.9:
+    if random.random() >= 0.9:
         stake+=1
     S+=[(stake, i+1.0)]
+    col = []
     t = target(f, stake/(i+1.0))
-    T+=[t]
-    t_approx = approx_target(f, stake, (i+1.0))
-    T_approx+=[t_approx]
-
-plt.plot(T)
-plt.plot(T_approx)
-plt.legend(["target", "approximation"])
-plt.savefig('plot.png')
+    col += [t]
+    for j in range(1,k+1):
+        t_approx = approx_target(c, stake, (i+1.0), j)
+        col += [t_approx]
+    targets +=[col]
+
+targets = np.array(targets).T
+
+plt.subplot(2,1,1)
+
+plt.plot(targets[0])
+
+START=1
+
+for i in range(START,k+1):
+    plt.plot(targets[i])
+
+plt.legend(["target"] + ["{} terms".format(i) for i in range(START,k+1)], loc='upper right')
+
+Deltas = [0]
+for j in range(START,k+1):
+    diff = np.array(targets[j])-np.array(targets[j-1])
+    delta = np.sum(diff)
+    Deltas += [delta]
+
+plt.subplot(2,1,2)
+Deltas_derivates = np.poly1d(Deltas)
+plt.plot(Deltas)
+plt.plot(Deltas_derivates.deriv())
+plt.legend(["delta", "derivative"], loc='upper right')
+plt.savefig("target.png")

BIN
script/research/crypsinous/linearindependence/plot.png


BIN
script/research/crypsinous/linearindependence/target.png