I have three variables Min=0.29, Max=6.52 and center = 2.10. I wish to create a table that distributes this data into 100 values in a table format in the following fashion:
Here, this image can be split into two parts 0 to 50 and 50 to 100.
In the first part, the increase in x vs y for subsequent value is higher between 1-10 vs 10-20 and higher in 10-20 vs 20-30 and so on.
In the second part, the increase in x vs y for subsequent value is lower between 50-60 vs 60-70 and lower between 60-70 vs 70-80 and so on.
Now, I don't have high proficiency in statistics hence was unable to figure out how to provide min, max and centre value to an exponential distribution and how to implement it in python.
I tried using solution given in link, but couldn't get it to work for my case. Any help will be appreciated.
Each of the two exponential functions is defined by 3 parameters, but you only have 2 points belonging to each. One possibility is to provide the asymptotic value for both functions. I'll paste my code here, including the derivation of all formulae, for lack of time – sorry:
from math import exp, log
from matplotlib import pyplot as plt
X_MIN, X_CTR, X_MAX = 1, 50, 100
Y_MIN, Y_CTR, Y_MAX = 0.29, 2.10, 6.52
c1 = float(input(f"c1 (> {Y_CTR}): "))
c2 = float(input(f"c2 (< {Y_CTR}): "))
plot = input("plot? (y|n): ")[:1] in "yY"
# c1 - a1 * exp(-b1 * X_MIN) == Y_MIN # with a1 > 0, b1 > 0, c1 > Y_CTR
# c1 - a1 * exp(-b1 * X_CTR) == Y_CTR
# c2 + a2 * exp( b2 * X_CTR) == Y_CTR # with a2 > 0, b2 > 0, c2 < Y_CTR
# c2 + a2 * exp( b2 * X_MAX) == Y_MAX
# a1 * exp(-b1 * X_MIN) == c1 - Y_MIN
# a1 * exp(-b1 * X_CTR) == c1 - Y_CTR
# a2 * exp( b2 * X_CTR) == Y_CTR - c2
# a2 * exp( b2 * X_MAX) == Y_MAX - c2
# log(a1) - b1 * X_MIN == log(c1 - Y_MIN)
# log(a1) - b1 * X_CTR == log(c1 - Y_CTR)
# log(a2) + b2 * X_CTR == log(Y_CTR - c2)
# log(a2) + b2 * X_MAX == log(Y_MAX - c2)
# b1 * (X_CTR - X_MIN) == log(c1 - Y_MIN) - log(c1 - Y_CTR)
# b2 * (X_MAX - X_CTR) == log(Y_MAX - c2) - log(Y_CTR - c2)
b1 = (log(c1 - Y_MIN) - log(c1 - Y_CTR)) / (X_CTR - X_MIN)
b2 = (log(Y_MAX - c2) - log(Y_CTR - c2)) / (X_MAX - X_CTR)
# log(a1) == log(c1 - Y_MIN) + b1 * X_MIN
# log(a2) == log(Y_MAX - c2) - b2 * X_MAX
a1 = exp(log(c1 - Y_MIN) + b1 * X_MIN)
a2 = exp(log(Y_MAX - c2) - b2 * X_MAX)
x_lst = list(range(X_MIN, X_MAX+1))
y_lst = [c1 - a1 * exp(-b1 * x) if x < X_CTR else
c2 + a2 * exp( b2 * x) for x in x_lst]
if plot:
plt.plot(x_lst, y_lst)
plt.grid(True)
plt.show()
else:
for x, y in zip(x_lst, y_lst):
print(f"{x},{y:.14}")
E.g., with this input:
c1 (> 2.1): 2.13
c2 (< 2.1): 2.08
plot? (y|n): y
the output is:
Related
There is one combination of b1, b2, b3, r1, r2, r3 which meets the following equations:
b1 = 4 * r1
b2 = 4 * r2
b3 = 4 * r3
b1 = b2 + b3
r1 = r2 + r3
I have been trying to write a piece of python code to find that combination (or maybe there is more than one). The piece of code has to go through various permutations to find the right combination. I was struggling to write the code so hopefully someone can help me out. This is what i started off with:
for b1 in range(9,50):
for b2 in range(9,50):
for b3 in range(5,20):
for r1 in range(5,20):
for r2 in range(5,20):
for r3 in range(5,20):
if b1 == 4 * r1 & b2 == 4 * r2 & b3 == 4 * r3 & b1 == b2 + b3 & r1 == r2 + r3:
print(b1, b2, b3, r1, r2, r3)
Unless I've completely missed what the desired outcome is supposed to be from the equations above, there are countless permutations that produce a true comparison of values for "r1", "r2", "r3", "b1", "b2", and "b3". That is because the equation:
b1 = b2 + b3
is equivalent to:
4 * r1 = 4 * (r2 + r3)
So once you determine what value of "r1" is equal to "r2 + r3", the "b1" equation is going to follow suit.
I built the following proof of principle program to test that out using just eighty-one combinations of values for "r2" and "r3" and solving for "r1" with a check of "b1".
r1 = 0
r2 = 0
r3 = 0
b1 = 0
b2 = 0
b3 = 0
print("Valid combinations of r1/r2/r3")
print("______________________________________________________________")
for r2 in range (1, 10):
for r3 in range (1, 10):
r1 = r2 + r3
b1 = 4 * r1
b2 = 4 * r2
b3 = 4 * r3
if (b1 == (4 * (r2 + r3))):
print("r1:", r1, "r2:", r2, "r3:", r3)
print("r1 = r2 + r3, which is", r1, "=", r2, "+", r3)
print("b1 =", b1, "b2 =", b2, "b3 =", b3)
print("b1 = b2 + b3, which is", b1, "=", b2, "+", b3)
print("- - - - - - - - - - - - -")
That yielded eighty-one instances where all of the parameters are met.
r1: 17 r2: 9 r3: 8
r1 = r2 + r3, which is 17 = 9 + 8
b1 = 68 b2 = 36 b3 = 32
b1 = b2 + b3, which is 68 = 36 + 32
- - - - - - - - - - - - -
r1: 18 r2: 9 r3: 9
r1 = r2 + r3, which is 18 = 9 + 9
b1 = 72 b2 = 36 b3 = 36
b1 = b2 + b3, which is 72 = 36 + 36
- - - - - - - - - - - - -
The exercise almost seems more of a proof that if the first four equations are true, the fifth equation is true.
Hi thank you for your time. I just realized I needed a different set of equations. The problem is about blue and red tiles. A child has a huge amount of blue tiles and red tiles, the number is unknown. He uses all the blue tiles to form a rectangle and the red tiles to form a border around the rectangle of 1 tile thickness, he has 4 times as many blues tiles as red tiles. He then messes us the rectangle and forms 2 smaller (unidentical) rectangles using all of the tiles. Each rectangle follows the same convention of blue rectangle and red border consisting of 1 tile thickness. The question asks to find the number of blue and red tiles for each rectangle (big/medium/small). Algebraically I only see 5 equations which can be formed but 6 unknowns so I have no idea how to solve it algebraically but I realized I can use brute force to get the answer using python. In the end, I slept on it for a few days and tried again just now and got the answer.
This was a question my 14yr cousin got as his math homework, haha. I still couldn't work it out algebraically, I guess his teacher will find the solution. Thank you again for your time and apologies I only later realized the equations needed to be amended.
The below solves the answer.
for w1 in range(10, 100):
for w2 in range(10, 50):
for w3 in range(10, 25):
for l1 in range(5, 15):
for l2 in range(5, 15):
for l3 in range(5, 15):
if (w1 * l1 == (w2 * l2) + (w3 * l3)) and \
(((w1 * l1) / 4) == ((w2 * l2) / 4) + ((w3 * l3) / 4)) and \
(w1 * l1 == (8 * w1) + (8 * l1) + 16) and \
(w2 * l2 == (8 * w2) + (8 * l2) + 16) and \
(w3 * l3 == (8 * w3) + (8 * l3) + 16):
print("blue tiles for big rectangle is", int(w1 * l1))
print("red tiles for big rectangle is", int((w1 * l1) /4))
print("blue tiles for medium rectangle is", int(w2 * l2))
print("red tiles for medium rectangle is", int((w2 * l2) /4))
print("blue tiles for small rectangle is", int(w3 * l3))
print("red tiles for small rectangle is", int((w3 * l3) /4))
else:
None
This code I wrote in Sympy is running slow. I want to write this with symengine. How can I translate? I had some difficulty with the Solve commands. Can you help me ?
Edit: Here is my code:
import sympy as sy
import time
#import numpy as np
import math as mat
from sympy import Eq
testere_capi=197
dis_sayisi=78
ic_acisi = 16
sirt_acisi = 8
derinlik_carpani = 0.4
kucuk_daire_carpani = 0.25
buyuk_daire_carpani = 0.8
son_dogrunun_carpani = 0.06
tas_kalinligi = 2.0
T = ((testere_capi * mat.pi) / dis_sayisi) # hatve
H = T * derinlik_carpani # derinlik
x = sy.symbols("x")
y = sy.symbols("y")
D2 = sy.Eq(y, H)
a8 = 0
b8 = 0
m_d1 = mat.tan(mat.radians(90 - ic_acisi))
D1 = sy.Eq(y - b8, m_d1 * (x - a8))
S1 = sy.solve((D2, D1), (x, y))
a1 = S1[x]
b1 = S1[y]
b2 = T * kucuk_daire_carpani
a2 = b2 / mat.tan(mat.radians(90 - ic_acisi) / 2)
r1 = T * kucuk_daire_carpani
D3 = sy.Eq((x - a2) ** 2 + (y - b2) ** 2, r1 ** 2)
D1 = sy.expand(D1)
D3 = sy.expand(D3)
S7 = sy.solve((D1,D3),(x,y))
a7 = S7[0][0]
b7 = S7[0][1]
I have two distributions over two parameters h and t. h is Weibull distributed while t is conditioned on h and it is log-normal distributed:
f_H = np.exp(-(h / alpha) ** beta) * (beta / alpha) * (h / alpha) ** (beta - 1)
f_TIH = np.exp(-(np.log(t) - mu_h) ** 2.0 / (2.0 * sigma_h ** 2)) / (t * sigma_h * np.sqrt(2.0 * np.pi))
where:
mu_h = a0 + a1 * h ** a2
sigma_h = b0 + b1 * np.exp(b2 * h)
and:
a0 = 0.7
a1 = 0.282
a2 = 0.167
b0 = 0.07
b1 = 0.3449
b2 = -0.2073
alpha = 1.76
beta = 1.59
The joint PDF for h and t is then given as:
f_joint = f_H * f_TIH
My question is how can I sample random values for h and t from the joint PDF?
I am having issues calculating a function, while the function itself is pretty straightforward.
I have the following dataframe:
import pandas as pd
import numpy as np
import math as m
from scipy.stats import norm
dff = pd.DataFrame({'SKU': ['001', '002', '003','004','005'],
'revenue_contribution_in_percentage': [0.2, 0.2, 0.3,0.1,0.2],
'BuyPrice' : [7.78,9.96,38.87,6.91,14.04],
'SellPrice' : [7.9725,12.25,43,7.1,19.6],
'margin' : [0.9725,2.2908,5.8305,0.2764,5.1948],
'Avg_per_week' : [71.95,75.65,105.7,85.95,66.1],
'StockOnHand' : [260,180,260,205,180],
'StockOnOrder': [0,0,0,0,0],
'Supplier' : ['ABC', 'ABC', 'ABC','ABC','ABC'],
'SupplierLeadTime': [12,12,12,12,12],
'cumul_value':[0.20,0.4,0.6,0.8,1],
'class_mention':['A','A','B','D','C'],
'std_week':[21.585,26.4775,21.14,31.802, 26.44],
'review_time' : [5,5,5,5,5],
'holding_cost': [0.35, 0.35, 0.35,0.35,0.35],
'aggregate_order_placement_cost': [1000, 1000,1000,1000,1000],
'periods' : [7,7,7,7,7]})
dff['holding_cost'] = 0.35
dff1 = dff.sort_values(['Supplier'])
df2 = pd.DataFrame(dff1)
df2['forecast_dts'] = 5
df2['sigma_rtlt'] = 0.5
i need passing some of this parameters into the function:
#
a0 = -5.3925569
a1 = 5.6211054
a2 = -3.883683
a3 = 1.0897299
b0 = 1
b1 = -0.72496485
b2 = 0.507326622
b3 = 0.0669136868
b4 = -0.00329129114
z = np.sqrt(np.log(25
/
(norm.pdf((df2['forecast_dts'])*(1-0.98)/df2['sigma_rtlt']) -
((df2['forecast_dts']*(1-0.98)/df2['sigma_rtlt']))* (1-norm.cdf(df2['forecast_dts']*(1-0.98)/df2['sigma_rtlt']))) ^ 2))
num = (a0 + a1 * z + a2 * z ^ 2 + a3 * z ^ 3)
den = (b0 + b1 * z + b2 * z ^ 2 + b3 * z ^ 3 + b4 * z ^ 4)
k = num / den
return k
but then calculating
calc = calc_invUnitNormalLossApprox()*df2['sigma_rtlt']
returns the error:
File "/usr/local/lib/python3.7/site-packages/pandas/core/ops/__init__.py", line 1280, in na_op
dtype=x.dtype, typ=type(y).__name__
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
At this point I am not sure what is going on there, especially because i know the formula itself is correct, I am assuming there is something wrong with my use of norm pdf and cdf but I couldnt figure it out.
Any help would be really appreciated.
I think with the ^ operator you are trying to do a bitwise XOR
I think you need to use the ** operator.
This code works
def calc():
a0 = -5.3925569
a1 = 5.6211054
a2 = -3.883683
a3 = 1.0897299
b0 = 1
b1 = -0.72496485
b2 = 0.507326622
b3 = 0.0669136868
b4 = -0.00329129114
z = np.sqrt(np.log(25
/
(norm.pdf((df2['forecast_dts'])*(1-0.98)/df2['sigma_rtlt']) -
((df2['forecast_dts']*(1-0.98)/df2['sigma_rtlt']))* (1-norm.cdf(df2['forecast_dts']*(1-0.98)/df2['sigma_rtlt']))) ** 2))
num = (a0 + a1 * z + a2 * z ** 2 + a3 * z ** 3)
den = (b0 + b1 * z + b2 * z ** 2 + b3 * z ** 3 + b4 * z ** 4)
k = num / den
return k
Not : I have change the ^ operator to **
this is my first time with sympy and I don't know what I'm doing wrong. Could you help me please?
This is my code:
from __future__ import division
from sympy import *
q, l, p = symbols('q, l, p')
x1, x2, x3, x4, x5, x6, x7 = symbols('x1, x2, x3, x4, x5, x6, x7')
c1, c2, c3, c4, c5, c6, c7 = symbols('c1, c2, c3, c4, c5, c6, c7')
c8, c9, c10, c11, c12, c13, c14 = symbols('c8, c9, c10, c11, c12, c13, c14')
a = 209 * q * l / 42 - p / 3
"TRECHO AB"
m1 = a * x1 - q * x1 ** 2 / 2
d1 = integrate(m1, x1) + c1
y1 = integrate(d1, x1) + c2
"TRECHO BC"
m2 = a * 2 * l + a * x2 - 2 * q * l(x2 + l)
d2 = integrate(m2, x2) + c3
y2 = integrate(d2, x2) + c4
"TRECHO CD"
m3 = a(3 * l + x3) - 2 * q * l(2 * l + x3) - q * x3 ** 3 / 6 * l
d3 = integrate(m3, x3) + c5
y3 = integrate(d3, x3) + c6
"TRECHO DE"
m4 = a(x4 + 6 * l) - 2 * q * l(x4 + 5 * l) - (9 * q * l / 2)(x4 + l)
d4 = integrate(m4, x4) + c7
y4 = integrate(d3, x4) + c8
"TRECHO FG"
m5 = a(x5 + 7 * l) - 2 * q * l(x5 + 6 * l) - (9 * q * l / 2)(x5 + 2 * l) - p * x5
d5 = integrate(m5, x5) + c9
y5 = integrate(d5, x5) + c10
"TRECHO GH"
m6 = a(x6 + 15 * l / 2) - 2 * q * l(x6 + 13 * l / 2) - (9 * q * l / 2)(x6 + 5 * l / 2) - p(x6 + l / 2) - 2 * q * x6(
x6 / 2)
d6 = integrate(m6, x6) + c11
y6 = integrate(d6, x6) + c12
"TRECHO HI"
m7 = a(x7 + 19 * l / 2) - 2 * q * l(x7 + 17 * l / 2) - (9 * q * l / 2)(x7 + 9 * l / 2) - p(x7 + 5 * l / 2) - 4 * q * l(
x7 + l) - (3 * q * x7 ** 2 / 2 * l)(x7 / 3)
d7 = integrate(m7, x7) + c13
y7 = integrate(d7, x7) + c14
print(
"{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{},\n{}".format(d1, y1, d2, y2, d3, y3, d4, y4, d5, y5,
d6, y6, d7, y7))
The traceback:
Traceback (most recent call last):
File "mecsolidos2.py", line 17, in <module>
m2 = a * 2 * l + a * x2 - 2 * q * l(x2 + l)
TypeError: 'Symbol' object is not callable
That is the same error to m3 through m7 :S
I've tried to change m2 to another constant, used m2 as m2.eval but none of that worked :S
What pissed me of was that for m1 it worked perfectly
ps: sorry for my bad english
If l() is intended as a function, it should be declared somewhere. If you wish, in SymPy you can have functions without giving its internal details, e.g. as l = Function('l')(x). More details in the documentation.
If, on the contrary, l(...) is meant to be just a multiplication (as suggested by your symbols declaration and the use of l as a scalar elsewhere), the multiplication (*) needs to be written explicitly.
Note that for SymPy to work optimally, when declaring symbols, it helps to specify their type. For example symbols(".....", real=True). Also, specifying whether some variable is always positive can help, especially when logarithms or sqrt are involved. See the documentation for more details about the possible types of assumptions.
By the way, if you prefer a looser syntax, where the multiplication symbol can be omitted and ^ can get converted to **, the sympify() or parse_expr() functions might be interesting.