Android: laying out a simple stack of boxes?

Question: This should be easy, but for the life of me I cannot figure it out.
I want to make a flud stack of boxes, as in this picture:
+------------------+
| +------o-------+ |
| |  Banner      | |
| +------o-------+ |
| | Buttons      | |
| |              | |
| +------o-------+ |
| |              | |
| |              | |
| | ViewFlipper  | |
| |              | |
| |              | |
| |              | |
| |              | |
| +------o-------+ |
| | Buttons      | |
| +------o-------+ |
| | Footer       | |
| +------o-------+ |
+------------------+

Every box - including the outer one - represents a Layout containing other widgets (or even, a lone widget). The outer box matches the device's width and height. The width and height of the stack must match the outer container. The edges of the boxes are glued to each other and to the top and bottom edges of the container, as indicated by the 'o's.  All the inner layouts wrap their content tightly, except the largest - a ViewFlipper - which contains a scrollable listbox and can expand or contract as needed. The number of boxes is not important, as long as it is four or more.
The ViewFlipper contains a number of vertical LinearLayouts with width/height=fillparent (where my understanding is that the parent is the ViewFlipper).
In my attempts so far, I have had some success with an outer RelativeLayout and  stitching the edges of the internal boxes with:
android:layout_below="@+id/former_box and android:layout_above="@+id/following_box, but it is an instable situation in which the boxes start acting weird (such as the second one covering entirely the others, etc.) every time I change the design (by, for instance, inserting another intermidiate box). Please note, that this is a design-time layout, no fancy dynamic run-time rearrangements are involved.
I am now experimenting with TableLayout (a single column) with basically no joy worth reporting. I am obviously not an Android expert, but still. What is the best way to do this?
Edit - insert actual code
Since it appears that omitting the actual code fogged the issue (I hoped it would clarify it, my mistake) I am inserting the working version here. You will notice it lacks the banner and footer. As mentioned above, every time I try to insert additional boxes it blows up on me - circular references starring often.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_margin="0dip"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".TomActivity" >

  <RelativeLayout
      android:id="@+id/spinnerbox"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center_horizontal"
      android:onClick="left"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:orientation="horizontal" >

    <Button
        android:id="@+id/leftarrow"
    android:background="#888888"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:minHeight="0dip"
        android:minWidth="0dip"
        android:layout_alignParentLeft="true"
        android:onClick="left"
        android:text="Button" />

    <TextView
    android:id="@+id/spintitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/leftarrow"
    android:layout_toLeftOf="@+id/rightarrow"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/leftarrow"
    android:layout_toStartOf="@+id/rightarrow"
    android:gravity="center"
    android:text="Label" />

    <Button
    android:id="@+id/rightarrow"
    android:background="#888888"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:minHeight="0dip"
    android:minWidth="0dip"
    android:onClick="right"
    android:text="Button"
    />

  </RelativeLayout>

  <ViewFlipper
      android:id="@+id/view_flipper"
      android:layout_margin="0dip"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/spinnerbox"
      android:layout_above="@+id/buttonbox"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

    <LinearLayout
    android:layout_margin="0dip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

      <ListView
      android:id="@+id/CoursesView"
      android:layout_margin="0dip"
      android:background="#ffffff"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" >
      </ListView>
    </LinearLayout>
    <LinearLayout
    android:layout_margin="0dip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

      <ListView
      android:id="@+id/FutureCoursesView"
      android:layout_margin="0dip"
      android:background="#ffffff"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" >
      </ListView>
    </LinearLayout>
    <LinearLayout
    android:layout_margin="0dip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

      <ListView
      android:id="@+id/MessagesView"
      android:layout_margin="0dip"
      android:background="#ffffff"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" >
      </ListView>
    </LinearLayout>
  </ViewFlipper>

  <LinearLayout
      android:id="@+id/buttonbox"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true"
      android:orientation="horizontal"
      >

    <Button
    android:id="@+id/CurrentCoursesButton"
    android:background="#888888"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/FutureCoursesButton"
    android:background="#888888"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/UnreadMessagesButton"
    android:background="#888888"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

    <Button
    android:id="@+id/RefreshButton"
    android:background="#888888"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="refreshAll"
    android:text="Button" />

    <Button
    android:id="@+id/LogoffButton"
    android:background="#888888"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="logOff"
    android:text="Button" />

  </LinearLayout>

</RelativeLayout>

Cheers,
Edited to show the layout more clearly and give info on the rationale
Comment: You're using some useless attributes(like the gravity on the `LinearLayouts` and the weight on the `ListView`(as you could simply set the ListView's height to match_parent)). The `ViewFlipper`'s height should be `wrap_content`, also the order in which you place the compoenents in the outer `RelativeLayout`(see the order in my gist).
Comment: Maybe I don't understand were do you have problems. Adding extra stuff to your already present layout shouldn't break things. At the same link as above https://gist.github.com/luksprog/6549402 I've inserted two buttons as the banner and footer and they work with no problems.
Comment: What is wrong with wrapping the children boxes with a vertical LinearLayout?
Comment: When I do, the large widget (a ViewFlipper) ends up on top of everything that is below. That is why I turned to the RelativeLayout originally.
Comment: A ViewFlipper is used to show one view (out of a number of views) at a time. I didn't mean using a LinearLayout as the outer view, but wrapping the inner boxes inside a vertical LinearLayout
Comment: Is this what you want https://gist.github.com/luksprog/6549402?
Comment: It is - except that, when I add my vertical linearlayout inside the viewflipper, (fillparetn in both directions) they overlap the top widgets (banner ad buttons).
Comment: The useless attributes come from the constant layout churn/test/edit of the layout - they should however, be harmless, isn't it? You are right about wrap_content (changing it does not appear to have any noticeable effect, however) The order, I guess, is wrong (witness the circular dependencies that hit me now and then) however what would the correct order be? Listing the widgets top to bottom (what I did) seems the natural thing to do. I see you followed a different principle, but which one and why beats me. (Besides that example is still not what I need, see my comment below)
Answer: When building complex/multilevel layouts, prepare your innermost layout first. Then wrap your outer layouts around the innermost one iteratively. This way, decisions like which layout container to use get a bit simplified.
See if the following looks like what you are looking for:

I have posted the Layout's xml here: Link(pastebin) Link(file)
What I have done is use the layout you provided and placed it inside a LinearLayout with vertical orientation. The top and bottom "banners" are given layout_weight="0" while the content area(code that you have given) has a layout_weight set to 1. So, the top and bottom banners take a fixed amount of space, and the content takes up the rest.
Answer: I think LinearLayout will be best for you...
please try and let me know if you find any difficulty

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">
    <LinearLayout
        android:id="@+id/spinnerbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:onClick="left">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
            <Button
                android:id="@+id/leftarrow"
                android:background="#888888"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:minHeight="0dip"
                android:minWidth="0dip"
                android:onClick="left"
                android:text="Button" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
            <TextView
                android:id="@+id/spintitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Label" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
            <Button
                android:id="@+id/rightarrow"
                android:background="#888888"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minHeight="0dip"
                android:minWidth="0dip"
                android:onClick="right"
                android:text="Button"
                />
        </LinearLayout>

    </LinearLayout>

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" >

                <ListView
                    android:id="@+id/CoursesView"
                    android:background="#ffffff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </ListView>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" >

                <ListView
                    android:id="@+id/FutureCoursesView"
                    android:layout_margin="0dip"
                    android:background="#ffffff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                </ListView>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" >

                <ListView
                    android:id="@+id/MessagesView"
                    android:layout_margin="0dip"
                    android:background="#ffffff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    >
                </ListView>
            </LinearLayout>
        </LinearLayout>
    </ViewFlipper>

    <LinearLayout
        android:id="@+id/buttonbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <Button
            android:id="@+id/CurrentCoursesButton"
            android:background="#888888"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/FutureCoursesButton"
            android:background="#888888"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/UnreadMessagesButton"
            android:background="#888888"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/RefreshButton"
            android:background="#888888"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="refreshAll"
            android:text="Button" />

        <Button
            android:id="@+id/LogoffButton"
            android:background="#888888"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="logOff"
            android:text="Button" />

    </LinearLayout>
</LinearLayout>

Answer: First of all, your ViewFlipper shouldn't have "fill_parent" for its height. In a RelativeLayout, that really does mean "fill the parent".
If I were you, I'd switch to a LinearLayout, like others have mentioned. To do this, simply give all of your other "block" components a height of "wrap_content". For your ViewFlipper, you want a combination of layout_height="0dp" and layout_weight="1". This will tell it to simply expand in all the remaining space, without overlapping.
Then, since you're using a LinearLayout, get rid of all the _align and above/below tags. They don't do anything for a linear layout, and it makes it ridiculously hard to read.
One other thing: When doing semi-complicated layouts like this, it's often helpful to break each "block" out into a separate XML file. You can then include each piece in the "main" layout, and everything looks much better. It helps you work on one piece at a time, without having to scroll through a 500-line layout file.
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Banner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Buttons
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    </ViewFlipper>
    <Buttons
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

