To get started, I need an address base. In this case, such a database is updated annually on the website of the electronic legal documentation fund http://docs.cntd.ru/document/432885269. I will use the latest from 01/23/2019.
I create a request through "get":
Optimization
Optimization allows you to solve many problems from different areas of knowledge, optimizing incoming data. Familiarity with optimization in Python will allow you to learn libraries such as PyDOE2, numpy and learn how to solve polynomials in various ways. In this example, I will use the OLS Regression method or the least-squares method.
Suppose you have some kind of dependent variable that can be measured under various conditions. These conditions can also be changed to some extent. For the calculation, you need to take the min and max values to see the change in the dependent variable.
The DataFrame is shown below, where "T" and "P" are independent variables, and "y" is a dependent variable. The main question is to find the optimal solution in which "y" takes the maximum value:
For a more accurate solution, I will supplement the table with a 5th value that will be determined by the average values of the independent variables:
To determine which next step of approximation, I must solve a polynomial of the 1st degree. I will do this using the "statsmodels" function:
Despite a small error, I got a fairly good model with a convergence of 99.6%. And he determined the unknown coefficients 389.8, 134, 55 and -3.5. The parameter "t" here demonstrates the significance of a variable.
Let's plot:
Isolinia 400 is very close to my point 407, which suggests that the model is good.
In small steps, I have to move towards the maximum of "y"
We define in our model the very midpoint. It will be equal to 389.8 which is not much less than the value of 407.
We again found a good approximation of 98.5%. The midpoint value is also close to expected (y = 645.4).
Find the coordinates of this point:
Let's rebuild our plot again:
Note that the contours are already trying to change direction, so you can already try to find the maximum of our y.
To calculate the equation, compose a Dataframe with additional ray points.
We solve a polynomial of the second degree:
Let's build the resulting plot:
We display all the previous points on the plot and display it in colour.
At the maximum values of the variables, our point takes a maximum at 1963.4.
Build a 3D graph of our function:
Also, the maximum can be found by finding the derivative of the function with respect to x and y and solving equations with 2 unknowns. For example, let's create a new DataFrame and plot the graph:
We rewrite the resulting equation and find the coordinates of this function:
This technique is applied in absolutely any areas where it is necessary to find the most optimal result with certain input data.