Monday, January 8, 2024

Passing Cars (Count the number of passing cars on the road) - Problem on Prefix Sums

Passing Cars
Count the number of passing cars on the road.
Problem on Prefix Sums

Problem

A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.

Array A contains only 0s and/or 1s:

0 represents a car traveling east,

1 represents a car traveling west.

The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.

Write a function:

def solution(A)

that, given a non-empty array A of N integers, returns the number of pairs of passing cars.

The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..100,000];

each element of array A is an integer that can have one of the following values: 0, 1.

Example

For example, consider array A such that:

A[0] = 0

A[1] = 1

A[2] = 0

A[3] = 1

A[4] = 1

We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).

For example, given:

the function should return 5, as explained above.

Hint

Code (I)

def solution(A):
    # In the question, it is given that 0 are travelling east. We assume it to be the direction of prefix sums.
    # So if we implement using prefix counts, we would have to count zeroes.

    # '1' is going to the west, that is having the direction of suffix sums.

    prefix_counts = [0] * (len(A) + 1)

    for i in range(len(A)):
        if A[i] == 0:
            prefix_counts[i+1] = prefix_counts[i] + 1
        else:
            prefix_counts[i+1] = prefix_counts[i] + 0

    passing_cars = 0 
    for i in range(len(A)):
        if A[i] == 1:
            passing_cars += prefix_counts[i+1]

    if passing_cars > 1000000000:
        passing_cars = -1

    return passing_cars

Code (II): Now Using Suffix Sums

def solution(A):
    # In the question, it is given that 0 are traveling east. We assume it to be the direction of prefix sums.

    # '1' is going to the west, that is having the direction of suffix sums.
    # So if we implement using suffix counts, we would have to count ones.

    suffix_counts = [0] * (len(A) + 1)

    for i in range(len(A) - 1, -1, -1):
        if A[i] == 1:
            suffix_counts[i] = suffix_counts[i+1] + 1
        else:
            suffix_counts[i] = suffix_counts[i+1] + 0

    passing_cars = 0 
    for i in range(len(A)):
        if A[i] == 0:
            passing_cars += suffix_counts[i]

    if passing_cars > 1000000000:
        passing_cars = -1

    return passing_cars

Time Complexity

Detected time complexity:

O(N)

Tests

Correctness tests

single element

double

two elements

simple

simple test

small_random

random, length = 100

small_random2

random, length = 1000

Performance tests

medium_random

random, length = ~10,000

large_random

random, length = ~100,000

large_big_answer

0..01..1, length = ~100,000

large_alternate

0101..01, length = ~100,000

large_extreme

large test with all 1s/0s, length = ~100,000

I am what I think I am (Chapter 1)

 In 1902, the sociologist Charles Horton Cooley wrote: “I am not what I think I am, and I am not what you think I am. I am what I think you think I am.”
Let that blow your mind for a moment.
Our identity is wrapped up in what others think of us—or, more accurately, what we think others think of us.
Not only is our self-image tied up in how we think others see us, but most of our efforts at self-improvement are really just us trying to meet that imagined ideal. If we think someone we admire sees wealth as success, then we chase wealth to impress that person. If we imagine that a friend is judging our looks, we tailor our appearance in response. In West Side Story, Maria meets a boy who's into her.
What's her very next song? “I Feel Pretty.”

~ ~ ~

When you try to live your most authentic life, some of your relationships will be put in jeopardy. Losing them is a risk worth bearing; finding a way to keep them in your life is a challenge worth taking on.

~ ~ ~

IS THIS DUST OR IS IT ME?

