Table Calculation in python-3.x - python-3.x

I am still early on in my python journey and I have encountered a problem. I have a fairly large table similar to the one shown below;
Haz
Fault - floats
Mit - floats
H1
F1
M1
H1
F1
M2
H1
F1
M3
H1
F1
M4
H1
F2
M5
H1
F3
M6
H1
F3
M7
H1
F3
M8
H2
F4
M8
H2
F4
M9
..
..
..
H20
F87
M435
For each unique "Fault", I need to obtain a probability value (FM) through the following equation;
FM = [(1-M1)*(1-M2)...*(1-Mn)] * Fn
And then, for each unique "Haz", I need to obtain a probability value (HM) through the following equation;
HM = 1-[(1-FM1)*(1-FM2)...*(1-FMn)]
Any guidance on how I should approach this problem?

Related

Making a vector of specific length with random numbers

I tried writing a program that would give me the i,j and k components of a vector of a specified magnitude.
import random
import math
while True:
a = random.uniform(0,1000)
b = random.uniform(0,1000)
c = random.uniform(0,1000)
d = 69.420
if math.sqrt(a**2 + b**2 + c**2) == d:
print(a,b,c)
break
But it seems that this program might take literally forever to give me an output.
What would faster or possible solution be?
#Update
import random
import math
while True:
a2 = random.uniform(1,1000)
b2 = random.uniform(1,1000)
c2 = random.uniform(1,1000)
d = 69.420
d2 = a2 + b2 + c2
a2 *= d/d2
b2 *= d/d2
c2 *= d/d2
a = math.sqrt(a2)
b = math.sqrt(b2)
c = math.sqrt(c2)
if math.sqrt(a**2 + b**2 + c**2) == d:
print(a,b,c)
break
As per suggested, but still taking a very long time to compute
Get three random numbers a2, b2, and c2. Those are your random squares. Add them up to get d2. You want the sum of the squares to be d squared, so multiply a2, b2, and c2 by d*d/d2. These are your new squares that add up to d squared. Now assign the square roots of a2, b2, and c2 to a, b, and c. Does that make sense?
Avoid dividing by zero. If d2 happens to be zero, just start over.

complex IF statements with multiple variables possibilities