Comment: The basics are here: http://developer.android.com/training/improving-layouts/reusing-layouts.html
Comment: I love the idea of using include for layout pieces. How is it done?
Answer: As Geobits suggested separates the views into separate XML files.I have made complete view as you wanted.just check it.I have used weightSum so that it can fill whole screen.
main_layout.xml
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:weightSum="10"
  >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#ababab"
    android:baselineAligned="false"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Header"
        android:textColor="#ffffff"
        android:gravity="center"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:background="#ababab"
    android:baselineAligned="false"
    >

     <include layout="@layout/button_row" />

</LinearLayout>

<RelativeLayout
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="6"
    >

    <ViewFlipper
       android:id="@+id/vfPageContainer"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >

        <include layout="@layout/view_one" />

    </ViewFlipper>

</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:background="#ababab"
    android:baselineAligned="false"
    >

     <include layout="@layout/button_row" />

</LinearLayout>

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#ababab"
    android:baselineAligned="false"
    >

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Footer"
        android:textColor="#ffffff"
        android:gravity="center"
        />

</LinearLayout>

button_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
 >

 <LinearLayout
    android:id="@+id/buttonbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/CurrentCoursesButton"
        android:background="#888888"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/FutureCoursesButton"
        android:background="#888888"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/UnreadMessagesButton"
        android:background="#888888"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/RefreshButton"
        android:background="#888888"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="refreshAll"
        android:text="Button" />

    <Button
        android:id="@+id/LogoffButton"
        android:background="#888888"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="logOff"
        android:text="Button" />

 </LinearLayout>

</LinearLayout>

view_one.xml
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <ListView
      android:id="@+id/MessagesView"
      android:background="#ffffff"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >
  </ListView>
</LinearLayout>

</LinearLayout>

Answer: If I understand correctly, your ViewFlipper is appearing at the top of a scrollable container. If it resides inside a ScrollView, you can instruct its contents to fill the viewport by using android:fillViewport="true":
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

    <ViewFlipper
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</ScrollView>

If you have multiple items inside the ScrollView, use it inside a LinearLayout in conjunction with android:layout_weight:
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        <ViewFlipper
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

    </LinearLayout>

</ScrollView>

Comment: I added my current code to the question. ScrollViews are not part of the (outer) layout
Answer: Stack up your views with property
android:layout_centerInParent="true"

for all the child in RelativeLayout,
and you can set child view's height and width and even margins individually
Comment: That - setting a fixed width/height - is exactly what i do not want to do. I want the content to determine the height of the boxes (except for the largest, as explained) and the edges should adjust "flowing" - except for the topmost and bottom-most.
Answer: Just use a LinearLayout as Outter. With orientantion Vertical.
Every child inside it, should be width="match_parent" height="wrap_content"
Doesnt matter which kind of view are the childs inside the Outter.
Comment: When I do, the large widget (a ViewFlipper) ends up on top of everything that is below. That is why I turned to the RelativeLayout originally.
Comment: Can you add a screenshot? But I guess the problem is that you should set proper layout params on the views you're using in the view flipper.
Comment: I will, as soon as I figure out how to add images in this wiki. In the meantime, I have edited the question to give more detail.
Answer: //try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TomActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">
        <LinearLayout
            android:id="@+id/spinnerbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:onClick="left">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <Button
                    android:id="@+id/leftarrow"
                    android:background="#888888"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:minHeight="0dip"
                    android:minWidth="0dip"
                    android:onClick="left"
                    android:text="Button" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/spintitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Label" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1">
                <Button
                    android:id="@+id/rightarrow"
                    android:background="#888888"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="0dip"
                    android:minWidth="0dip"
                    android:onClick="right"
                    android:text="Button"
                    />
            </LinearLayout>

        </LinearLayout>

        <ViewFlipper
            android:id="@+id/view_flipper"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginTop="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center" >

                    <ListView
                        android:id="@+id/CoursesView"
                        android:background="#ffffff"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    </ListView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center" >

                    <ListView
                        android:id="@+id/FutureCoursesView"
                        android:layout_margin="0dip"
                        android:background="#ffffff"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        >
                    </ListView>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center" >

                    <ListView
                        android:id="@+id/MessagesView"
                        android:layout_margin="0dip"
                        android:background="#ffffff"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        >
                    </ListView>
                </LinearLayout>
            </LinearLayout>
        </ViewFlipper>

        <LinearLayout
            android:id="@+id/buttonbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <Button
                android:id="@+id/CurrentCoursesButton"
                android:background="#888888"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />

            <Button
                android:id="@+id/FutureCoursesButton"
                android:background="#888888"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />

            <Button
                android:id="@+id/UnreadMessagesButton"
                android:background="#888888"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />

            <Button
                android:id="@+id/RefreshButton"
                android:background="#888888"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="refreshAll"
                android:text="Button" />

            <Button
                android:id="@+id/LogoffButton"
                android:background="#888888"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="logOff"
                android:text="Button" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>


Dirichlet posterior

Question: I have a question about the Dirichlet posterior distribution. Given a multinomial likelihood function it's known that the posterior is $Dir({\alpha_i + N_i})$, where $N_i$ is the number of times we've seen $i^{th}$ observation.
What happens if we start to decrease $\alpha$s for a given fixed data $D$? It seems from the form of the posterior that after some point $\alpha$s will stop affecting posterior at all. But wouldn't it be right to say that when we make $\alpha$s very small the probability mass moves to the corners of the simplex and the posterior must be affected to a greater extent? What statement is the correct one?
Answer: For me the most helpful way to envision the effect of the parameters for Dirichlet  is the Polya urn. Imagine you have an urn containing n different colors, with $\alpha_i$ of each color in the urn (note that you can have fractions of a ball). You reach in and draw a ball, then replace it along with another of the same color. You then repeat this an infinite amount of times and the final proportion constitutes a sample from the a Dirichlet distribution. If you have very small values for $\alpha$, it should be clear that the added ball will heavily weight you towards the color of that first draw, which explains why the mass moves to the corners of the simplex. If you have large $\alpha's$, then that first draw doesn't impact the final proportion as much.
What your posterior is essentially saying is that you started with $\alpha_i$ balls of color $i$, did a bunch of draws, and happened to draw out that color  $N_i$ times. You can then imagine samples from the posterior being generated with the same process and imagine the effects the initial $\alpha$ along with the counts $N$ will have on those samples. Clearly a small value for $\alpha$ will have less effect on the posterior.
Another way to think about it is that the parameters to your Dirichlet control how much you trust your data. If you have small values of $\alpha$, then you trust your data almost entirely. Conversely, if you have large values for $\alpha$, then you are less trusting of your data and will smooth the posterior a bit more.
In summary, you are correct to say that as you decrease the $\alpha's$, they will have less effect on the posterior, but at the same time the prior will have most of its mass on the corners of the simplex.

Dirichlet posterior


What is the probability to get a value of 11 or 12 in 10 rolls or less?

Question: Suppose I have a 3-sided die with each side contains different number of 1, 2, and 3. In this problem, each time I roll the die I will add up the number that shows up with previous rolls. The target number I need is either 11 or 12. Once it reaches either of those number I won't roll the die again. What is the probability to get a value of 11 or 12 in 10 rolls or less?
I tried to calculate myself but the result is more than 100%.
<code>Sum of 11 or 12 in 4 rolls: 0,061728395
Sum of 11 or 12 in 5 rolls: 0,308641975
Sum of 11 or 12 in 6 rolls: 0,366255144
Sum of 11 or 12 in 7 rolls: 0,195244627
Sum of 11 or 12 in 8 rolls: 0,057613169
Sum of 11 or 12 in 9 rolls: 0,010211858
Sum of 11 or 12 in 10 rolls: 0,001100781
Total: 1,000795949
</code>
Comment: There are only two ways to fail: roll ten ones, or roll a three when the sum is ten.
Comment: Not clear what your events are.  If you get an $11$ after $6$ rolls and then get a $1$ on your next toss, do you count that sequence twice?  After all you won on both the sixth and seventh toss.  That would explain why you are getting more than $1$ for the probability.
Comment: @lulu I just realized that i calculated the probability for sum of 12 with including a event that it already reached 11 in previous rolls. Here's a revision of my calculations:
Sum of 11 or 12 in 4 rolls: 0,061728395
Sum of 11 or 12 in 5 rolls: 0,29218107
Sum of 11 or 12 in 6 rolls: 0,304526749
Sum of 11 or 12 in 7 rolls: 0,137631459
Sum of 11 or 12 in 8 rolls: 0,033074226
Sum of 11 or 12 in 9 rolls: 0,004521668
Sum of 11 or 12 in 10 rolls: 0,000338702 Total: 0,834002269
Comment: Sorry, Once it reaches 11 or 12 it should be no extra rolls needed. I should add this information on the description.
Comment: Sure, but do your calculations know that?  Since you are getting more than $1$, it seems possible that you are double counting somewhere.
Comment: Your new result is correct.
Answer: Let $p(X)$ denote the probability of reaching the sum $X$. Then $p(13) = p(10)/3$. We note the following:
$$p(1) = 1/3, p(2) = 4/9, p(3) = 16/27$$(assuming a fair dice) and
$$p(n) = (p(n-1)+p(n-2)+p(n-3))/3$$for $3<n\le10$.
Using this, we find:
$$p(10) = 29404/59049 = 29404/3^{10}$$
Hence, $p(13) = 29404/177147 = 29404/3^{11}$.
Now, the probability that you get $11$ in 11 rolls is $(1/3)^{11}$, that you get $12$ in $11$ rolls is $(1/3)^{11}$. So, the probability you're looking for is
$$1 - 29404/3^{11} - 2\cdot(1/3)^{11} = 49247/3^{10} \approx \boxed{0.834}$$