Gauranga Das offered me a beautiful metaphor to illustrate the external influences that obscure our true selves. We are in a storeroom, lined with unused books and boxes full of artifacts. Unlike the rest of the ashram, which is always tidy and well swept, this place is dusty and draped in cobwebs. The senior monk leads me up to a mirror and says, “What can you see?” Through the thick layer of dust, I can't even see my reflection. I say as much, and the monk nods. Then he wipes the arm of his robe across the glass. A cloud of dust puffs into my face, stinging my eyes and filling my throat. He says, “Your identity is a mirror covered with dust. When you first look in the mirror, the truth of who you are and what you value is obscured. Clearing it may not be pleasant, but only when that dust is gone can you see your true reflection.” This was a practical demonstration of the words of Chaitanya, a sixteenth- century Bengali Hindu saint. Chaitanya called this state of affairs ceto-darpaṇa- mārjanam, or clearance of the impure mirror of the mind. The foundation of virtually all monastic traditions is removing distractions that prevent us from focusing on what matters most—finding meaning in life by mastering physical and mental desires. Some traditions give up speaking, some give up sex, some give up worldly possessions, and some give up all three. In the ashram, we lived with just what we needed and nothing more. I experienced rsthand the enlightenment of letting go. When we are buried in nonessentials, we lose track of what is truly significant. I'm not asking you to give up any of these things, but I want to help you recognize and filter out the noise of external influences. This is how we clear the dust and see if those values truly reflect you. Guiding values are the principles that are most important to us and that we feel should guide us: who we want to be, how we treat ourselves and others. Values tend to be single-word concepts like freedom, equality, compassion, honesty. That might sound rather abstract and idealistic, but values are really practical. They're a kind of ethical GPS we can use to navigate through life. If you know your values, you have directions that point you toward the people and actions and habits that are best for you. Just as when we drive through a new area, we wander aimlessly without values; we take wrong turns, we get lost, we're trapped by indecision. Values make it easier for you to surround yourself with the right people, make tough career choices, use your time more wisely, and focus your attention where it matters. Without them we are swept away by distractions.

WHERE VALUES COME FROM

Our values don't come to us in our sleep. We don't think them through consciously. Rarely do we even put them into words. But they exist nonetheless. Everyone is born into a certain set of circumstances, and our values are defined by what we experience. Were we born into hardship or luxury? Where did we receive praise? Parents and caregivers are often our loudest fans and critics. Though we might rebel in our teenage years, we are generally compelled to please and imitate those authority gures. Looking back, think about how your time with your parents was spent. Playing, enjoying conversation, working on projects together? What did they tell you was most important, and did it match what mattered most to them? Who did they want you to be? What did they want you to accomplish? How did they expect you to behave? Did you absorb these ideals, and have they worked for you? From the start, our educations are another powerful influence. The subjects that are taught. The cultural angle from which they are taught. The way we are expected to learn. A fact-driven curriculum doesn't encourage creativity, a narrow cultural approach doesn't foster tolerance for people from different backgrounds and places, and there are few opportunities to immerse ourselves in our passions, even if we know them from an early age. This is not to say that school doesn't prepare us for life—and there are many different educational models out there, some of which are less restrictive—but it is worth taking a step back to consider whether the values you carried from school feel right to you.

TRY THIS: WHERE DID YOUR VALUES COME FROM?

It can be hard to perceive the effect these casual influences have on us. Values are abstract, elusive, and the world we live in constantly pushes blatant and subliminal suggestions as to what we should want, and how we should live, and how we form our ideas of who we are. Write down some of the values that shape your life. Next to each, write the origin. Put a checkmark next to each value that you truly share.
VALUE ORIGIN IS IT TRUE TO ME?
Kindness Parent
Appearance Media Not in the same way
Wealth Parent No
Good grades School Interfered with real learning
Knowledge School
Family Tradition Family: yes, but not traditional
When we tune out the opinions, expectations, and obligations of the world around us, we begin to hear ourselves. ~ ~ ~

AUDIT YOUR LIFE