I need help with making some IF/OR/AND statements.
I have a cell (C8) that can be one of fourteen different variables. Depending on the value for C8 either cells F8, D8, or E8 will be used in three possible equations.
C D E F G H
7
8
9
C8 can equal any of the following values
0.5,0.55,0.6,0.7,0.75,1,1.0625,1.125,1.1875,1.25,1.325,1.375,1.4375,1.5
Equations needed:
IF C8 equals any values from 0.6 - 1.5 will then need to solve for (100-(F8-108)*5))+(G8+1))
IF C8 equals 0.5 will then need to solve for (100-((D8-56)*5)+(G8*1))
IF C8 equals 0.55 will then need to solve for (100-((E8-102)*5)+(G8*1)
I currently have this equation C8 if values are 0.6 1.5
=IF(AND(SUMPRODUCT(--ISNUMBER(SEARCH({0.6,0.65,0.7,0.75,1,1.0625,1.125,1.1875,1.25,1.325,1.375,1.4375,1.5},C8)))>0),100-(((F8-108)*5)+(G8*1)),"")
I think I need an IF/OR statement for two additional Situation
C8 equals 0.5 to solve for 100-(((D8-56)*5)+(G8*1))
C8 equals 0.55 to solve for 100-(((E8-102)*5)+(G8*1))
The following is the they type of IF/OR formulas I have tried.
=IF(OR(SUMPRODUCT(--ISNUMBER(SEARCH({0.6,0.65,0.7,0.75,1,1.0625,1.125,1.1875,1.25,1.325,1.375,1.4375,1.5},C8)))>0), 100-(((F8-108)*5)+(G8*1)), OR(ISNUMBER(SEARCH({0.5,C8)))>0)100-(((D8-56)*5)+(G8*1)), OR(ISNUMBER(SEARCH({0.55,C8))>0)100-(((E8-102*5)+(G8*1))"")
=IF(OR(SUMPRODUCT(--ISNUMBER(SEARCH({0.6,0.65,0.7,0.75,1,1.0625,1.125,1.1875,1.25,1.325,1.375,1.4375,1.5},C8)))>0), 100-(((F8-108)*5)+(G8*1)), (ISNUMBER(SEARCH({0.5,C8)))>0)100-(((D8-56)*5)+(G8*1)), (ISNUMBER(SEARCH({0.55,C8))>0)100-(((E8-102*5)+(G8*1))"")
Do you need to search for the values? If the cell can only equal one of the values you shared you can just build your statement around that assumption.
If that assumption is false, this will not work. [Equation3] will be called when C8 equals anything BUT .50 & .55 so C8 has to have limitations for this to work.
IF(C8 = .50, [Equation1], IF(C8 = .55, [Equation2], [Equation3]))
Where
[Equation1] = (100-((D8-56)*5)+(G8*1))
[Equation2] = (100-((E8-102)*5)+(G8*1)
[Equation3] = (100-(F8-108)*5))+(G8+1))

Formula to put dat within a sentence

Assuming I have the following cells:
A1 - first_Name (e.g Mary) B1 - last_name (e.g Thomas) C1 - car (e.g 3) D1 - houses (e.g 1) E1 - children (e.g blank)
I want to create a formula which can auto populate into the following sentence:
Mary Thomas has car=‘3’, House=‘1’,
All 3 of below equations build the exact same output seen in photo
Option 1 =CONCATENATE(A2," ",B2," has car= ",C2,", House= ",D2,", Children= ",E2)
Option 2 =TEXTJOIN(CHAR(32),1,A2:B2,"Has Car=",C2,",House=",D2,",Children=",E2)
Option 3 =A2&" "&B2&" Has Car= "&C2&", House= "&D2&" ,Children= "&E2

Analysing simulation result of event sequences with branch

So I have a problem where a sequence of
A1 > B1 > C1 > D1
or
A1 > B1 > C2 > D2
or
A1 > B1 > C2 > D3
or
A2 > B2 > C3 > D4
Note there's more than 1 root starting point too. Each stage also has some other properties to it. So I'd want to ask
find all stage (regardless of ABCD) where property 1 = some value and has some where up the parent chain property 2 = some value.
I need to work out the probability of getting to each stage if given all "sequence branch" are of equal probability. So probability of getting to D3 is 1/2(A) * 1/2(C) where as D1 or D2 stage is 1/2(A) * 1/2(C) * 1/2(D)
Conditional probability. Given B1 has happened, what's the chance of D3.
What's the best way / technique to store and analyse / query / interrogate data like this? What sort of keywords should I google / field / technology to read and learn?
Note I'm thinking to generate in the neighbourhood of 100s of k up to millions sample of sequence events.
I've had some look at RDBMS recursive CTE. That solves problem 1, but 2 and 3 in combination seem a bit more difficult. Was wondering if a graph database like neo4j can solve the problem better?

How do you create and use datatypes in Haskell?

I am an Absolute Newbie to Haskell. But I really like it. I have been reading Learn You A Haskell and Real World Haskell and practicing along. I am on the Functionally Solving Problems section in the Learn You A Haskell book and I need someone to explain the following to me:
data Node = Node Road Road | EndNode Road
data Road = Road Int Node
Questions:
Is this some recursive data type creation?
Why Can't I do: Node "A" "B" and get a valid Node type? When I try something I can an error saying that Haskell can't match [Char] with Expected Road and I understand that. But I can't create a Node X Y without a Road Int Node. I thought Road was a type synonym for String but since it isn't declared it certainly is not. If anyone read the book and understood this thing PLEASE EXPLAIN WHAT THOSE DATA TYPES MEAN AND PROVIDE EXAMPLE IF POSSIBLE.
Edit: The Chapter In Question >> Heathrow to London problem
Yes, these are mutually recursive data types.
To create values of these types you are likely to use recursion too. For example, here's a minimal road system - two nodes where each one lets you drive to the other:
nodeA, nodeB :: Node
nodeA = EndNode (Road 99 nodeB)
nodeB = EndNode (Road 99 nodeA)
Note how the definition of nodeA refers to nodeB and vice versa.
If I read the example in the book correctly it can be written like this:
aRoad, bRoad :: Road
aRoad = Road 50 a1
bRoad = Road 10 b1
a1, a2, a3, a4 :: Node
a1 = Node (Road 5 a2) (Road 30 b1)
a2 = Node (Road 40 a3) (Road 20 b2)
a3 = Node (Road 10 a4) (Road 25 b3)
a4 = EndNode (Road 0 b4)
b1, b2, b3, b4 :: Node
b1 = Node (Road 90 b2) (Road 30 a1)
b2 = Node (Road 2 b3) (Road 20 a2)
b3 = Node (Road 8 b4) (Road 25 a3)
b4 = EndNode (Road 0 a4)
Note again the mutual recursion of a1 and b1, a2 and b2 etc. for the crossing roads.
I've written the roads inline but of course you could name them all if you wanted:
roadA1ToB1 :: Road
roadA1ToB1 = Road 30 b1
If you did it this way the cycle of definitions would involve four of them - the definition of (the node) a1 would use roadA1ToB1, which would use b1, which would use roadB1ToA1, which would use a1.

Resources