how to insert timestamp beyond the limitation of keep in tdengine

Question: I found that tdengine has a parameter will create database. this definition "The KEEP parameter refers to the number of days to save a modified data file. " from tdengine's website https://www.taosdata.com/en/documentation/taos-sql#management.  I think this parameter is very useful that there is no need to delete the history data. so i create a database which only keep 10 days.
CREATE DATABASE IF NOT EXISTS db_keep KEEP 10 PRECISION 'ms' ;
create table test_keep(ts timestamp,desc nchar(20));

after create db and table , I tried to insert some data into the table. the follows are my insert sqls.
insert into test_keep values(now,'now');
insert into test_keep values('2021-08-31 10:28:53.521','yesterday');
insert into test_keep values('2021-09-02 10:28:53.521','tomorrow');
insert into test_keep values('2021-08-25 10:28:53.521','6 days before');
insert into test_keep values('2021-09-20 12:28:53.521','20 days later');
insert into test_keep values('2021-08-21 10:28:53.521','10 days before');
insert into test_keep values('2021-08-11 10:28:53.521','20 days before');

While the lass three sql had execute error "DB error: Timestamp data out of range"

taos> insert into test_keep values(now,'now'); Query OK, 1 of 1 row(s)
in database (1.024000s)
taos> insert into test_keep values('2021-08-31
10:28:53.521','yesterday'); Query OK, 1 of 1 row(s) in database
(0.006000s)
taos> insert into test_keep values('2021-09-02
10:28:53.521','tomorrow'); Query OK, 1 of 1 row(s) in database
(0.004000s)
taos> insert into test_keep values('2021-08-25 10:28:53.521','6 days
before'); Query OK, 1 of 1 row(s) in database (0.004000s)
taos> insert into test_keep values('2021-09-20 12:28:53.521','20 days
later');
DB error: Timestamp data out of range (0.005000s) taos> insert into
test_keep values('2021-08-21 10:28:53.521','10 days before');
DB error: Timestamp data out of range (0.004000s) taos> insert into
test_keep values('2021-08-11 10:28:53.521','20 days before');
DB error: Timestamp data out of range (0.004000s) taos>

I thought this because of my keep is to small, so i made it larger.
ALTER DATABASE db_keep KEEP 365;

and the I tried to insert the failed sql again I  found cannot insert data some days later from now .

taos> insert into test_keep values('2021-09-20 12:28:53.521','20 days
later');
DB error: Timestamp data out of range (0.005000s) taos> insert into
test_keep values('2021-08-21 10:28:53.521','10 days before'); Query
OK, 1 of 1 row(s) in database (0.004000s)
taos> insert into test_keep values('2021-08-11 10:28:53.521','20 days
before'); Query OK, 1 of 1 row(s) in database (0.004000s)

I want to ask how to use keep and the how dose it limit the data's timestamp?
Answer: For timestamp in a database, the two configuration options is most reasonable:

keep: the longest days to keep in the database refer to current timestamp(3650 days by default). When the "newest" timestamp in a persist file goes out of the range, the persist file will be deleted. Timestamp older than now - keep will be treated as Timestamp out of range.
days: the time range to store data in a file, 10 days by default. Newer timestamp would not be larger than now+ days.

So, the acceptale timestamp range in a database will be [now - keep, now + days].
Answer: For past data, the timestamp value cannot exceed (current_time - keep). Meanwhile, for future data, the timestamp value cannot exceed (current time + days).
Since my reputation is not enough for inserting images here, please refer to reference link below for details (grey means can NOT be inserted).
Reference:

https://segmentfault.com/a/1190000040617572


Wordpress Migrate Inline Images looking in wrong folder

Question: I've recently used the Wordpress Migration module to bring over a lot of posts into drupal. The images get moved, and the text comes in fine. The problem is when the images get brought over they are put in one location on the server, but the inline media tags tell them to use view_mode: media_large the images don't show in the post because it's looking in the '/sites/default/files/styles/large/public' folder instead of where they actually got imported to. How do I either import these posts so that the images go there, or so that the media tags that are created don't look in the large folder but where the files actually are?
Answer: Turns out the styles folder inside /sites/default/files was the only folder in there that was not writable by the server so it put the images elsewhere. I rolled back the wordpress import, fixed the permissions, and ran the import again. Everything seems to be good now.


Fixed Sidebar for Navigation in Website

Question: I want to have a sidebar in my website with navigation in it. I will use script like phpBB etc. but I want sidebar to be displayed on every page.
So I am thinking about making a Sidebar in HTML and then using frame tag for displaying other pages/scripts.
But as Frames are getting obsolute, Is there any other method to display a sidebar in everypage without using frames and without adding html coding on every page?
Answer: Put your HTML in a PHP file (or HTML if you want) with no  or other tags aside from what you want to display, then put the following on each page you want to display the menu on:
<code><?php include ('page-name.php'); ?>
</code>
One line of code is all you need. If you make a change to the 'page-name.php' file, it updates on every page.
Cheers
Comment: Inserting it on a single php file wont work man...Cauz Like in phpBB, there are Files for Different Functions like Index.php for Index, ShowForum.php for Showing single forum... It Wont be displayed on every page unless I insert it on every file...
Comment: You could also have a line of jQuery in a common .js file that makes an `$.ajax` call to your sidebar content if you like that better.  You're probably already including the .js file, right?  Adding the include there keeps it out of your markup (which can be a good thing or a bad thing).
Comment: The problem is to Insert Include function in Every Script i.e If I use phpBB and eyeOS then I have to Insert that line to many places to include a js file...
Answer: This question was asked here and on Webmasters; I answered it over there.

Fixed Sidebar for Navigation in Website


How to simulate Poisson arrival times if the rate varies with time?

Question: Suppose we are to study a non-homogeneous Poisson process of 3 hour cycles in which:

At the first hour, the arrival rate is 1.5 events / hr.
At the second hour, the arrival rate is 2.1 events / hr.
At the third hour, the arrival rate is 3.4 events / hr.

I would like to generate the time points (not inter-arrival times) of events observed in $3\times1000$ hours. How should I implement this in R?
Edit1:
Following jbowman's answer, I tried to verify the mean rate with
ia=diff(arrival_times)
t=arrival_times[2:length(arrival_times)]%%3
ia_bygrp=list(ia[which(0<=t & t<1)],ia[which(1<=t & t<2)],ia[which(2<=t & t<3)])
sapply(ia_bygrp,function(x) 1/mean(x))

The result was, unexpectedly (I only quote 1 result but I tried for 30+ times to reduce randomness)
[1] 1.892691 2.869797 2.116035

Why? See Edit2.
Edit2:
I tried to use larger rates: rates = c (15.0, 21.0, 34.0) for example. The arithmetic means were instead
[1] 21.64794 32.72656 15.59476

I believe it's because of the small number of occurrence n_arrivals in each hour which makes runif(n_arrivals) not-so-uniform. Any other solution?
Edit3:
Based on GeoMatt22's code with Octave (well, there is large variety here), I wrote another code to simulate the empirical rates $\lambda$.
Here is the code
rates <- c(1.5, 2.1, 3.4)
arrival_times <- c()
Nrep=10000
for (hour in 1:(3*Nrep)) {
  n_arrivals <- rpois(1,rates[1 + hour%%3])
  arrival_times <- c(arrival_times, runif(n_arrivals) + hour - 1)
}
arrival_times <- sort(arrival_times)

x=hist(arrival_times%%3,breaks=c(0,1,2,3),plot=FALSE)
bp=barplot(x$counts/Nrep,axes=FALSE,ylim=range(0,max(rates)*1.1),ylab="Observed rate / hr",main="Simulation for jbowman's answer")
points(bp,rates[c(2,3,1)],col='red',pch=20)
axis(side=2,at=seq(0,3.5,0.1),label=seq(0,3.5,0.1))
axis(side=1,at=bp,label=c("1st hours","2nd hours","3rd hours"))

and the histogram (red dot = true rates)

Pretty close. Apparently counting occurrences in each hour is not the same as the reciprocal of mean hourly inter-arrival times. [I think I have seen a question about the difference between the two but I could not retrieve it at the moment].
It turns out jbowman's answer can simulate well even using rates down to rates <- c(1.5, 2.1, 3.4)/100.
Comment: This is a coding question. I don't think OP has an issue with the conceptual understanding of the problem at all
Comment: @Aksakal the main question is about simulating a piecewise-homogeneous Poisson process. There are some R aspects in the edits, but I think the core question is statistical. (IMO this is worth keeping, if it is not a duplicate.)
Comment: @GeoMatt22, you are right, I'm retracting my close vote
Answer: There are already several reasonable answers, but to try and add some clarity, here I will try to synthesize these, and do some verification on the resulting algorithm.

Homogeneous Process
For a homogeneous 1D Poisson process with constant rate $\lambda$, the number of events $n$ in a time interval $T$ will follow a Poisson distribution
$$n\sim\text{Poiss}_\lambda$$
And the arrival times $t_1,\ldots,t_n$ will be i.i.d. uniformly through the interval
$$t_i\sim\text{Unif}_{[0,T]}$$
while the waiting times between events will be i.i.d. exponentially
$$\tau_i=t_{i+1}-t_i\sim\text{Exp}_\lambda$$
with CDF
$$\Pr\big[\tau<\Delta{t}\big]=1-\exp^{-\lambda\Delta{t}}$$
So a homogeneous Poisson process can be easily simulated by first sampling $n$ and then sampling $t_{1:n}$ (or, alternatively sampling $\tau$ until $t=\sum\tau>T$).

Inhomogeneous Process
For an inhomogeneous Poisson process with rate parameter $\lambda(t)$ the above can be generalized by working in the transformed domain
$$\Lambda(t)=\int_0^t\lambda(s)ds$$
where $\Lambda(t)$ is the expected cumulative number of events up to time $t$. (As noted in Aksakal's answer, and the reference cited in jth's answer.)
In the generalized approach we simulate in the $\Lambda$ space, where "time" is dilated so in the deformed timeline the Poisson process is now a homogeneous process with unit rate. After simulating this $\Lambda$ process, we map the samples $\Lambda_{1:n}$ into arrival times $t_{1:n}$ by inverting the (monotonic) $\Lambda(t)$ mapping.
Note that for the piecewise-constant rates $\lambda(t)$ here, the mapping $\Lambda(t)$ is piecewise linear, so very easy to invert.

Example Simulation
I made a short code in Octave to demonstrate this (listed at the end of this answer). To try and clear up questions about the validity of the approach for small rates, I simulate an ensemble of concatenated simulations. That is, we have 3 rates $\boldsymbol{\lambda}=[\lambda_1,\lambda_2,\lambda_3]$ each of duration 1 hour. To gather better statistics, I instead simulate a process with $\boldsymbol{\hat{\lambda}}=[\boldsymbol{\lambda},\boldsymbol{\lambda},\ldots,\boldsymbol{\lambda}]$, repeatedly cycling through the $\boldsymbol{\lambda}$ vector to allow larger sample sizes for each of the $\lambda$'s (while preserving their sequence and relative durations).
The first step produces a distribution of waiting times (in $\Lambda$ space) like this

which compares very well to the expected theoretical CDF (i.e. a unit-rate exponential).
The second step produces the final ($t$ space) arrival times. The empirical distribution of waiting times matches well with theory here as well:

This figure is a little bit more busy, but can be interpreted as follows. First, the upper panel shows the component CDFs associated with a homogeneous Poisson process at each of the three $\lambda$'s. We expect the aggregate CDF to be a mixture of these components
$$\Pr\big[\tau<\Delta{t}\big]=\sum_iw_i\Pr\big[\tau<\Delta{t}\mid\lambda=\lambda_i\big]$$
where $w_i=\Pr\big[\lambda=\lambda_i\big]$ are the mixing fractions.
For each component process, the expected number of samples is $\langle{n_i}\rangle=\lambda_iT_i$. Since the durations $T_i$ are equal, the expected mixing weights will then scale with the rates, i.e.
$$w_i\propto\lambda_i$$
The lower panel above shows the expected CDF obtained by mixing the components in proportion to their rates. As can be seen, the empirical CDF of the inhomogeneous simulation is consistent with this theoretically expected CDF.

Example Code
The following code can be run here. (Note: the algorithm is only 4 lines; the bulk of the code is comments and verification.)
%% SETUP
lam0=[1.5,2.1,3.4]; dt0=ones(size(lam0)); % rates and their durations
Nrep=1e3; lam=repmat(lam0,[1,Nrep]); dt=repmat(dt0,[1,Nrep]); % do replications (for stats check)

%% SIMULATION
L=cumsum([0,lam.*dt]); t=cumsum([0,dt]); % cumulative expected # events and time
Lmax=L(end); N=poissrnd(Lmax); % sample total # events
Lsmp=Lmax*sort(rand([1,N])); % sample event times (in "L space")
tsmp=interp1(L,t,Lsmp); % transform to "t space"

%% STATS CHECK
% "L space" waiting time CDF
dL=sort(diff(Lsmp)); p=(1:N-1)/N; % simulated
p0_L=1-exp(-dL); % exponential
h=plot(dL,p,'k',dL,p0_L,'r','LineWidth',1.5); set(h(1),'LineWidth',4);
axis tight; xlabel('dL'); legend('data','theory (exp)',4); title('L space CDF');

% "t space" waiting time CDF
dT=sort(diff(tsmp)); % simulated
wcmp=(lam0.*dt0)/(lam0*dt0'); pcmp=1-exp(-lam0'*dT); p0=wcmp*pcmp; % mixture
subplot(211); plot(dT,pcmp); ylabel('CDF'); title('Mixture Components'); axis tight;
legend(cellstr(num2str(lam0','lam = %0.1f ')),4);
subplot(212); h=plot(dT,p,'k',dT,p0,'r','LineWidth',1.5);  set(h(1),'LineWidth',4);
axis tight; xlabel('dt'); ylabel('CDF'); title('Aggregate');
legend('data','theory (exp mixture)',4);

% mean arrival rate
tbin=0.5:2.5; Navg=hist(mod(tsmp,3),tbin)/Nrep; bar(tbin,Navg,1);
hold on; plot(tbin,lam0,'r.'); hold off;
legend('observed (hist.)','theory (lam.)',0);
xlabel('hour'); ylabel('arrivals/hour');

As requested in the comments, here is an example of the simulated mean arrival rates (see also updated code above):
Comment: I wonder if we could include calculate the empirical rate $\lambda_i$ from $\frac{1}{dT_i}$ (segregated into different time sessions)?
Comment: You just do a histogram on arrival times modulo 3 (see update). If the answer was helpful, feel free to upvote it :)  BTW the idea of concatenating the simulations (to get larger samples) should apply to any of the techniques suggested in the other answers as well.
Comment: I decided to accept this answer as it illustrated fully the conceptual and statistical aspects (even though the answer originated from others). The final histogram just made the whole answer complete to me.
Answer: There are several methods. For simple rate functions like yours I find the inversion approach to be the easiest. See algorithm 3 of this paper; it's 5 lines. Edit: and the algorithm is:
rates = c(1,2,3)
s = sum(rates)
cs = c(0, cumsum(rates))
R = function(t) {
  L = length(rates)
  t0 = floor(t)
  d = t0 %% L + 1
  return(s * floor(t / L) + cs[d] + (t - t0) * rates[d])
}
Rinv = function(y) {
  t0 = L * y / s
  return(uniroot(function(t) R(t) - y, c(t0, t0 + L))$root)
}
# Actual algorithm itself is only 2 lines.
x = cumsum(rexp(1000))  # desired nbr of points
y = sapply(x, Rinv)

If efficiency matters then you'd want to analytically invert R(t).
Comment: Can you add the 5 lines? (To make your answer self-contained and avoid link-rot, see [here](https://stats.stackexchange.com/help/how-to-answer).)
Comment: FYI the linked reference is [Pasupathy, Raghu. "Generating Nonhomogeneous Poisson Processes." Wiley Encyclopedia of Operations Research and Management Science (2010).](https://scholar.google.com/scholar?cluster=9959853397508521067)
Answer: It's pretty straightforward, thanks to the fact that the interarrival times of a Poisson process are exponentially distributed and the exponential distribution has the memoryless property.  (As an aside, note that if you are generating the time points, you are generating the interarrival times too, as they are just the differences between successive time points, and vice versa.)
One strategy is to generate the number of arrivals in each of the 3 x 1000 hours, then make use of the fact that the arrival times are uniformly distributed within each hour (thanks to the memoryless property.)  If you have, say, 3 arrivals in hour 1, you can generate the arrival times for that hour by generating three uniform variates over the hour.
You can ignore "crossing the hour boundary" effects, also because of the memoryless property of the exponential distribution.
For example:
rates <- c(1.5, 2.1, 3.4)
arrival_times <- c()
for (hour in 1:3000) {
   n_arrivals <- rpois(1,rates[1 + hour%%3])
   arrival_times <- c(arrival_times, runif(n_arrivals) + hour - 1)
}
arrival_times <- sort(arrival_times)

The first and last few arrival times are:
> c(head(arrival_times), tail(arrival_times))
 [1] 6.175204e-02 5.907350e-01 1.066275e+00 1.089332e+00 1.638492e+00 2.296899e+00 2.996753e+03 2.996817e+03 2.997005e+03
[10] 2.997376e+03 2.998619e+03 2.999689e+03

Comment: @user2513881 OK, that makes sense. But does that mean the simulation algorithm is wrong? For a low rate **and** a short timespan, the exponential will necessarily have a severely truncated tail. Try the following: First simulate 1000 times, i.e. $\vec{t}_i$ for $i=1:1000$. Then concatenate the event times together, offset by $i-1$, i.e. $[\vec{t}_1,\vec{t}_2+1,\ldots ]$. When I do this, the concatenated $\Delta\vec{t}$ has the appropriate exponential PDF.
Comment: I tried to verify the mean of interarrival times. But the result is not as expected. Please see the edited question.
Comment: @user2513881 for such small numbers of samples, it is expected that the mean will vary a lot. To judge you could check the associated confidence intervals (e.g. see [here](https://en.wikipedia.org/wiki/Exponential_distribution#Confidence_intervals)). Or, more simply, just simulate a bunch of times (e.g. until $\sum n_\text{event}$ exceeds some threshold) and look at the ensemble statistics.
Comment: @GeoMatt22: "I only quote 1 result but I tried for 30+ times to reduce randomness". I further tried 10,000 simulations (each with 3,000 hours). The aggregated mean turns out to be `[1] 2.473990 2.587038 1.797931`. For the hours simulated with rate `3.4`, the range is `2.268 - 2.906`. Obviously CI will not cross the expected mean of 3.4. Running the simulation with 1 arrival for 1000 times is not the same as running another with 100 arrival for 10 times. This simulation algorithm does not converge to mean rate for small rates, seemingly.
Comment: In [Octave](http://octave-online.net/) I did this: `lam=3.4; N=1e3; T=[]; for t0=1:N; n=poissrnd(lam); t=sort(rand(1,n)); T=[T,t+t0-1]; end; dT=diff(T); m=numel(dT); p=(1:m)/(m+1); dT=sort(dT); plot(dT,p,dT,1-exp(-lam*dT));`
Comment: I also observed that the code gives out converged rates even for rates as small as 1.5, if I make the process homogeneous, i.e. `rates <- c(1.5, 1.5, 1.5)`. Strangely it doesn't when the rates vary with time. I also tried to **calculate mean after aggregation of $10000\times3000$ data** (using my understanding with your pseudocode) rather than **aggregating 1000 means as CI**. No luck.
Comment: user2513881 - To illustrate a problem you'll have with your algorithm, what interarrival time do you use in your estimation if there were no events in the interval?  That says there was an interarrival time of > 1 hour  for the process over the specified interval, but there's no actual number there to use when calculating a mean.  This is a more concrete way of illustrating @GeoMatt22's point above.
Comment: You can ignore "crossing the hour boundary" effects - what does this mean? I'm afraid this effect is very significant given the low intensities
Comment: @Aksakal - you can ignore the issue of having, for example, an event occurring at timestamp 2.9 with the next event's mean interarrival time being 1/3.4 hours and not having an event occur between timestamp 2.9 and timestamp 3, when the interarrival time changes to 1/1.5 hours.  You can do so because, conditional upon no event occurring in that 0.1 hour timespan (which we know it didn't, because we know how many events did occur in the hour and when they occurred) the time to the next event is now distributed exponential with mean 1/1.5 (ignoring the fact of another reset after 1 hour.)
Comment: ... the point being that you don't have to keep track of how much time there was between the last event and the end of the last interval in order to correctly generate the time of the next event.
Comment: @jbowman, the problem with your reasoning is this: you drew two events, then discarded one of them, by doing so you changed the distribution. I'll use your example: how do you know that no event has happened between 2.9 and 3? The only way to "know" it is by generating a next arrival time, and checking whether it pushes further than 3. Let's say it was 0.5 hours. Where are you going to place this event now? You're throwing it out, saying that "I know that nothing has happened between 2.9 and 3", then you proceed generating events in the next period. This is not Poisson process anymore
Comment: I gave an [answer](https://stats.stackexchange.com/a/276681/127790) using the "inversion method" (time dilation), and tried to add some verification to settle the disagreements in the comments here. (At least I *think* I got it right!) I think your approach is most likely correct: If the "edge crossing" was significant, then shouldn't it affect any Poisson model at its beginning (i.e. history would be needed to model the "first" event)? Perhaps you could add a demo doing as I did (cycling through the 3-hour sim. many times), to verify your solution's correctness?
Comment: @Aksakal - I know because there were (say) three events in the time between 2 and 3, the first happened at 2.4, the second at 2.6, and the third at 2.9.  Therefore there cannot be any events between 2.9 and 3, because I've already accounted for all three events that happened between 2 and 3.   Your final point is also incorrect - thanks to the memoryless property, we can, for example, generate an exponential time, and if it's greater than 3 (in this case), discard it and generate a new exponential time with the >3 rate and add "3" to it to get the time of the next event.
Comment: @jbowman, the memoryless property doesn't mean that you can discard the event. You're messing up with process, it's not Poisson anymore. I'd suggest you try to simulate the times with large sample and look at the event counts in each period, I bet that they'll be biased.
Comment: @Aksakal I tried to simulate with 10,000 replicates (totally 30,000 hours) and looked at the event counts with code modified from jbowman. I don't want to overload this comment, please refer to my Edit 3.
Comment: @Aksakal - No, they won't be biased.  They are as exactly Poisson distributed over each interval as the Poisson RNG allows.   I know this because I'm using a Poisson RNG to generate the number of events in each interval, so how could the event counts in each interval be anything but Poisson with the event-specific mean?  Furthermore, I am not discarding events.  I don't like saying this, because I've read more than enough of your work to know this is virtually never the case, but I suspect you may not have quite grasped the approach I'm taking, which does not involve interarrival times at all.
Answer: Your intensities are so low that it's impossible to ignore the boundary effects. By that I mean that a time between the last event in the first hour and the first event in the next hour is covered by two Poisson (exponential) processes.
Method 1:
So, the simplest way to treat this (certainly, not the most efficient) is to construct the nonhomegenous intensity $\lambda(t)$ function, where $t$ is time. Next, step in time by one second (or a smaller interval), then generate the Bernoulli experiments, until you get to the end of the time.
Method 2:
However, a much better (faster) solution is to scale the time instead of scaling the intensity. Here's the idea. You have a homogenous Poisson process, BUT the time speeds up after one hour, NOT the intensity increases. Your clock ticks faster after the first hour by a factor of $\frac{2.1}{1.5}=1.4$ or $\frac{3.4}{1.5}\approx 2.27$. So, all you need to do is to generate a bunch of exponential arrival times, then compress the times after the first hour by appropriate factors.
Solution for Method 1
This is not R forum, and other folks might be interested in the solution, so here's a pseudo code:
function y = lambda(t)
  % nonhomegenous Poisson intensity
  % t - time is hours
  if t<1
    y = 1.5;
  else if t<2
    y = 2.1;
  else
    y = 3.4;
  end if
end

function times = f()
  % times in hours of the event occurrences
  list times;
  dt = 1/3600;
  for t = 0 to 3 step dt
    if rand() < lambda(t)*dt
      times.add(t);
    end if
  end
end

Solution for Method 2
function times = f()
  lambda = 1.5
  list times
  t = 0
  do
    dt = random_exponential(lambda)
    t = t + dt
    st = compress(t)
    if st<3
      times.add(st)
    end if
  while st < 3
end

% compress slow time to faster times after 1st hour
function st = compress(t)
  scale2 = 2.1/1.5
  scale3 = 3.4/1.5
  t2 = 1 + scale2;
  st = min(t,1) + (min(t,t2)-min(t,1))/scale2 + (max(t,t2)-t2)/scale3
end

Comment: (+1) I think this is the same approach as @jth, but the added explanation and generic pseudocode is much more clear.
Comment: If ``dt > 3`` in Method 2 then is the calculated rate correct?
Comment: @jth, that's a bug, you're right
Comment: I bit the bullet and tried my own hand at an answer, based on the time-dilation idea suggested by you and @jth. About the same, but I tried to add some verification to hopefully increase the OP's confidence in the result (and increase the non-R code diversity, I guess :) ).
Comment: You are trying to do this by generating interarrival times, but the OP said he/she did not want to generate interarrival times.  Also, you can ignore the boundary effects if you know how many events occurred in each hour, and you can find that out by randomly generating them, because they are distributed Poisson with the hour-specific rate.
Comment: @jbowman, OP wants arrival times, that's what's being returned
Comment: @Aksakal, yes you are right. I just don't want interarrival times as an end, but as a means it is more than welcome.
Answer: If Python is not a problem for you, I advise you to have a look at this new open-source library: tick. Among others, it provides simulation and inference methods for point processes. You can find an example explaining the simulation of an inhomogeneous Poisson process here.
Comment: Thanks for your suggestion but I am more inclined to use R.


Correct Fingering for Piano Long Stretches in Bass Clef

Question: I am a piano student at the early intermediate level.  I am fortunate to have long fingers that generally proves helpful.  However in the below arrangement of O Holy Night, the last two sections in the bass clef, I cannot figure out the best fingering.

(image source)
Comment: Please clarify what you mean by "last two sections". Numbering the measures 1 - 10, which ones would you like help with?
Answer: Through the (crude) power of Microsoft Paint, I grant thee some fingerings:

The left hand requires pedal as you can't hold interval from C to a G past an octave. Focused on fluidity of movements in the LH, avoiding unnecessary jumps and utilizing stretching where possible and logical. If there are no fingers for a particular measure, it's because it draws its fingering from an earlier identical measure


How to calculate correct velocity values from accerometer

Question: I need to calculate iphone velocity in space, punch speed for example.
My first question is: Are the acceleration values accelerometer:didAcceleratewith this filtering actions:
gravity_x = acceleration.x * kFilteringFactor + gravity_x * (1.0 - kFilteringFactor);
gravity_y = acceleration.y * kFilteringFactor + gravity_y * (1.0 - kFilteringFactor);
gravity_z = acceleration.z * kFilteringFactor + gravity_z * (1.0 - kFilteringFactor);
float gravityNorm = sqrt(gravity_x * gravity_x + gravity_y * gravity_y + gravity_z * gravity_z);
accelX = acceleration.x - gravity_x / gravityNorm;
accelY = acceleration.y - gravity_y / gravityNorm;
accelZ = acceleration.z - gravity_z / gravityNorm;

the same as using CoreMotion's
motionManager.deviceMotion.userAcceleration.x;
motionManager.deviceMotion.userAcceleration.y;
motionManager.deviceMotion.userAcceleration.z; ?
Next, according to same questions, I do the following
    const float accuracy=10;
    float accx = (float) ((int)  (accelX * accuracy))/ accuracy;
    float accy=  (float) ((int)  (accelY * accuracy))/ accuracy;
    float accz=  (float) ((int)  (accelZ * accuracy))/ accuracy;

for rounding values, then, as I think, I obtain current speed
float dx = accx*9.81+prevX*9.81;
float dy = accy*9.81+prevY*9.81;
float dz = accz*9.81+prevZ*9.81;

speed_after_x = dx/2*myAccelerometer.updateInterval+speed_before_x;
speed_after_y = dy/2*myAccelerometer.updateInterval+speed_before_y;
speed_after_z = dz/2*myAccelerometer.updateInterval+speed_before_z;

according to iphone accelerometer speed and distance
then
    prevX=accx;
    prevY=accy;
    prevZ=accz;

    speed_before_x=speed_after_x;
    speed_before_y=speed_after_y;
    speed_before_z=speed_after_z;

and finally calculating speed as vector's length
    float speed = sqrt(speed_after_x*speed_after_x+speed_after_y*speed_after_y+speed_after_z*speed_after_z);
    if (max_speed<speed)  max_speed = speed;

But speed value, that is containing in label, is always increasing. I mean if i moved device and then stopped, speed value doesn't become to 0. Doesn't acceleration compensate itself at 0?
Comment: Doesn't this cancel itself out:  `((int)  (accelX * accuracy))/ accuracy;`.  Like ((10 * 100) / 100 == 10.  If your intention is to round the number and then cast back to a float there are better ways to do it.
Comment: accuracy is 10, to avoid rapidly acceleration values changing when device is holding.
Comment: Yes, but multiplying a number then dividing by the same number is the equivalent of doing nothing.  (A * B) / B = A.
Comment: I don't think that this is why velocity doesn't change into 0, anyway, I have to use `acceleration.timestamp`s delta, not exactly `updateInterval`
Comment: Oh I know, that's why it's a comment not an answer.
Comment: Is there any reason you are not using Core Motion?  I think that is available in >=ios4.0.
Answer: Here's the example code I managed to hash out.  I'll just put it here for now:
//
//  ViewController.m
//  Acce
//
//  Created by Diziet on 14/05/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () {
    UIAccelerationValue gravX;
    UIAccelerationValue gravY;
    UIAccelerationValue gravZ;
    UIAccelerationValue prevVelocity;
    UIAccelerationValue prevAcce;
}

@property (strong) UIAccelerometer *sharedAcc;

@end

@implementation ViewController

@synthesize sharedAcc = _sharedAcc;

#define kAccelerometerFrequency        50.0 //Hz
#define kFilteringFactor 0.1

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.sharedAcc = [UIAccelerometer sharedAccelerometer];
    self.sharedAcc.delegate = self;
    self.sharedAcc.updateInterval = 1 / kAccelerometerFrequency;

    gravX = gravY = gravZ = prevVelocity = prevAcce = 0.f;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (UIAccelerationValue)tendToZero:(UIAccelerationValue)value {
    if (value < 0) {
        return ceil(value);
    } else {
        return floor(value);
    }
}

#define kAccelerometerFrequency        50.0 //Hz
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    gravX = (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor));
    gravY = (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor));
    gravZ = (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor));

    UIAccelerationValue accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor)) );

    UIAccelerationValue accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor)) );
    UIAccelerationValue accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor)) );
    accelX *= 9.81f;
    accelY *= 9.81f;
    accelZ *= 9.81f;
    accelX = [self tendToZero:accelX];
    accelY = [self tendToZero:accelY];
    accelZ = [self tendToZero:accelZ];

    UIAccelerationValue vector = sqrt(pow(accelX,2)+pow(accelY,2)+pow(accelZ, 2));
    UIAccelerationValue acce = vector - prevVelocity;
    UIAccelerationValue velocity = (((acce - prevAcce)/2) * (1/kAccelerometerFrequency)) + prevVelocity;

    NSLog(@"X %g Y %g Z %g, Vector %g, Velocity %g",accelX,accelY,accelZ,vector,velocity);

    prevAcce = acce;
    prevVelocity = velocity;
}