No matter what you think your values are, your actions tell the real story. What we do with our spare time shows what we value. For instance, you might put spending time with your family at the top of your list of values, but if you spend all your free time playing golf, your actions don't match your values, and you need to do some self-examination. Time TRY THIS: AUDIT YOUR TIME Spend a week tracking how much time you devote to the following: family, friends, health, and self. (Note that we're leaving out sleeping, eating, and working. Work, in all its forms, can sprawl without boundaries. If this is the case for you, then set your own definition of when you are “officially” at work and make “extra work” one of your categories.) The areas where you spend the most time should match what you value the most. Say the amount of time that your job requires exceeds how important it is to you. That's a sign that you need to look very closely at that decision. You're deciding to spend time on something that doesn't feel important to you. What are the values behind that decision? Are your earnings from your job ultimately serving your values? Media When you did your audit, no doubt a significant amount of your time was spent reading or viewing media. Researchers estimate that, on average, each of us will spend more than eleven years of our lives looking at TV and social media! Perhaps your media choices feel casual, but time reflects values. Money Like time, you can look at the money you spend to see the values by which you live. Exclude necessities like home, dependents, car, bills, food, and debt. Now look at your discretionary spending. What was your biggest investment this month? Which discretionary areas are costing you the most? Does your spending correspond to what matters most to you? We often have an odd perspective on what's “worth it” that doesn't quite make sense if you look at all your expenditures at once. I was advising someone who complained that the family was overspending on afterschool classes for the kids… until she realized that she spent more on her shoes than on their music lessons. Seeing posts on social media that compared spending and our priorities got me thinking about how the ways we spend our time and money reveal what we value. A 60-minute TV show (“Flew by!”) A 60-minute lunch with family (“Will it ever end!”) Everyday coffee habit ($4/day, almost $1,500/year) (“Need it!”) Fresh healthy food choices (an extra 1.50/day, about $550/year) (“Not worth it!”) 15 minutes scrolling social media (“Me time!”) 15 minutes of meditation (“No time!”) It's all in how you see it. When you look at a month of expenses, think about whether discretionary purchases were long- or short-term investments—a great dinner out or a dance class? Were they for entertainment or enlightenment, for yourself or someone else? If you have a gym membership, but only went once this month and spent more on wine, you have some rethinking to do.

CURATE YOUR VALUES

TRY THIS: PAST VALUES Reflect on the three best and three worst choices you've ever made. Why did you make them? What have you learned? How would you have done it differently? TRY THIS: VALUE-DRIVEN DECISIONS For the next week, whenever you spend money on a nonnecessity or make a plan for how you will spend your free time, pause, and think: What is the value behind this choice? It only takes a second, a flash of consideration. Ideally, this momentary pause becomes instinctive, so that you are making conscious choices about what matters to you and how much energy you devote to it. TRY THIS: COMPANION AUDIT Over the course of a week, make a list of the people with whom you spend the most time. List the values that you share next to each person. Are you giving the most time to the people who align most closely with your values?

Sunday, January 7, 2024

Exercise 1.1 On Linear Equations for Regression - From Pattern Recognition and ML - By Christopher Bishop

Pre-read
Question
Solution from web
My Explanation
Tags: Technology,Machine Learning,Mathematical Foundations for Data Science,

Exercise 1.2 On Regularization in Linear Regression - Pattern Recognition and ML - By Christopher Bishop

Pre-read for exercise 1.1:
Pre-read for exercise 1.2:
Problem 1.2:
Solution for problem 1.2: Pg 1:
Pg 2:
Pg 3:
Pg 4:

Exercise 1.3 on 'sum rule and product rule of probability' (From the book Pattern Recognition and ML by Christopher Bishop)

Let's quiz you for the basic concepts of probability theory by considering a simple example. 
    
Imagine we have two boxes, one red and one blue, and in the red box we have 2 apples and 6 oranges, and in the blue box we have 3 apples and 1 orange.

This is illustrated in Figure 1.9. Now suppose we randomly pick one of the boxes and from that box we randomly select an item of fruit, and having observed which sort of fruit it is we replace it in the box from which it came. We could imagine repeating this process many times. 

Let us suppose that in so doing we pick the red box 40% of the time and we pick the blue box 60% of the time, and that when we remove an item of fruit from a box we are equally likely to select any of the pieces of fruit in the box.

In this example, the identity of the box that will be chosen is a random variable, which we shall denote by B. This random variable can take one of two possible values, namely r (corresponding to the red box) or b (corresponding to the blue box). Similarly, the identity of the fruit is also a random variable and will be denoted by F . It can take either of the values a (for apple) or o (for orange).