@end

It'll need modifying for your needs, especially as I'm just throwing the values away afterwards.  The resultant value 'velocity' logged as 'Velocity' tends back towards extremely small negative values after acceleration events have ceased, e.g. *10^-17.  So yeah, that's practically zero in my book.  You'll want to do some rounding in there and probably even scale the values up.  I don't think I could get it higher than 0.2 or 0.3 but then again I don't want to hurl my phone across the room (yet).
Comment: Just because I can't comment, I post here : I think the unit of acceleration is in m/s^2. > A G is a unit of gravitation force equal to that exerted by the earth's gravitational field (9.81 m s−2). https://developer.apple.com/library/prerelease/ios/documentation/CoreMotion/Reference/CMAccelerometerData_Class/index.html#//apple_ref/c/tdef/CMAcceleration But it's with the method whit CMMotionManager to get acceleration...
Comment: I don't think so, because I added this multiplier after found out the problem, and it is used to convert G-measures to m per sec
Comment: It's not the * 9.81 I'm wondering about, that was an 'also'/aside.  It's the fact that you add accx to prevX.  If prevX cannot be negative, which judging by your math I don't think it can be, then you will get a constantly increasing value for acceleration.   You need to have some multiplication of the existing value, such that if the current moment acceleration in a particular axis is zero the acceleration you record will be zero.  I'll put this in the answer.
Comment: why prevX cannot be negative?`prevX=accx;
    prevY=accy;
    prevZ=accz;`accx can be negative. I add accx to prevX according to `velocity[i] = (acceleration[i] + acceleration[i-1])/2 * interval + velocity[i-1]`
Comment: update interval is 0.01, i and i tried 0.05. This filter is for ignoring rotations around its own axes. What values you need to know?
Comment: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11228/discussion-between-diziet-and-aft3rmath)
Comment: thanks) x*x faster then pow(x,2) as i know, am i wrong? and I don't need to calculate acceleration vector as I understand it, I need `i` and `i-1` accel values to calculate velocity as `v[i] = (a[i]+a[i-1])/2*delta t + v[i-1]` what I actually did, the problem is why velocity doesn't change into 0 when I stop moving device
Comment: I think that's as close as I'm going to get to solving this for you without getting some mighty funny looks where I work. :D
Comment: Dude, you are awesome! Don't test yet, but your help is great! Report you soon)
Comment: And I don't understand `UIAccelerationValue acce = vector - prevVelocity;` Why do you subtract previous velocity value from acceleration vector value
Comment: Because the acceleration at that instance is the change in velocity (e.g. current velocity minus previous).  If we are currently going (pretending units are m/s which they are not) 9 m/s and were going 12 m/s our acceleration is -3 m/s.  This lets the equation tend towards zero when motion stopped.  Without that you get continuously increasing values.
Comment: hm, can we compare m/s(velocity) with m/s^2 (acceleration)? Is it correct?
Comment: max detected speed is 0.33 m/s, seems to be incorrect =\ it must be 5-8 m/s with punch, now i'll try to apply it to core motion
Comment: Because I have no idea what the original measurements are in.
Comment: I need to to calculate the punch speed. Plz tell the final method you have implemented ..
Comment: Aha, I didn't know for sure it was G that was returned.  Excellent.
Comment: Why velocity is not  0 when device stoped?