To begin with, we shall define the probability of an event to be the fraction of times that event occurs out of the total number of trials, in the limit that the total number of trials goes to infinity. Thus the probability of selecting the red box is 4/10 and the probability of selecting the blue box is 6/10. We write these probabilities as: 
p(B = r) = 4/10 and p(B = b) = 6/10. 

Note that, by definition, probabilities must lie in the interval [0, 1]. Also, if the events are mutually exclusive and if they include all possible outcomes (for instance, in this example the box must be either red or blue), then we see that the probabilities for those events must sum to one.

We can now ask questions such as: 
“what is the overall probability that the selection procedure will pick an apple?”, 

or 

“given that we have chosen an orange, what's the probability that the box we chose was the blue one?”

We can answer questions such as these, and indeed much more complex questions associated with
problems in pattern recognition, once we have equipped ourselves with the two elementary rules of probability, known as the sum rule and the product rule. 

Sum Rule and Product Rule

Here p(X, Y ) is a joint probability and is verbalized as “the probability of X and Y ”. Similarly, the quantity p(Y |X) is a conditional probability and is verbalized as “the probability of Y given X”, whereas the quantity p(X) is a marginal probability and is simply “the probability of X”. Let us now return to our example involving boxes of fruit. For the moment, we shall once again be explicit about distinguishing between the random variables and their instantiations. We have seen that the probabilities of selecting either the red or the blue boxes are given by p(B = r) = 4/10 p(B = b) = 6/10 respectively. Note that these satisfy p(B = r) + p(B = b) = 1. Now suppose that we pick a box at random, and it turns out to be the blue box. Then the probability of selecting an apple is just the fraction of apples in the blue box which is 3/4, and so p(F = a|B = b) = 3/4. In fact, we can write out all four conditional probabilities for the type of fruit, given the selected box p(F = a|B = r) = 1/4 p(F = o|B = r) = 3/4 p(F = a|B = b) = 3/4 p(F = o|B = b) = 1/4

Practice Exercise

Suppose that we have three coloured boxes r (red), b (blue), and g (green). Box r contains 3 apples, 4 oranges, and 3 limes, box b contains 1 apple, 1 orange, and 0 limes, and box g contains 3 apples, 3 oranges, and 4 limes. If a box is chosen at random with probabilities p(r) = 0.2, p(b) = 0.2, p(g) = 0.6, and a piece of fruit is removed from the box (with equal probability of selecting any of the items in the box), then what is the probability of selecting an apple? If we observe that the selected fruit is in fact an orange, what is the probability that it came from the green box?

Solution

Ref: ChatGPT

Exercise 1.4 On Probability Densities - Pattern Recognition and ML - By Christopher Bishop

Pre-read

Fig:1
Fig:2
Fig:3
If we have several continuous variables x1,...,xD, denoted collectively by the vector x, then we can define a joint probability density p(x) = p(x1,...,xD) such Fig:4

Question

Fig:5

Solution

Exercise 1.5 On Concept of Variance From Statistics - Pattern Recognition and ML - By Christopher Bishop

Pre-read

1.5: Using the definition (1.38) show that var[f(x)] satisfies (1.39).

Solution

Exercise 1.6 On Covariance - Pattern Recognition and ML - By Christopher Bishop

Pre-read

Fig 1
Fig 2

1.6: Show that if two variables x and y are independent, then their covariance is zero.

Solution

Fig 3
Fig 4

Exercise 1.7 - Identifying normalization constant for Gaussian Distribution and Proving that it's integral equals 1 - From Pattern Recognition and ML - By Christopher Bishop

Pre-read

Fig 1:
Fig 2:

Question

Fig 3:

Solution

Fig 4:
Fig 5:
Fig 6:
Fig 7:
Fig 8:
Fig 9:
Fig 10:
Fig 11:

Index of Machine Learning

Toggle All Sections

Integration

Vector Calculus

Exercises from the book "Pattern Recognition and Machine Learning" by Christopher Bishop

Statistics

Tags: Index,Machine Learning,Statistics,