Confused with (trivial?) geometric probability problem.

Question: I am preparing the the probability test. I faced the problem that is expected to be trivial, but I fear I do not get the most essential part of it.
Ok, let point $M$ is located on the ray $R = \{y=0, x \geq 1\}$ in the $(x,y)$ plane. M has Pareto distribution with probability density function $p(x) = 1/x^2,\, x \ge 1$. Let $d(x)$ is distance from $M$ to $(0, 1)$. NOTE: not to (1,0)! I have to find

Cumulative distribution function of $d$.
Probability density function of $d$.
Expected value of $d$.

If I would know how to do $1$ or $2$ I have no doubts that I should be able to handle two other parts of the problem, but I simply do not understand how and why $p(x)$ and $d(x)$ are related.
I feel there should be some use of Pythagorean theorem and integration/differentiation, but I I do not understand the most essential step (and more importantly intuition and explanation behind it) - how to move from $p(x)$ to $d(x)$. What is going on here? How can probability density function be related with position? What does it mean?
Answer: There is some notational sloppiness in the statement of the problem. The point $M$ is not a random variable, but its $x$-coordinate is. And the quantity $d(x)$ is a random variable but it's not explained what $x$ is.
It would make more sense if the problem were:

Point $M$ is located on the ray $R=\{ y=0,x\ge1\}$ in the $(x,y)$ plane. $\color{red}{\text{The $x$-coordinate of}}$ $M$ has Pareto distribution with probability density function $p(x)=\frac1{x^2}, x\ge1$. Let $\color{red}D$ be the distance from $M$ to $(0,1)$.

With this formulation you can observe (using Pythagoras) that $D=\sqrt{X^2+1}$ where $X$ stands for the (random) $x$-coordinate of $M$. You should be able to take it from here.
Comment: Thanks for clarification.. I expected answer similar to this one and I am happy with it, because I thought something very similar but was completely unsure if I understand that.


String update in SQL Server

Question: Currently I have varchar field. The delimiter is "$P$P$".
The delimiter will appear at least once and at most twice in the varchar data.
Eg.
Sample Heading$P$P$Sample description$P$P$Sample conclusion

Sample Heading$P$P$Sample Description

If the delimiter appears twice, I need to insert a text before the second occurance of the delimiter.
Eg:
Sample Heading$P$P$Sample DescriptionINSERT TEXT HERE$P$P$Sample Conclusion

If the delimiter occurs only once, then I need to insert a text at the end of the field.
Eg:
Sample Heading$P$P$Sample DescriptionAPPEND TEXT HERE

How this can be done in SQL query?
Comment: Might I sugest three separate fields?
Answer: If you are going to do a lot of string manipulation you might want to use a CLR (.net) function. Since SQL Server isn't exactly made for string manipulation.
Or even better, pull this data back to your application and do it in code.
I even think you can't do it using the default SQL Server String functions
Answer: The CharIndex function has an optional 3rd parameter that allows you to specify the starting position of the search.  You can use this to find the 2nd occurrence of a string using CharIndex. You can also use the stuff function to insert a string in to another string.
Example:
Declare @Temp Table(Data VarChar(8000))

Insert Into @Temp Values('Sample Heading$P$P$Sample description$P$P$Sample conclusion')
Insert Into @Temp Values('Sample Heading$P$P$Sample Description')

Select len(Data),
        CharIndex('$P$P$', Data + '$P$P$',CharIndex('$P$P$',Data) + 1),
        Stuff(Data + ' ', CharIndex('$P$P$', Data + '$P$P$',CharIndex('$P$P$',Data) + 1), 0, 'Text Here')
From   @Temp

I realize it looks like a mess, but I do encourage you to understand how this works because you may need something similar in the future.
Answer: instead of using delimiters, why not creating 3 columns or if you only want one --> an xml field?

String update in SQL Server


C# How can I embed a connection string in a class?

Question: I've got a C# MVC project I'm working on at the moment with a basic plugin architecture implemented via MEF. Plugins and their corresponding parts get uploaded through a web interface and are placed in a plugin folder they are then discovered at application start.
Some of the plugins will require data access to external databases (not the main database). When the application goes live I won't be able to easily make changes to the main application's web.config file.
So my question is how can I embed a connection string directly into one of my plugin's classes?
Comment: I could store the connection string in a database but I'd still have to refer to it directly in my plugin without modifying the core app's web.config; hence the problem. Bear in mind that I'll have no access to the server the app is running on besides the functionality I've put into the frontend such as being able to load plugins.
Comment: Hi User3861788 Can you help with this question? http://stackoverflow.com/questions/24971606/asp-net-mvc-5-mef-how-to-programmically-import-and-export-parts   Sounds like you were able to have MVC with MEF having a plugin framework.  Do you have an Admin setting page where you can enable and disable plugins?
Answer: A couple of ideas:
1) Just hard code it:
   Con: I think you are concerned about changing the connection string on the fly. So this wouldn't work well for what you need.
2) Store another config file on a shared drive that you CAN access.
   Pro: Let's you change the connection string on the fly.
   Con: Not the best option for security.
3) Use an authenticated web service. On startup, call the web service (and possibly every hour, or on connection failure) to get the connection string.
   Pro: This allows you to change the connection string on the fly.
   Pro: This can be implemented with security
Of the three options, number three seems to best fit your needs. But those are just off the top of my head.


redmine poor write performance (updates not truly multithreaded)

Question: (Self-answered already)
I'm fighting poor performance of my redmine of about 100 users. To test it, I've set up a simple scenario to update "Percent done" on five different example issues. All the updates start at exactly the same second, using curl. To my amazement, the updates in redmine seem serialized (I mean not multithreaded) with timings like this:
real    0m1.122s
real    0m1.404s
real    0m2.258s
real    0m2.782s
real    0m3.151s
real    0m3.336s

Or another try, when some processing by other sessions gets in the way too:
real    0m7.492s
real    0m7.803s
real    0m8.045s
real    0m8.246s
real    0m8.597s
real    0m8.825s

The key moment in the production.log after processing zilliards of SQL queries is the COMMIT phase. Note how COMMITs are done in milliseconds, but something mysterious goes on between COMMITs that not only serializes them but takes seconds to take to Completed status. This log excerpt is intact - I didn't delete any lines in-between.
   (0.6ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (30.1ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/11973
Completed 302 Found in 7418.4ms (ActiveRecord: 135.8ms)
   (0.6ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (24.8ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/9240
Completed 302 Found in 7738.3ms (ActiveRecord: 57.4ms)
   (0.4ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (25.9ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/9614
Completed 302 Found in 7949.0ms (ActiveRecord: 135.7ms)
   (0.5ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (24.6ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/12016
Completed 302 Found in 8058.5ms (ActiveRecord: 102.5ms)
   (0.5ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (21.2ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/8853
Completed 302 Found in 8472.5ms (ActiveRecord: 90.4ms)
   (0.5ms)  SELECT COUNT(*) FROM `custom_workflows` WHERE `custom_workflows`.`active` = 1 AND `custom_workflows`.`observable` = 'issue' AND (is_for_all OR EXISTS (SELECT * FROM custom_workflows_projects WHERE project_id=123 AND custom_workflow_id=id))
   (27.9ms)  COMMIT
Redirected to http://redmine.mydomain.local/issues/10007
Completed 302 Found in 8700.4ms (ActiveRecord: 137.4ms)

What I've already checked:

passenger-status has more threads than enough
apache2 httpd has more processes than enough
mysql 5.1 uses innodb
innotop doesn't show locks
innotop doesn't excessive I/O
iostat doesn't show excessive I/O  (about 10-15 IOPS, disk max 17% time usage)
top doesn't show excessive CPU usage (max 70% of possible 400%).
Answer: OK it was SMTP serializing things; it is somehow unlogged by default.
I've changed /var/www/html/redmine/config/configuration.yml :
  email_delivery:
    # delivery_method: :smtp       # slows down everything!
    delivery_method: :async_smtp


Postgres sql query by time window

Question: I have a table "meterreading" that has columns: "timestamp", "value", "meterId".  I would like to get sums of the "value" for each hour starting a specific time...  So far I have come up with this query, but it is erroring saying I need to group by timestamp.  Timestamps are just integers representing unix epoch timestamps.
select date_trunc('hour', to_timestamp(timestamp)) as hours, sum(value)
              from meterreading
              WHERE timestamp >= 1377993600 AND timestamp < 1409595081
              group by date_trunc('hours', to_timestamp(timestamp))
              order by date_trunc('hours', to_timestamp(timestamp)) asc

Comment: I guess you need a subquery that projects hours directly, on which you will then be able to group... Hint: what about gaps?
Comment: You have `'hours'` in the select and `'hour'` in the `group by`.
Answer: select date_trunc('hour', to_timestamp(timestamp)) as hours, sum(value)
from meterreading
WHERE timestamp >= 1377993600 AND timestamp < 1409595081
group by 1
order by 1

or use the exact same expression used in the select list
group by date_trunc('hour', to_timestamp(timestamp));

Notice 'hour' in instead of 'hours'. Hence the convenience of the number reference syntax in the group by. It is clearer and less prone to errors.

Postgres sql query by time window


Has there ever been an NBA game in which one team did not score a single three-pointer?

Question: Does someone know of an example of an NBA game in which one team did not score a single three-pointer? Did this ever happen?
Answer: First, the three-point line was introduced in the NBA for the 1979-1980 season(1). Therefore, every game before that season did not include a single three-pointer.

Does someone [know] of an example of [an] NBA game in which one team did not score ... a single ... three-pointer?

Between the 1979-1980 NBA season and the 2015-2016 NBA season, this has happened 11,253 times according to Basketball Reference's Team Game Finder(2).
A few interesting points to note:

The number of three point attempts per game has steadily increased since its introduction(1).
Between the 2013-2014 NBA season and the 2015-2016 NBA season (which includes three seasons), the number of times a team did not score a single three-pointer in a game is 6 (including once during the entire 2013-2014 NBA season)(3).
During the 2012-2013 NBA season, the number of times a team did not score a single three-pointer in a game is 5.
Before the 2012-2013 NBA season, a team not scoring a single three-pointer in a game was more common.


Delete columns of a nested list with list comprehension

Question: How would I write this for loop as a list comprehention?
This is the list:
table = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

I want to delete row 4 and 5
for row in table:
    del row[3:]

I did try this but it just gives me a syntax error
table2 = [del row[5:] for row in table]

Any idea how to do that with a list comprehension?
Comment: Welcome, @noskule.   I don't understand the description, starting with "delete row 4 and 5".   I would say that "table" has three rows and five columns.   Did you mean you wanted to delete columns 4 and 5?   What happens if "table" is not rectangular, e.g., [[1,2],[1,2,3],[1]] ?
Comment: A list comprehension is for creating a _new_ list, which is different _behaviour_ from what you have. Why do you think you want to rewrite this?
Comment: Is your intent to delete rows in `table` or to create a new `table2` without the extra rows?
Comment: My intent is to delete rows in table which I dont need so I have no overhead. I get a list of lists like this one:
`[1604448000000, '0.10000000', '0.35000000', '0.10000000', '0.14570000', '3862430.90000000', 1604534399999, '648643.30411400', 4516, '1461260.70000000', '247206.51557700', '0']`

And I need only the first 6 elements:
`[1604448000000, '0.10000000', '0.35000000', '0.10000000', '0.14570000', '3862430.90000000']`
Answer: You can slice the list upto index 3:
>>> table = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
>>> table2 = [i[:3] for i in table]
>>> table2
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

If that is what you want.
Answer: The for loop is already the proper way to remove the rows. You could call the list's __delitem__ method directly, but you are merely using the comprehension for its side effects which is considered "unpythonic". Its done by using a slice object:
table2 = [row.__delitem__(slice(3)) for row in table]

but table2 ends up as the quite boring
[None, None, None]

That's one of the reasons why we don't use list comprehensions this way. Besides being non-obvious, we create useless objects. But if its got the cool factor for you, go for it.


Como fazer para o Apache (wamp) parar de matar a sessão sozinho?

Question: Desenvolvi uma aplicação com laravel e deixei ela rodando no wamp, porém depois de um tempo ele acaba matando a sessão do usuário, gostaria de saber se existe alguma configuração para que ele pare de fazer isso?
Obrigado!
Answer: Existe vários tipos de configurações que você pode fazer no .htacess do Apache:
<IfModule mod_php5.c>

    # Definir o tempo máximo de execucao do script para 30 mins (padrão: 60s)
    php_value max_execution_time 1800

    # Definir o tempo de expiração de sessao para 2 horas (padrão: 24 mins)
    php_value session.cookie_lifetime 7200
    php_value session.cache_expire 7200
    php_value session.gc_maxlifetime 7200

</IfModule>

A documentação aqui: Configuração Session
Você também pode aumentar a expiração do cache da session:
Documentação
E uma boa leitura sobre session do PHP: Link StackOverFlow
Answer: Precisa definir o session.cookie_lifetime e a probabilidade de quanto % do tempo o lixo das sessions expiradas serão coletadas. 0% = nunca.

session.cookie_lifetime especifica o tempo de vida do cookie em segundos que é enviado para o browser. O valor 0 significa "até o browser ser fechado".

Pode configurar no seu php.ini:
#definie para ficar ativo até o browser ser fechado
session.cookie_lifetime = 0

#  0 de probabilidade
session.gc_probability = 0

# 1 de divisão
session.gc_divisor = 1

# Logo o gc_maxlifetime vai ser checado 0% das vezes. (0 / 1 = 0%)
# ou seja, gc_probability / gc_divisor

# seta para ficar ativo até o browser ser fechado.
session.gc_maxlifetime = 0

Pode configurar direto no PHP, em tempo de execução:
// definie para ficar ativo até o browser ser fechado
ini_set('session.cookie_lifetime', 0);

// 0 de probabilidade
ini_set('session.gc_probability', 0);

// 1 de divisão
ini_set('session.gc_divisor',1);

// Logo o gc_maxlifetime vai ser checado 0% das vezes.
// (0 / 1 = 0%) ou seja, gc_probability / gc_divisor

// ativo até o browser ser fechado
ini_set('session.gc_maxlifetime', 0);

// SOMENTE APÓS as configurações, abre a nova seção
session_start();

Comment: Vou testar, obrigado pela ajuda
Comment: Aparentemente funcionou, obrigado
Answer: Verifique se o seu php.ini , tem um valor para session.gc_maxlifetime (e também session.cookie_lifetime ), que estabelece um limite de quanto tempo PHP permitirá sessões para durar.
Uma solução consiste em verificar o seu php.ini. Você pode ter essa variável definida: session.gc_maxlifetime. Por padrão, ele é definido para 1440. Apenas comentar ou excluí-lo .
Comment: Vou tentar fazer isso, para saber se deu certo terei que aguardar um tempo para ver se não mata a sessão rsrsrs.
Comment: Não funcionou :/


Drupal Shurly before redirect

Question: I want  create to something like adf.ly script with Drupal. I am using shurly (developed by Jeff Robins) for URL shortening, and it works fine, but I want to display an advertising page for 10 seconds before redirecting to the final URL, which is not a feature provided by this module.
It has a hook called hook_shurly_redirect_before() which may help me, but I don't have enough knowledge to use it.
Can anyone explain how I should use this hook?
Comment: Shurly module does its job during the bootstrapping so most of the standard Drupal functionality is unavailable here. You will need to include necessary files manually, theme the page through a template file, and then add a mata refresh or something. I'm not quite sure how to do it well though.
Answer: With a quick glance at the shurly code, I'd think this should work:
function yourmodule_shurly_redirect_before(&$redirect) {
  $redirect->destination = 'path/to/advert?somevar='. $redirect->destination;
}

And then you need to set up your advertisement page as path/to/advert and have it redirect to $_GET['somevar'] after the timeout.

Drupal Shurly before redirect


Where are django-lean reports stored?

Question: I'm new to django-lean.
Does anyone know where are the reports generated by calling manage.py update_experiment_reports  are stored?
Answer: I don't have django-lean installed, but based on experiments.reports, I would suggest looking at these two tables (even via the admin), DailyConversionReport and DailyEngagementReport.
Answer: You can access these reports via:
<code>/admin/django-lean/<experiment_name>/</code>

Where are django-lean reports stored?